Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing the clearIndicator X with custom text in React-Select

I need to remove the clearIndicator's default X in the select component on MultiSelect and replace it with custom text. Is there a way to do this without losing the ability to remove the selected options (as happens with isClearable={false})?

Code:

export const MultiSelect = () => {
  const [selected, setSelected] = useState([]);

  const options = [
    { value: '1', label: 'Label1' },
    { value: '2', label: 'Label2' },
    { value: '3', label: 'Label3' },
  ];

const customStyles = {
    control: (prevStyle, { isFocused }) => ({
      ...prevStyle,
      backgroundColor: 'rgba(248, 251, 251, 1)',
      boxShadow: 'none',
      borderColor: isFocused ? 'black' : 'grey',
      ':hover': {
        borderColor: isFocused ? 'black' : 'grey',
      },
    }),
    clearIndicator: (prevStyle) => ({
      ...prevStyle,
      color: 'rgba(0, 0, 0, 0.4)',
      ':hover': {
        color: 'rgba(0, 0, 0, 0.4)',
      },
    }),
  };

return (
    <ReactSelect
      ref={reactSelectRef}
      placeholder={placeholder}
      instanceId={`multiselect-${id}`}
      styles={customStyles}
      isOptionSelected={isMulti && isOptionSelected}
      options={getOptions()}
      value={getValue()}
      onChange={isMulti ? onChangeHandler : onChange}
      hideSelectedOptions={false}
      closeMenuOnSelect={!isMulti}
      formatGroupLabel={formatGroupLabel}
      isMulti={isMulti}
    />
  );

like image 675
BBHeX Avatar asked Oct 29 '25 15:10

BBHeX


1 Answers

React Select has an option of passing in your own custom Components Docs

Would look something like this

 <Select
    //You can pass in any component as the ClearIndidcator and do whatever customizations you want
    components={{ ClearIndicator: () => <div>Clear</div> }}
    {...props}
 />
like image 167
Moshe Sommers Avatar answered Oct 31 '25 08:10

Moshe Sommers