You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm having trouble with the "Combobox" component. I've bound an object to it and set the displayValue property to the value I want. However, I'm unable to submit a different value than the display value.
When I change the value of the Combobox.Option component to the desired value, it only passes that value to the displayValue property, while ignoring the option value onSubmit. Below is a small code example where I want to send the value property instead of the name property, but when I press send, I keep getting returned the name property or any value that is inside the displayValue property.
This behavior seems weird because the documentation explicitly states that this component can take complex objects, yet it only takes a display value and ignores the rest of the object. Is there a way to make the displayValue and the submitted value different?
Desired behavior:
That the displayed value is different from the submitted value.
Screenshot:
Code:
import{useState}from"react";import{CheckIcon,ChevronUpDownIcon}from"@heroicons/react/20/solid";import{Combobox}from"@headlessui/react";constpeople=[{id: 1,name: "Leslie Alexander",value: "user1"},];exportdefaultfunctionHome(){const[query,setQuery]=useState("");const[selectedPerson,setSelectedPerson]=useState(null);constfilteredPeople=query===""
? people
: people.filter((person)=>{returnperson.name.toLowerCase().includes(query.toLowerCase());});consthandleSubmit=(e: React.FormEvent<HTMLFormElement>)=>{e.preventDefault();// Get the form data.constform=e.currentTarget;constdata=newFormData(form);constentries=Object.fromEntries(data.entries());console.log(entries);};return(<formonSubmit={handleSubmit}><Comboboxas="div"value={selectedPerson}onChange={setSelectedPerson}><Combobox.Label>Assigned to</Combobox.Label><div><Combobox.Inputname="user"onChange={(event)=>setQuery(event.target.value)}displayValue={(person: {id: number;name: string})=>person?.name}/><Combobox.Button><ChevronUpDownIconaria-hidden="true"/></Combobox.Button>{filteredPeople.length>0&&(<Combobox.Options>{filteredPeople.map((person)=>(<Combobox.Optionkey={person.id}value={person}>{({ active, selected })=>(<><span>{person.name}</span>{selected&&(<span><CheckIconclassName="h-5 w-5"aria-hidden="true"/></span>)}</>)}</Combobox.Option>))}</Combobox.Options>)}</div></Combobox><buttontype="submit">send</button></form>);}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Issue:
I'm having trouble with the "Combobox" component. I've bound an object to it and set the
displayValue
property to the value I want. However, I'm unable to submit a different value than the display value.When I change the value of the
Combobox.Option
component to the desired value, it only passes that value to thedisplayValue
property, while ignoring the option value onSubmit. Below is a small code example where I want to send thevalue
property instead of thename
property, but when I press send, I keep getting returned thename
property or any value that is inside thedisplayValue
property.This behavior seems weird because the documentation explicitly states that this component can take complex objects, yet it only takes a display value and ignores the rest of the object. Is there a way to make the
displayValue
and the submitted value different?Desired behavior:
That the displayed value is different from the submitted value.
Screenshot:
Code:
Beta Was this translation helpful? Give feedback.
All reactions