-
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 all 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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2025 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
import {expect} from '@storybook/jest'; | ||
import {Menu, MenuItem, SearchField} from '../src'; | ||
import type {Meta, StoryObj} from '@storybook/react'; | ||
import {Autocomplete as RACAutocomplete, useFilter} from 'react-aria-components'; | ||
import {style} from '../style/spectrum-theme' with {type: 'macro'}; | ||
import {userEvent, waitFor, within} from '@storybook/testing-library'; | ||
|
||
const meta: Meta<typeof Menu<any>> = { | ||
component: Menu, | ||
parameters: { | ||
chromaticProvider: {colorSchemes: ['light'], backgrounds: ['base'], locales: ['en-US'], disableAnimations: true} | ||
}, | ||
tags: ['autodocs'], | ||
title: 'S2 Chromatic/Autocomplete' | ||
}; | ||
|
||
export default meta; | ||
|
||
function Autocomplete(props) { | ||
let {contains} = useFilter({sensitivity: 'base'}); | ||
return ( | ||
<RACAutocomplete filter={contains} {...props} /> | ||
); | ||
} | ||
|
||
function AutocompleteStory() { | ||
return ( | ||
<Autocomplete> | ||
<SearchField label="Search" styles={style({marginTop: 12, marginX: 12})} /> | ||
<Menu aria-label="test menu" styles={style({marginTop: 12})}> | ||
<MenuItem>Foo</MenuItem> | ||
<MenuItem>Bar</MenuItem> | ||
<MenuItem>Baz</MenuItem> | ||
</Menu> | ||
</Autocomplete> | ||
); | ||
} | ||
|
||
export const CutPaste: StoryObj<typeof AutocompleteStory> = { | ||
render: () => <AutocompleteStory />, | ||
play: async ({canvasElement}) => { | ||
await userEvent.tab(); | ||
await userEvent.keyboard('Foo'); | ||
let body = canvasElement.ownerDocument.body; | ||
let seachfield = await within(body).findByRole('searchbox'); | ||
await waitFor(() => { | ||
expect(seachfield).not.toHaveAttribute('data-focus-visible'); | ||
}, {timeout: 5000}); | ||
|
||
await userEvent.keyboard('{Control>}a{/Control}'); | ||
await userEvent.cut(); | ||
await userEvent.keyboard('{ArrowDown}'); | ||
await waitFor(() => { | ||
expect(seachfield).not.toHaveAttribute('data-focus-visible'); | ||
}, {timeout: 5000}); | ||
|
||
await userEvent.paste(); | ||
await waitFor(() => { | ||
expect(seachfield).toHaveAttribute('data-focus-visible'); | ||
}, {timeout: 5000}); | ||
} | ||
}; |
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> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only has cut/paste, undo and redo didn't seem to fire properly