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

Feature : Refactor Autocomplete using DropdownMenu #66192

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
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
52 changes: 30 additions & 22 deletions packages/components/src/autocomplete/autocompleter-ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import { __, _n, sprintf } from '@wordpress/i18n';
*/
import getDefaultUseItems from './get-default-use-items';
import Button from '../button';
import Popover from '../popover';
import { VisuallyHidden } from '../visually-hidden';
import { createPortal } from 'react-dom';
import type { AutocompleterUIProps, KeyedOption, WPCompleter } from './types';
import DropdownMenu from '../dropdown-menu';

type ListBoxProps = {
type DropdownItemsProps = {
items: KeyedOption[];
onSelect: ( option: KeyedOption ) => void;
selectedIndex: number;
Expand All @@ -37,19 +37,18 @@ type ListBoxProps = {
Component?: React.ElementType;
};

function ListBox( {
function DropdownItems( {
items,
onSelect,
selectedIndex,
instanceId,
listBoxId,
className,
Component = 'div',
}: ListBoxProps ) {
}: DropdownItemsProps ) {
return (
<Component
id={ listBoxId }
role="listbox"
className="components-autocomplete__results"
>
{ items.map( ( option, index ) => (
Expand Down Expand Up @@ -175,28 +174,37 @@ export function getAutoCompleterUI( autocompleter: WPCompleter ) {
}

return (
<>
<Popover
focusOnMount={ false }
onClose={ onReset }
placement="top-start"
<div>
<DropdownMenu
label={ __( 'Select a block.' ) }
defaultOpen
icon={ <></> }
className="components-autocomplete__popover"
anchor={ popoverAnchor }
ref={ popoverRefs }
popoverProps={ {
anchor: popoverAnchor,
onClose: onReset,
} }
onClose={ onReset }
focusOnMount={ false }
>
<ListBox
items={ items }
onSelect={ onSelect }
selectedIndex={ selectedIndex }
instanceId={ instanceId }
listBoxId={ listBoxId }
className={ className }
/>
</Popover>
{ () => (
<>
<DropdownItems
items={ items }
onSelect={ onSelect }
selectedIndex={ selectedIndex }
instanceId={ instanceId }
listBoxId={ listBoxId }
className={ className }
/>
</>
) }
</DropdownMenu>
{ contentRef.current &&
needsA11yCompat &&
createPortal(
<ListBox
<DropdownItems
items={ items }
onSelect={ onSelect }
selectedIndex={ selectedIndex }
Expand All @@ -207,7 +215,7 @@ export function getAutoCompleterUI( autocompleter: WPCompleter ) {
/>,
contentRef.current.ownerDocument.body
) }
</>
</div>
);
}

Expand Down
10 changes: 7 additions & 3 deletions packages/components/src/autocomplete/style.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
.components-autocomplete__popover .components-popover__content {
padding: $grid-unit-10;
min-width: 200px;
.components-autocomplete__popover {
height: 0;

.components-popover__content {
padding: $grid-unit-10;
min-width: 200px;
}
}

.components-autocomplete__result.components-button {
Expand Down
3 changes: 2 additions & 1 deletion packages/components/src/dropdown-menu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function UnconnectedDropdownMenu( dropdownMenuProps: DropdownMenuProps ) {
disableOpenOnArrowDown = false,
text,
noIcons,

focusOnMount,
open,
defaultOpen,
onToggle: onToggleProp,
Expand Down Expand Up @@ -212,6 +212,7 @@ function UnconnectedDropdownMenu( dropdownMenuProps: DropdownMenuProps ) {
open={ open }
defaultOpen={ defaultOpen }
onToggle={ onToggleProp }
focusOnMount={ focusOnMount }
/>
);
}
Expand Down
6 changes: 6 additions & 0 deletions packages/components/src/dropdown-menu/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ export type DropdownMenuProps = {
* A valid DropdownMenu must specify a `controls` or `children` prop, or both.
*/
controls?: DropdownOption[] | DropdownOption[][];
/**
* Prop to control whether the dropdown menu should focus on mount.
*
* @default true
*/
focusOnMount?: boolean;
/**
* The controlled open state of the dropdown menu.
* Must be used in conjunction with `onToggle`.
Expand Down
Loading