react-selectable-fast icon indicating copy to clipboard operation
react-selectable-fast copied to clipboard

How to set default selected to an item

Open madankz opened this issue 5 years ago • 2 comments
trafficstars

I want to set some items default selected how can I achive that.

Thanks!

madankz avatar May 04 '20 11:05 madankz

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>
        );
    }
}

zk-phi avatar May 29 '20 08:05 zk-phi

It's best handled this way: https://github.com/valerybugakov/react-selectable-fast/issues/77#issuecomment-769557329

heri16 avatar Jan 29 '21 04:01 heri16