-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix: Clear Autocomplete virtualFocus upon paste/undo/redo and other focus fixes #8438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
1801b69
5df2049
68b16cc
9725f8d
b61d5b4
8d2446d
db8e26a
965da23
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,12 +10,12 @@ | |
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
import {CLEAR_FOCUS_EVENT, FOCUS_EVENT, focusWithoutScrolling, isCtrlKeyPressed, mergeProps, scrollIntoView, scrollIntoViewport, useEffectEvent, useEvent, useRouter, useUpdateLayoutEffect} from '@react-aria/utils'; | ||
import {CLEAR_FOCUS_EVENT, FOCUS_EVENT, focusWithoutScrolling, getActiveElement, isCtrlKeyPressed, mergeProps, scrollIntoView, scrollIntoViewport, useEffectEvent, useEvent, useRouter, useUpdateLayoutEffect} from '@react-aria/utils'; | ||
import {dispatchVirtualFocus, getFocusableTreeWalker, moveVirtualFocus} from '@react-aria/focus'; | ||
import {DOMAttributes, FocusableElement, FocusStrategy, Key, KeyboardDelegate, RefObject} from '@react-types/shared'; | ||
import {flushSync} from 'react-dom'; | ||
import {FocusEvent, KeyboardEvent, useEffect, useRef} from 'react'; | ||
import {focusSafely, getInteractionModality} from '@react-aria/interactions'; | ||
import {getFocusableTreeWalker, moveVirtualFocus} from '@react-aria/focus'; | ||
import {getItemElement, isNonContiguousSelectionModifier, useCollectionId} from './utils'; | ||
import {MultipleSelectionManager} from '@react-stately/selection'; | ||
import {useLocale} from '@react-aria/i18n'; | ||
|
@@ -407,7 +407,6 @@ export function useSelectableCollection(options: AriaSelectableCollectionOptions | |
let {detail} = e; | ||
e.stopPropagation(); | ||
manager.setFocused(true); | ||
|
||
// If the user is typing forwards, autofocus the first option in the list. | ||
if (detail?.focusStrategy === 'first') { | ||
shouldVirtualFocusFirst.current = true; | ||
|
@@ -419,7 +418,11 @@ export function useSelectableCollection(options: AriaSelectableCollectionOptions | |
|
||
// If no focusable items exist in the list, make sure to clear any activedescendant that may still exist | ||
if (keyToFocus == null) { | ||
let previousActiveElement = getActiveElement(); | ||
// TODO: Bit gross, but we need the first moveVirtualFocus to clear the previous aria-activeDescendant, then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i don't quite follow, what's going on here? and what are the repercussions across other collections? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah sorry, I'll add a more illustrative comment once I confirm this is the only way to do this. What this is handling is the case where the user types into the autocomplete input -> tells the filtered collection to try to virtually focus its first key -> the filtered collection filters itself and ends up in a state where there are no focusable items (aka only disabled items/no matching items at all) -> it needs to then tell the autocomplete input to clear its old aria-activeDescendant and that the input should be focused instead. I was hoping to remove This shouldn't have an repercussions for other standalone collections because it only runs when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's very helpful, thank you for explaining again <3 |
||
// we need to refocus the input element so the focus ring comes back... | ||
moveVirtualFocus(ref.current); | ||
dispatchVirtualFocus(previousActiveElement!, null); | ||
|
||
// If there wasn't a focusable key but the collection had items, then that means we aren't in an intermediate load state and all keys are disabled. | ||
// Reset shouldVirtualFocusFirst so that we don't erronously autofocus an item when the collection is filtered again. | ||
|
LFDanLu marked this conversation as resolved.
Show resolved
Hide resolved
|
Uh oh!
There was an error while loading. Please reload this page.