-
Hi folks! Running into an issue where setting the inputValue to const selections = [
{label: 'Item 1', value: '1'},
{label: 'Item 2', value: '2'},
{label: 'Item 3', value: '3'},
];
const ControlledCombobox = () => {
const [value, setValue] = React.useState('');
const [items, setItems] = React.useState(selections);
const [selectedItems, setSelectedItems] = React.useState([]);
return (
<>
<Combobox
autocomplete
items={items}
labelText="Make a selection"
itemToString={item => (item ? String(item.label) : null)}
onInputValueChange={({inputValue}) => {
if (inputValue !== undefined) {
setItems(selections.filter(item => {
return item.label.toLowerCase().startsWith(inputValue.toLowerCase())
}));
setValue(inputValue);
}
}}
onSelectedItemChange={changes => {
setSelectedItems([...selectedItems, changes.selectedItem]);
setValue('');
}}
inputValue={value}
/>
<ul>
{selectedItems.map(item => (<li>{item.label}</li>));
</ul>
</>
);
}; |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
Hi @jhdielman, we're taking a look into it and get back to you. I can offer a little reasoning as to why we had to stop allowing Because of the way the combobox works where the selected item can be a string or object, and where the input can act as a filter to the options, it acts differently to a normal HTML element. Whereas a normal HTML element the The fact we allowed Where it gets interesting though is when the visual input value and the selected value stop being independent. When you select an item a combobox is then supposed to visually reflect what was selected, so InputValue and selectedValue become explicitly tied together. You can't clear the But we'll investigate next week |
Beta Was this translation helpful? Give feedback.
-
OK, I have a potential fix for this. The only way to successfully reset both the input value and selected item is for the consumer to use the hook that we use underneath and use the set methods By doing so we'll need to provide a way to pass state to the combobox, much like we do else where in the system like in tabs, for example. I'll try and push a fix up this week |
Beta Was this translation helpful? Give feedback.
OK, I have a potential fix for this. The only way to successfully reset both the input value and selected item is for the consumer to use the hook that we use underneath and use the set methods
selectItem
orreset
.By doing so we'll need to provide a way to pass state to the combobox, much like we do else where in the system like in tabs, for example.
I'll try and push a fix up this week