Skip to content

How to set default selected to an item #84

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
madankz opened this issue May 4, 2020 · 2 comments
Open

How to set default selected to an item #84

madankz opened this issue May 4, 2020 · 2 comments

Comments

@madankz
Copy link

madankz commented May 4, 2020

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

Thanks!

@zk-phi
Copy link

zk-phi commented May 29, 2020

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

@heri16
Copy link

heri16 commented Jan 29, 2021

It's best handled this way: #77 (comment)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants