[Bug]: Combobox - Defaul - Space key clears the combobox input
Library
React Components / v9 (@fluentui/react-components)
System Info
System:
OS: Windows 10 10.0.22621
CPU: (12) x64 Intel(R) Xeon(R) E-2176M CPU @ 2.70GHz
Memory: 12.51 GB / 31.74 GB
Browsers:
Edge: Spartan (44.22621.1105.0), Chromium (108.0.1462.76)
Internet Explorer: 11.0.22621.1
Are you reporting Accessibility issue?
None
Reproduction
https://codesandbox.io/s/7mzpld?file=/example.tsx
Bug Description
Steps:
- Navigate to combobox
- Type letter "w" or any letter which doesn't match any option in list
- press space key
Actual Behavior
it close the list and clear whole input
Expected Behavior
it should not clear what user wrote in input... it sounds buggy... we should investigate what we want to do about list closing, because in this example: https://w3c.github.io/aria-practices/examples/combobox/combobox-autocomplete-list.html they close the list once they don't match the option from the list...
Logs
No response
Requested priority
Normal
Products/sites affected
No response
Are you willing to submit a PR to fix?
no
Validations
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [X] The provided reproduction is a minimal reproducible example of the bug.
In the UX study on combobox, one of the more interesting results was that people using the keyboard and especially using it with screen readers overwhelmingly expected space to select rather than type, to the extent that the reverse behavior presented an issue. This was true even in freeform cases where the value was expected to contain a string (specifically for a combobox that had names, both first and last). That's why the default behavior is to select with space.
I did actually create a PR to add a prop to override that behavior (https://github.com/microsoft/fluentui/pull/25914), but in chatting with the team, decided to hold off until we got a better feel for the use cases and whether they would be better served by the prop or by recomposition. As a reference, recomposing to allow space would look essentially like this:
const useCustomCombobox = (props, ref) => {
const state = useCombobox_unstable(props, ref);
state.input.onKeyDown = React.useCallback((ev) => {
if (ev.key === ' ') return;
state.input.onKeyDown(ev);
});
return state;
};
const CustomCombobox = React.forwardRef((props, ref) => {
const state = useCustomCombobox(props, ref);
const contextValues = useComboboxContextValues(state);
useComboboxStyles_unstable(state);
return renderCombobox_unstable(state, contextValues);
});
I'm definitely still looking at whether we should add the allowSpaceAsCharacter out of the box, and specific use cases would help a lot to determine whether that's the right prop and approach to take.
In freeform, this does not make any sense. free form should allow a user to type whatever they want in the textbox. Not allowing a space is ridiculous in this case.
In the UX study on combobox, one of the more interesting results was that people using the keyboard and especially using it with screen readers overwhelmingly expected space to select rather than type, to the extent that the reverse behavior presented an issue.
@smhigley Wondering how it works for search.... For example, in Teams, I would like to search for "Sarah" and there multiple persons with this name. Without being able to type a surname it might be impossible to select a proper person 🙃
Another example is Google search:
I can't imagine that I will not be able to use a space key there.
Adding Partber Ask as this also needs to be clarified before Teams starts consuming Combobox.
I understand why users might have liked the possibility to use space to confirm the option. However, it would have been to double check with them, what would be the expectation if any of the options had a space in the name.
In reality, most of the times combobox options come from translations or user data. The developer might not have sufficient information to make a decision if the proposed allowSpaceAsCharacter should or should not be enabled - it might not be necessary for some languages, but might need to be used for other languages.
I just want to add an additional use case. We're building an address autocomplete and ran into this problem. It's impossible to get an accurate match without the use of "space" characters.
If the fluentui team doesn't want to change the default behavior then this is fine for me, but I would really hope to at least get the option to be able to use the alternate behavior which allows entry of spaces.
FYI, there is a RFC related to this problem, https://github.com/microsoft/fluentui/pull/27700
@layershifter thanks for providing the link
in the meantime if anyone needs this ASAP this is the patch I'm using for now:
diff --git a/node_modules/@fluentui/react-combobox/lib-commonjs/utils/dropdownKeyActions.js b/node_modules/@fluentui/react-combobox/lib-commonjs/utils/dropdownKeyActions.js
index 7737ed5..f66a74b 100644
--- a/node_modules/@fluentui/react-combobox/lib-commonjs/utils/dropdownKeyActions.js
+++ b/node_modules/@fluentui/react-combobox/lib-commonjs/utils/dropdownKeyActions.js
@@ -32,7 +32,7 @@ function getDropdownActionFromKey(e, options = {}) {
return 'None';
}
// select or close actions
- if (code === _keyboardKeys.ArrowUp && altKey || code === _keyboardKeys.Enter || !multiselect && code === _keyboardKeys.Space) {
+ if (code === _keyboardKeys.ArrowUp && altKey || code === _keyboardKeys.Enter) {
return 'CloseSelect';
}
if (multiselect && code === _keyboardKeys.Space) {
diff --git a/node_modules/@fluentui/react-combobox/lib/utils/dropdownKeyActions.js b/node_modules/@fluentui/react-combobox/lib/utils/dropdownKeyActions.js
index 3278839..ef862ea 100644
--- a/node_modules/@fluentui/react-combobox/lib/utils/dropdownKeyActions.js
+++ b/node_modules/@fluentui/react-combobox/lib/utils/dropdownKeyActions.js
@@ -28,7 +28,7 @@ export function getDropdownActionFromKey(e, options = {}) {
return 'None';
}
// select or close actions
- if (code === keys.ArrowUp && altKey || code === keys.Enter || !multiselect && code === keys.Space) {
+ if (code === keys.ArrowUp && altKey || code === keys.Enter) {
return 'CloseSelect';
}
if (multiselect && code === keys.Space) {
This bug has been open for 7 months already, is there any plan to get it fixed anytime soon? It looks like there is no momentum on this or the RFC. We've got work pending on it and we'd rather not need to run a package patch if possible
pinging as this is blocking me as well from using the combobox properly.