这意味着您可以将 aref
应用于SelectableGroup
对象并以编程方式调用clearSelection
ref 以清除所有选定的条目。
例子:
class App extends Component {
constructor() {
super();
this.selectionRef = createRef();
this.items = [ 'one', 'two', 'three', 'four'];
}
clearSelectionUsingRef = () => {
if(this.selectionRef) {
this.selectionRef.current.clearSelection();
}
}
render() {
return (
<div>
<button onClick={this.clearSelectionUsingRef}>Clear Selection using Ref</button>
<SelectableGroup
className="main"
clickClassName="tick"
enableDeselect
tolerance={10}
globalMouse={true}
ref={this.selectionRef}
allowClickWithoutSelected={false}
>
<main style={{ border: "0.1rem solid crimson" }}>
{this.items.map((item) => (
<Item item={item} />
))}
</main>
</SelectableGroup>
</div>
);
}
}
ref
可以作为this.ref.current
. 查看更多关于React 中的 Refs
请注意,您还可以使用DeselectAll
库中的组件来清除选择。但这只能在SelectionGroup
自身内部使用,而ref
可以在外部使用。
在 Stackblitz 上观看现场演示