furqanZafar / react-selectize

http://furqanzafar.github.io/react-selectize/
Apache License 2.0
704 stars 138 forks source link

Remove selected option from multiselect with mouse? #176

Open NoraCodes opened 7 years ago

NoraCodes commented 7 years ago

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.

mufasa71 commented 7 years ago

@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>
    );
      };
sverrejb commented 7 years ago

@mukimov How would you implement handleOnRemoveItemClick?

mufasa71 commented 7 years ago

@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} />;
   }