Skip to content

Commit a214902

Browse files
committed
clearing focused key on input change and removing autofocus behavior on menu open/filter
fixes nvda not announcing what letter is deleted when backspacing
1 parent 83733cc commit a214902

File tree

3 files changed

+80
-133
lines changed

3 files changed

+80
-133
lines changed

packages/@react-aria/combobox/src/useComboBox.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export function useComboBox<T>(props: AriaComboBoxProps<T>, state: ComboBoxState
5959

6060
let onChange = (val) => {
6161
state.setInputValue(val);
62+
state.selectionManager.setFocusedKey(null);
6263

6364
if (menuTrigger !== 'manual') {
6465
state.open();
@@ -192,23 +193,11 @@ export function useComboBox<T>(props: AriaComboBoxProps<T>, state: ComboBoxState
192193

193194
let lastValue = useRef(state.inputValue);
194195
useEffect(() => {
196+
// Close combobox menu if user clears input text unless trigger is focus (as per design)
195197
if (lastValue.current !== state.inputValue) {
196-
// Clear focused key if user clears textfield to prevent accidental selection on blur and menuTrigger='focus' (as per design feedback)
197-
// Also clear focused key if allowsCustomValue is true, there isn't a selected key, and the input value changed
198-
// Specifically for case where menu is closed and user copy pastes a matching value into input field then deletes a character
199-
if ((state.inputValue === '' && menuTrigger === 'focus') || (allowsCustomValue && !state.selectedKey)) {
200-
state.selectionManager.setFocusedKey(null);
201-
}
202-
203198
if (state.inputValue === '' && menuTrigger !== 'focus') {
204199
state.close();
205200
}
206-
207-
// Focus first item if filtered collection no longer contains original focused item (aka user typing to filter collection)
208-
// Only set a focused key if one existed previously, don't want to focus something by default if allowsCustomValue = true
209-
if ((!allowsCustomValue || state.selectionManager.focusedKey) && state.inputValue !== '' && !state.collection.getItem(state.selectionManager.focusedKey)) {
210-
state.selectionManager.setFocusedKey(layout.getFirstKey());
211-
}
212201
}
213202

214203
lastValue.current = state.inputValue;

packages/@react-spectrum/combobox/src/ComboBox.tsx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,8 @@ function ComboBox<T extends object>(props: SpectrumComboBoxProps<T>, ref: RefObj
8888

8989
let comboBoxAutoFocus;
9090
// Focus first/last item on menu open if focusStategy is set (done by up/down arrows)
91-
// Otherwise if allowsCustomValue is true, only autofocus if there is a selected item
92-
// If allowsCustomValue is false, autofocus first item/selectedItem
9391
if (state.focusStrategy) {
9492
comboBoxAutoFocus = state.focusStrategy;
95-
} else if (props.allowsCustomValue) {
96-
if (state.selectedKey) {
97-
comboBoxAutoFocus = true;
98-
}
99-
} else {
100-
comboBoxAutoFocus = 'first';
10193
}
10294

10395
let listbox = (

0 commit comments

Comments
 (0)