Investigate how Select Menu should select the first matching item when the user types on the keyboard
Bugs
What MDC-Web Version are you using?
Whichever version this demo is using: https://material-components-web.appspot.com/select.html
What browser(s) is this bug affecting?
Firefox, Chrome, IE11, Edge. As up to date as possible, as of this issue.
What OS are you using?
Windows 10 64-bit.
What are the steps to reproduce the bug?
- Go to https://material-components-web.appspot.com/select.html
- Tab-focus the first select menu, "Pick a Food Group"
- Press the "F" key on your keyboard
- Observe the first select item starting with "F" is not selected
- Open the select menu by clicking it or by pressing the down arrow or spacebar keys on your keyboard
- Press the "F" key on your keyboard
- Observe the first select item starting with "F" is not selected
What is the expected behavior?
The first select item starting with "F" should be selected, which in this case is "Fruit". Pushing the "F" key again very quickly after the first should cause the second select item starting with "F" to be selected, which in this case is "Fats, Oils, and Sweets". Constantly pushing the "F" key should cycle through all options in order as they appear. Additionally, typing "F" and then "R" right after one another, should bring up the first select item starting with "FR".
This should work when the menu is closed or open as long as it has focus.
What is the actual behavior?
Nothing happens when the select menu has focus and typing occurs. The menu does not react in any way and the selected item does not change.
Any other information you believe would be useful?
If you know what items are in the drop down, you should be able to type it out until it becomes the selected item. If there is only one item that starts with an "F", then you can tab into the select menu, press "F" and then tab to the next form element.
This is the default select menu behavior in all the browsers I mentioned above and can be tested on the same page using the "CSS Only" version of the select menu.
Some common examples of known select items would be:
- Month, day, and year for birthdays or other dates
- Credit card information with month and year
- Countries
- US states and their abbreviations, Canadian provinces and their abbreviations, etc.
- A quantity for the number of product that you want when ordering something
- Shoe or other clothing sizes
- Day of the week and their abbreviations
Thanks for filing this issue!
I agree 100% that we should be doing this.
Personally, I use this feature constantly, and it drives me crazy when websites use some pretty-looking <select> library that breaks native OS behaviors by not implementing them at all.
More importantly, though, it's a significant UX and accessibility problem. We should definitely fix it.
Here is a rough ES6 class based approach that you can implement/convert as a solution in the interim.
this.query = "";
this.mdlComponent.getDefaultFoundation().adapter_.registerMenuInteractionHandler("keypress", event => {
function executeQuery(query) {
const regex = new RegExp(`^${query.toLowerCase()}`);
const index = context.field.values.findIndex(x => x.value.toLowerCase().match(regex));
if (Number.isSafeInteger(index) && index >= 0) {
context.select(index);
return true;
}
return false;
}
this.query = this.query.concat(event.key);
const success = executeQuery(this.query);
if (!success) {
this.query = event.key;
executeQuery(this.query);
}
});
After talking with design, both sides agreed that always wrapping the native HTML select element is preferable, not only for the features above, but also for e.g. automatically getting an appropriate UI on mobile devices. We recently merged #2462 for this.
Has the reversion to a <select> element been changed?
Current example of material select - https://material-components.github.io/material-components-web-catalog/#/component/select
does not select an option when typing.
Ive come to a project that, though using the latest release, has <select-field> elements instead of <select> elements.