-
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 5 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
LFDanLu marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -427,6 +427,97 @@ describe('Autocomplete', () => { | |
expect(input).not.toHaveAttribute('data-focused'); | ||
}); | ||
|
||
it('should restore focus visible styles back to the input when typing forward results in only disabled items', async function () { | ||
let {getByRole} = render( | ||
<AutocompleteWrapper> | ||
<StaticMenu disabledKeys={['2']} /> | ||
</AutocompleteWrapper> | ||
); | ||
|
||
let input = getByRole('searchbox'); | ||
await user.tab(); | ||
expect(document.activeElement).toBe(input); | ||
expect(input).toHaveAttribute('data-focused'); | ||
expect(input).toHaveAttribute('data-focus-visible'); | ||
|
||
await user.keyboard('Ba'); | ||
act(() => jest.runAllTimers()); | ||
let menu = getByRole('menu'); | ||
let options = within(menu).getAllByRole('menuitem'); | ||
let baz = options[1]; | ||
expect(baz).toHaveTextContent('Baz'); | ||
expect(input).toHaveAttribute('aria-activedescendant', baz.id); | ||
expect(baz).toHaveAttribute('data-focus-visible'); | ||
expect(input).not.toHaveAttribute('data-focused'); | ||
expect(input).not.toHaveAttribute('data-focus-visible'); | ||
|
||
await user.keyboard('r'); | ||
act(() => jest.runAllTimers()); | ||
options = within(menu).getAllByRole('menuitem'); | ||
let bar = options[0]; | ||
expect(bar).toHaveTextContent('Bar'); | ||
expect(input).not.toHaveAttribute('aria-activedescendant'); | ||
expect(bar).not.toHaveAttribute('data-focus-visible'); | ||
expect(input).toHaveAttribute('data-focused'); | ||
expect(input).toHaveAttribute('data-focus-visible'); | ||
}); | ||
|
||
it('should maintain focus styles on the input if typing forward results in an completely empty collection', async function () { | ||
let {getByRole} = render( | ||
<AutocompleteWrapper> | ||
<StaticMenu /> | ||
</AutocompleteWrapper> | ||
); | ||
|
||
let input = getByRole('searchbox'); | ||
await user.tab(); | ||
expect(document.activeElement).toBe(input); | ||
expect(input).toHaveAttribute('data-focused'); | ||
expect(input).toHaveAttribute('data-focus-visible'); | ||
|
||
await user.keyboard('Q'); | ||
act(() => jest.runAllTimers()); | ||
let menu = getByRole('menu'); | ||
let options = within(menu).queryAllByRole('menuitem'); | ||
expect(options).toHaveLength(0); | ||
expect(input).toHaveAttribute('data-focused'); | ||
expect(input).toHaveAttribute('data-focus-visible'); | ||
expect(input).not.toHaveAttribute('aria-activedescendant'); | ||
}); | ||
|
||
it('should restore focus visible styles back to the input if the user types forward and backspaces in quick succession', async function () { | ||
let {getByRole} = render( | ||
<AutocompleteWrapper> | ||
<StaticMenu /> | ||
</AutocompleteWrapper> | ||
); | ||
|
||
let input = getByRole('searchbox'); | ||
await user.tab(); | ||
expect(document.activeElement).toBe(input); | ||
expect(input).toHaveAttribute('data-focused'); | ||
expect(input).toHaveAttribute('data-focus-visible'); | ||
|
||
await user.keyboard('F'); | ||
// If 500ms hasn't elapsed the aria-activedecendant hasn't been updated | ||
act(() => jest.advanceTimersByTime(300)); | ||
Comment on lines
+502
to
+503
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. 500 v 300? 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. yep, if I advance more than 500ms, the |
||
let menu = getByRole('menu'); | ||
let options = within(menu).getAllByRole('menuitem'); | ||
let foo = options[0]; | ||
expect(foo).toHaveTextContent('Foo'); | ||
expect(input).not.toHaveAttribute('aria-activedescendant'); | ||
expect(foo).toHaveAttribute('data-focus-visible'); | ||
expect(input).not.toHaveAttribute('data-focused'); | ||
expect(input).not.toHaveAttribute('data-focus-visible'); | ||
|
||
await user.keyboard('{Backspace}'); | ||
act(() => jest.runAllTimers()); | ||
expect(input).toHaveAttribute('data-focused'); | ||
expect(input).toHaveAttribute('data-focus-visible'); | ||
expect(input).not.toHaveAttribute('aria-activedescendant'); | ||
expect(foo).not.toHaveAttribute('data-focus-visible'); | ||
}); | ||
|
||
it('should work inside a Select', async function () { | ||
let {getByRole} = render( | ||
<Select> | ||
|
Uh oh!
There was an error while loading. Please reload this page.