react-selectize icon indicating copy to clipboard operation
react-selectize copied to clipboard

Remove selected option from multiselect with mouse?

Open NoraCodes opened this issue 7 years ago • 3 comments

Would it be possible (or is there a way to do this with the API) to optionally allow clicking on a selected option to unselect it in the MultiSelect? Having to use the keyboard is kind of annoying.

NoraCodes avatar Sep 26 '17 17:09 NoraCodes

@LeoTindall You can implement this with renderValue

     renderValue = ({ label, value }) => {
	return (
		<div className="simple-value">
			<span>{label}</span>
			<button
				type="button"
				onClick={this.handleOnRemoveItemClick.bind(this, value)}>Delete</button>
		</div>
	);
      };

mufasa71 avatar Oct 12 '17 12:10 mufasa71

@mukimov How would you implement handleOnRemoveItemClick?

sverrejb avatar Oct 26 '17 14:10 sverrejb

@sverrejb

MultiSelect has value prop, which indicates currently selected options. You can implement onValuesChange and store selected values in your state.

  handleOnValuesChange = (values) => {
    this.setState({ values });
  }     

  handleOnRemoveItemClick(selectedValue) {
    this.setState(({ values }) => {
       return {
         values: values.filter(v => v !== selectedValue),
       };
     });
   }

   render() {
     return <MultiSelect value={this.state.selectedValues} onValuesChange={this.handleOnValuesChange} />;
   }

mufasa71 avatar Oct 27 '17 08:10 mufasa71