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
Is there a way to extend the way the keyboard filtering works when a Listbox is open, to take more than the label shown for each option into account? This would be especially useful when binding objects as values.
Let’s say we have a Listbox where the user is supposed to pick their phone country code. The data looks like this:
constcountries=[{code: 'AR',label: 'Argentina (+54)',prefix: '54',value: 'argentina',},{code: 'BR',label: 'Brazil (+55)',prefix: '55',value: 'brazil',},{code: 'IM',label: 'Isle of Man (+44-1624)',prefix: '44-1624',value: 'isleOfMan',},// and so on…];
…and consider that the actual list is alot longer. The component that handles it all could look something like this:
constformatBoxLabel=(value)=>`(+${value.prefix})`;exportconstFoo=()=>{const[selected,setSelected]=React.useState('');constselectedOption=selected ? formatBoxLabel(selected) : 'Please select a country';return(<Listboxvalue={selected}onChange={setSelected}><Listbox.Button>{selectedOption}</Listbox.Button><Listbox.Options>{countries.map(country=>(<Listbox.Optionkey={country.code}value={country}>{country.label}</Listbox.Option>))}</Listbox.Options></Listbox>)}
The idea being that each option would be shown like Argentina (+54) Brazil (+55) Isle of Man (+44-1624)
…but the selected value would only contain the actual country code: (+55) (for brevity).
Now for the final criteria: since people think about filtering numbers differently, it’d be nice if typing either bror55 would focus the option for Brazil. But because of how it works right now, only br will work – typing 55 won’t do anything, since it does not match the start of any of the labels.
Ideally it’d be nice if there was some way of telling Listbox to look at some of the keys for each value (other than what’s printed in the label). Something like ”Hey Listbox. When using the keyboard to find an option to focus , consider the code, prefix and value keys for each option, and pick whatever you find first”. But I don’t think there is anything like that at all right?
I guess this might be doable with ref’s and eventlisteners in some way, but it feels like it’d become messy fast. But perhaps its considered a bit too niche to be included?
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.
-
Is there a way to extend the way the keyboard filtering works when a
Listbox
is open, to take more than the label shown for each option into account? This would be especially useful when binding objects as values.Let’s say we have a Listbox where the user is supposed to pick their phone country code. The data looks like this:
…and consider that the actual list is alot longer. The component that handles it all could look something like this:
The idea being that each option would be shown like
Argentina (+54)
Brazil (+55)
Isle of Man (+44-1624)
…but the selected value would only contain the actual country code:
(+55)
(for brevity).Now for the final criteria: since people think about filtering numbers differently, it’d be nice if typing either
br
or55
would focus the option for Brazil. But because of how it works right now, onlybr
will work – typing55
won’t do anything, since it does not match the start of any of the labels.Ideally it’d be nice if there was some way of telling
Listbox
to look at some of the keys for each value (other than what’s printed in the label). Something like ”Hey Listbox. When using the keyboard to find an option to focus , consider thecode
,prefix
andvalue
keys for each option, and pick whatever you find first”. But I don’t think there is anything like that at all right?I guess this might be doable with ref’s and eventlisteners in some way, but it feels like it’d become messy fast. But perhaps its considered a bit too niche to be included?
Beta Was this translation helpful? Give feedback.
All reactions