hc-oss / react-multi-select-component

Lightweight (~5KB gzipped) multiple selection dropdown component
https://codesandbox.io/s/react-multi-select-example-uqtgs
MIT License
386 stars 89 forks source link

Cannot apply custom Icon #343

Closed Youn-ha closed 3 years ago

Youn-ha commented 3 years ago
스크린샷 2021-03-23 오전 11 27 27

(screen capture above is normal case)

스크린샷 2021-03-23 오전 11 23 36

(screen capture above is error case)

I tried to apply custom Clear icon by using ClearSelectedIcon. But it doesn't work in my environment. Anytime I use ClearSelectedIcon, clear <button> gets empty. (like screen capture above) It would be so thankful if somebody could help me out. Here's my code below.

  const clearBtnRenderer = () => (
    <>
      // I tried several tags(img tag, text), but nothing worked
      <svg
        width="24"
        height="24"
        fill="none"
        stroke="currentColor"
        strokeWidth="2"
        className="dropdown-search-clear-icon gray"
      >
        <line x1="16" y1="8" x2="8" y2="16"></line>
        <line x1="8" y1="8" x2="16" y2="16"></line>
      </svg>
    <>
  );

  return (
    <div className="a_search_multiselect_box">
      <MultiSelect
        options={optionList}
        value={selected}
        onChange={handleChange}
        ClearSelectedIcon={clearBtnRenderer}
      />
    </div>
  );
};

I refered to storybook below. react-multi-select Storybook : custom arrow

harshzalavadiya commented 3 years ago

Hi @Youn-ha,

instead of passing as function please pass it as rendered component like below and it should work as expected

<MultiSelect
  options={options}
  value={selected}
  onChange={setSelected}
  labelledBy={"Select"}
- ClearSelectedIcon={clearBtnRenderer}
+ ClearSelectedIcon={<ClearBtnRenderer/>}
/>
Youn-ha commented 3 years ago

Hi @harshzalavadiya ,

I tried below -

1.

  const clearBtnRenderer = () => (
    <>
      <svg
        width="24"
        height="24"
        fill="none"
        stroke="currentColor"
        strokeWidth="2"
        className="dropdown-search-clear-icon gray"
      >
        <line x1="16" y1="8" x2="8" y2="16"></line>
        <line x1="8" y1="8" x2="16" y2="16"></line>
      </svg>
    <>
  );

  return (
    <div className="a_search_multiselect_box">
      <MultiSelect
        options={optionList}
        value={selected}
        onChange={handleChange}
        ClearSelectedIcon={<clearBtnRenderer />}
      />
    </div>
  );
};

===> type error : Type 'Element' is not assignable to type 'string | Function'.

2.

  return (
    <div className="a_search_multiselect_box">
      <MultiSelect
        options={optionList}
        value={selected}
        onChange={handleChange}
        ClearSelectedIcon={() => <clearBtnRenderer />}
      />
    </div>
  );
};

===> there was no type error, but clear button didn't show up.

3.

  return (
    <div className="a_search_multiselect_box">
      <MultiSelect
        options={optionList}
        value={selected}
        onChange={handleChange}
        ClearSelectedIcon={() => (
          <svg
            width="24"
            height="24"
            fill="none"
            stroke="currentColor"
            strokeWidth="2"
            className="dropdown-search-clear-icon gray"
          >
            <line x1="16" y1="8" x2="8" y2="16"></line>
            <line x1="8" y1="8" x2="16" y2="16"></line>
          </svg>
        )}
      />
    </div>
  );

===> this one either.

(And sorry btw how can I show subtracted/added code like you in code markdown? I tried to google it but failed to find it)

harshzalavadiya commented 3 years ago

@Youn-ha

harshzalavadiya commented 3 years ago

released v3.1.7 should fix this typecheck issue

Youn-ha commented 3 years ago

I updated to v3.1.7 and it works well! Thank you so much

prathambhardwaj commented 2 years ago

Hi, I want to create a customized clear icon for selected option, there should be a clear icon for every selected item, is that possible in react-multi-select-component?

harshzalavadiya commented 2 years ago

@prathambhardwaj you can do that but I would recommend using react-select

prathambhardwaj commented 2 years ago

Could you please tell me the custom element which can provide the clear icon to every selected item?

harshzalavadiya commented 2 years ago

@prathambhardwaj it's valueRenderer