I have a dropdown. Users are able to select multiple items from the dropdown. At the same time, users are able to deselect the items that they have selected by clicking the selected items again. If users select all, all numbers in the array will display.
How to implement this? This is what I have so far enter link description here
import "./styles.css";
import React, { useState } from "react";
const options = [1, 2, 3, 4, 5];
export default function App() {
const [nums, setNum] = useState([]);
const handleArrChange = ({ target }) => {
setNum([...nums, target.value]);
};
return (
<div className="App">
<select
onChange={handleArrChange}
value={"select a number" || nums.length > 0}
>
<option disabled>select a number</option>
{options.map((option) => (
<option>{option}</option>
))}
</select>
{nums.map((num) => (
<p>{num}</p>
))}
</div>
);
}
It seems like a multiple selector (A custom implementation of a select)
Example:
Ref: https://www.cluemediator.com/how-to-get-selected-by-only-value-for-multi-select-in-react-select