react-selectable-fast
react-selectable-fast copied to clipboard
How to set default selected to an item
trafficstars
I want to set some items default selected how can I achive that.
Thanks!
FYI: It seems there aren't any official APIs to do that (as far as I see), but if you may use unofficial APIs, this worked for me:
class Foo extends React.Component {
constructor () {
super();
this.selectableGroup = React.createRef();
}
/** like SelectableGroup.selectAll(), but select only elements with `defaultSelected` prop */
selectDefaults () {
const selectableGroup = this.selectableGroup.current;
selectableGroup.removeIgnoredItemsFromRegistry();
for (const item of selectableGroup.registry.values()) {
if (item.props.defaultSelected) {
item.setState({ isSelected: true });
selectableGroup.selectedItems.add(item);
}
}
selectableGroup.setState({ selectionMode: true });
}
componentDidMount () {
this.selectDefaults();
}
render () {
return (
<SelectableGroup ref={ this.selectableGroup } ...>
{ list.map((item, ix) => <SelectableItem defaultSelected={ ... } key={ ix }) }
</SelectableGroup>
);
}
}
It's best handled this way: https://github.com/valerybugakov/react-selectable-fast/issues/77#issuecomment-769557329