Skip to content
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

getAutocompleterUI: don't redefine ListBox component on every render #61877

Merged
merged 4 commits into from
May 23, 2024
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
4 changes: 4 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- `ComboboxControl`: Introduce Combobox expandOnFocus prop ([#61705](https://github.com/WordPress/gutenberg/pull/61705)).

### Bug Fixes

- `Autocomplete`: Stabilize rendering of autocomplete items ([#61877](https://github.com/WordPress/gutenberg/pull/61877)).

### Internal

- Add type support for CSS Custom Properties ([#61872](https://github.com/WordPress/gutenberg/pull/61872)).
Expand Down
106 changes: 68 additions & 38 deletions packages/components/src/autocomplete/autocompleter-ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,57 @@ import { VisuallyHidden } from '../visually-hidden';
import { createPortal } from 'react-dom';
import type { AutocompleterUIProps, KeyedOption, WPCompleter } from './types';

type ListBoxProps = {
items: KeyedOption[];
onSelect: ( option: KeyedOption ) => void;
selectedIndex: number;
instanceId: number;
listBoxId: string | undefined;
className?: string;
Component?: React.ElementType;
};

function ListBox( {
items,
onSelect,
selectedIndex,
instanceId,
listBoxId,
className,
Component = 'div',
}: ListBoxProps ) {
return (
<Component
id={ listBoxId }
role="listbox"
className="components-autocomplete__results"
>
{ items.map( ( option, index ) => (
<Button
key={ option.key }
id={ `components-autocomplete-item-${ instanceId }-${ option.key }` }
role="option"
aria-selected={ index === selectedIndex }
disabled={ option.isDisabled }
className={ clsx(
'components-autocomplete__result',
className,
{
'is-selected': index === selectedIndex,
}
) }
onClick={ () => onSelect( option ) }
>
{ option.label }
</Button>
) ) }
</Component>
);
}

export function getAutoCompleterUI( autocompleter: WPCompleter ) {
const useItems = autocompleter.useItems
? autocompleter.useItems
: getDefaultUseItems( autocompleter );
const useItems =
autocompleter.useItems ?? getDefaultUseItems( autocompleter );

function AutocompleterUI( {
filterValue,
Expand Down Expand Up @@ -62,7 +109,7 @@ export function getAutoCompleterUI( autocompleter: WPCompleter ) {
// If the popover is rendered in a different document than
// the content, we need to duplicate the options list in the
// content document so that it's available to the screen
// readers, which check the DOM ID based aira-* attributes.
// readers, which check the DOM ID based aria-* attributes.
setNeedsA11yCompat(
node.ownerDocument !== contentRef.current.ownerDocument
);
Expand Down Expand Up @@ -124,38 +171,6 @@ export function getAutoCompleterUI( autocompleter: WPCompleter ) {
return null;
}

const ListBox = ( {
Component = 'div',
}: {
Component?: React.ElementType;
} ) => (
<Component
id={ listBoxId }
role="listbox"
className="components-autocomplete__results"
>
{ items.map( ( option, index ) => (
<Button
key={ option.key }
id={ `components-autocomplete-item-${ instanceId }-${ option.key }` }
role="option"
aria-selected={ index === selectedIndex }
disabled={ option.isDisabled }
className={ clsx(
'components-autocomplete__result',
className,
{
'is-selected': index === selectedIndex,
}
) }
onClick={ () => onSelect( option ) }
>
{ option.label }
</Button>
) ) }
</Component>
);

return (
<>
<Popover
Expand All @@ -166,12 +181,27 @@ export function getAutoCompleterUI( autocompleter: WPCompleter ) {
anchor={ popoverAnchor }
ref={ popoverRefs }
>
<ListBox />
<ListBox
items={ items }
onSelect={ onSelect }
selectedIndex={ selectedIndex }
instanceId={ instanceId }
listBoxId={ listBoxId }
className={ className }
/>
</Popover>
{ contentRef.current &&
needsA11yCompat &&
createPortal(
<ListBox Component={ VisuallyHidden } />,
<ListBox
items={ items }
onSelect={ onSelect }
selectedIndex={ selectedIndex }
instanceId={ instanceId }
listBoxId={ listBoxId }
className={ className }
Component={ VisuallyHidden }
/>,
contentRef.current.ownerDocument.body
) }
</>
Expand Down
37 changes: 37 additions & 0 deletions test/e2e/specs/editor/various/autocomplete-and-mentions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,43 @@ test.describe( 'Autocomplete (@firefox, @webkit)', () => {
} );
} );

test( `should insert mention in a table block`, async ( {
page,
editor,
} ) => {
// Insert table block.
await editor.insertBlock( { name: 'core/table' } );

// Create the table.
await editor.canvas
.locator( 'role=button[name="Create Table"i]' )
.click();

// Select the first cell.
await editor.canvas
.locator( 'role=textbox[name="Body cell text"i] >> nth=0' )
.click();

// Type autocomplete text.
await page.keyboard.type( '@j' );

// Verify that option is selected.
const selectedOption = page.getByRole( 'option', {
name: 'Jane Doe',
selected: true,
} );
await expect( selectedOption ).toBeVisible();

// Insert the option.
await selectedOption.click();

// Verify it's been inserted.
const snapshot = `<!-- wp:table -->
<figure class="wp-block-table"><table class="has-fixed-layout"><tbody><tr><td>@testuser</td><td></td></tr><tr><td></td><td></td></tr></tbody></table></figure>
<!-- /wp:table -->`;
await expect.poll( editor.getEditedPostContent ).toBe( snapshot );
} );

// The following test concerns an infinite loop regression (https://github.com/WordPress/gutenberg/issues/41709).
// When present, the regression will cause this test to time out.
test( 'should insert elements from multiple completers in a single block', async ( {
Expand Down
Loading