I tried to customize Select component from react-select library.
I customized multiSelectValue with span elements and added commas to labels. but it doesn't work well with input. It works as expected when items selected in dropdown list. But when I try to input value for search options then input is focused, and span items shifts to left, as a result field become empty.
Multiselect
export default function App() {
const [values, setValues] = useState([]);
return (
<div className="root">
<Select
styles={styles}
isSearchable
isMulti
getOptionValue={(option) => option["value"]}
options={options}
value={values}
onChange={(options) => {
setValues(() => options);
}}
components={{
MultiValue: CustomMultiValue
}}
/>
</div>
);
}
Customized selected value
function CustomMultiValue(props) {
const { getValue, data } = props;
const selectedOptions = getValue();
const currentOptionIdx = selectedOptions.findIndex(
(option) => option.value === data.value
);
return (
<span>
{data.label}
{currentOptionIdx === selectedOptions.length - 1 ? "" : ", "}
</span>
);
}
Sandbox project https://codesandbox.io/s/boring-knuth-kfo1e?file=/src/CustomMultiValue.js
