Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep Semantic-React multiple selection dropdown open when removing an item

I'm trying to keep the multiple selection dropdown available from Semanti-UI-React open when I remove a selected item, but still have it close onBlur. I can set closeOnBlur to false and I get the behavior I want for removing the selected item, but then I have to click the arrow on the dropdown to close it. I tried plugging into the relevant events to see if there was a way I could achieve the desired behavior manually, but oddly enough if closeOnBlur is set to false, I don't even get the onBlur event.

Here is my code:

<Dropdown
    style={{whiteSpace: 'nowrap', zIndex: 9999}}
    upward={true}
    search={true}
    multiple={true}
    selection={true}
    loading={loading}
    options={options}
    value={this.props.selectedCodes || []}
    closeOnBlur={false}
    onBlur={() => console.log('onBlur')}
    onChange={ (e, d) => {
        console.log('onChange');
        const value = d.value as string[];
        const displayValues = value.map(code => {
            const lookupEntry = options.find(i => i.value === code);
            return lookupEntry ? lookupEntry.text : '';
        });
        const displayValue = displayValues.reduce(
            (result, text) => {
                return `${result}, ${text}`;
            }, ''\
        );
        this.props.onSelectionChange(value, displayValue);
    }}
    onClose={() => console.log('onClose')}
/>

Any suggestions on how to achieve my desired behavior, or insight as to why the onBlur even doesn't fire if closeOnBlur is set to false?

like image 587
TWacaster Avatar asked Dec 31 '25 18:12

TWacaster


1 Answers

This component supports manual control over it's open/closed status view an open prop. So simply manage that prop via the state of your custom containing class;

getInitialState() {
    return { open: false };
},

handleClose() {
    this.setState({open: false});
},

handleOpen() {
    this.setState({open: true});
}

render() {
    return <Dropdown
            open={this.state.open}
            onBlur={this.handleClose}
            onFocus={this.handleOpen}
            ...
            />;
like image 178
Jake Haller-Roby Avatar answered Jan 03 '26 11:01

Jake Haller-Roby



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!