Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/dataviews/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Bug Fixes

- DataForm: Fix label color of array control. [#75730](https://github.com/WordPress/gutenberg/pull/75730)
- DataViews: Fix focus transfer while searching in `list` layout. [#75999](https://github.com/WordPress/gutenberg/pull/75999)
- DataForm: Fix focus loss when collapsing in Card view. [#75689](https://github.com/WordPress/gutenberg/pull/75689)
- DataViews: Avoid flickering while refreshing. [#74572](https://github.com/WordPress/gutenberg/pull/74572)
- DataViews: Fix spacing in first column. [#75693](https://github.com/WordPress/gutenberg/pull/75693)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,8 @@ export default function ViewList< Item >( props: ViewListProps< Item > ) {
string | null | undefined
>( undefined );

const compositeRef = useRef< HTMLDivElement >( null );

// Update the active composite item when the selected item changes.
useEffect( () => {
if ( selectedItem ) {
Expand Down Expand Up @@ -465,7 +467,17 @@ export default function ViewList< Item >( props: ViewListProps< Item > ) {
const targetCompositeItemId = generateCompositeId( itemIdPrefix );

setActiveCompositeId( targetCompositeItemId );
document.getElementById( targetCompositeItemId )?.focus();
// The active composite item is controlled state that
// can update without needing a focus move (e.g., searching
// can trigger an active ID update). Only move DOM focus
// when it's already within the list.
if (
compositeRef.current?.contains(
compositeRef.current.ownerDocument.activeElement
)
) {
document.getElementById( targetCompositeItemId )?.focus();
}
Comment thread
ntsekouras marked this conversation as resolved.
Resolved
},
[ data, generateCompositeItemIdPrefix ]
);
Expand Down Expand Up @@ -536,6 +548,7 @@ export default function ViewList< Item >( props: ViewListProps< Item > ) {
if ( hasData && groupField && dataByGroup ) {
return (
<Composite
ref={ compositeRef }
id={ `${ baseId }` }
render={ <div /> }
className="dataviews-view-list__group"
Expand Down Expand Up @@ -601,6 +614,7 @@ export default function ViewList< Item >( props: ViewListProps< Item > ) {
return (
<>
<Composite
ref={ compositeRef }
id={ baseId }
render={ <div /> }
className={ clsx( 'dataviews-view-list', className, {
Expand Down
28 changes: 28 additions & 0 deletions test/e2e/specs/site-editor/dataviews-list-layout-keyboard.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,34 @@ test.describe( 'Dataviews List Layout', () => {
await expect( page.getByLabel( 'Privacy Policy' ) ).toBeFocused();
} );

test( 'Search input retains focus while typing', async ( { page } ) => {
const searchBox = page.getByRole( 'searchbox', { name: 'Search' } );
const grid = page.getByRole( 'grid' );

// Wait for Ariakit to auto-activate the first composite item.
await expect( grid.locator( '[data-active-item]' ) ).toBeVisible();

// Determine which item is active so we can search for the other one,
// forcing the active item to be filtered out of the list.
const activeRow = grid.locator(
'[role="row"]:has([data-active-item])'
);
const activeRowText = await activeRow.textContent();
const searchTerm = activeRowText.includes( 'Privacy' )
? 'Sample'
: 'Privacy';

// Type a query that filters out the auto-activated item.
await searchBox.click();
await searchBox.fill( searchTerm );

// Wait for the debounced search to filter the list.
await expect( grid.getByRole( 'row' ) ).toHaveCount( 1 );

// Focus should still be on the search input, not stolen by the list.
await expect( searchBox ).toBeFocused();
} );

test( 'Navigates the list via UP/DOWN arrow keys from action buttons', async ( {
page,
} ) => {
Expand Down
Loading