How can I make a selectable listbox in REACT with antd? Using antd is optional if it…
- User
How can I make a selectable listbox in REACT with antd? Using antd is optional if it doesn't have a component for it, but how would I do it in REACT? Remember, I am specifically talking about a box, with a list of items, I can click to select an item in the list, and it highlights, the selection can be retrieved etc for use in other components.
- Assistant
- User
I used the one without antd, as I wanted an actual select box, as opposed to a hacky solution using checkboxes. But I am getting data from a FastAPI backend, the structure is a Concept which contains two properties: id and name so I changed the html to be:
<div> <select size="20" value={selectedValue} onChange={handleChange}> {items.map((item) => ( <option key={item.id} value={item}> {item.name} </option> ))} </select> </div>I also have this for the state and events:
// New Listbox const [selectedValue, setSelectedValue] = useState(null); const [items, setItems] = useState([]); useEffect(() => { refreshConcepts(); refreshItems(); }, []); // Continuing new listbox const refreshItems = async () => { try { const response = await conceptsGetPaged(0, 1000); setItems(response.data.items); } catch (error) { console.error("Error fetching concepts:", error); } }; const handleChange = (e) => { setSelectedValue(e.target.value); }; - Assistant
You're right about the issue with setting
value={item}in your tags. The value of an in HTML must be a string, but is an object. To access the full item or its ID, you can store the selected item's ID in the component's state and then retrieve the corresponding item object from your items array when needed. - User
In REACT, how can I see if one string starts with another string? or contains another string(partially, not exact match)?
- Assistant