-
Hello, I use the combobox component with React inside a form and I control the input with the “Controller” component of react-hook-form. This is what my data looks like: const data = [
{
label: "Open Street Map",
value: LayerTypeEnum.OSM.toString(),
},
{
label: "Vecteur",
value: LayerTypeEnum.VECTOR.toString(),
}
] I retrieve its data in a react state so that I can update my list when I use the input: const [items, setItems] = React.useState(data);
function handleInputChange(e: Combobox.InputValueChangeDetails) {
const filtered = data.filter((item) =>
item.label.toLowerCase().includes(e.inputValue.toLowerCase()),
);
setItems(filtered.length > 0 ? filtered : data);
} Next, I use the combobox inside a react-hook-form Controller component: <Controller
control={form.control}
name={`layers.${layerIndex}.type`}
render={({ field }) => (
//...
)}
/> <Combobox.Root
openOnClick
closeOnSelect
items={items}
value={[field.value.toString()]}
defaultValue={[field.value.toString()]}
onInputValueChange={handleInputChange}
inputBehavior="autocomplete"
onValueChange={async (details) => {
const selectedValue = details.value[0];
if (!selectedValue) return;
const layerType = Number.parseInt(selectedValue); //I need a number value
const options = {
//...
};
//await things...
form.setValue(`layers.${layerIndex}`, options); //The layer type is updated here
}}
>
//...
<Combobox.Content>
{items.map((item) => (
<Combobox.Item key={item.value} item={item}>
<Combobox.ItemText>{item.label}</Combobox.ItemText>
<Combobox.ItemIndicator>
<CheckIcon />
</Combobox.ItemIndicator>
</Combobox.Item>
))}
</Combobox.Content>
</Combobox.Root> It worked fine until recently, I wonder if I made a change by mistake that broke the component, or maybe I'm forgetting something, I don't know ark well enough yet. If anyone sees the problem I'm interested, thank you. :) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
My bad I just added this fix: onOpenChange={() =>
//...
} |
Beta Was this translation helpful? Give feedback.
My bad I just added this fix: