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
4 changes: 4 additions & 0 deletions packages/ui/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
- `Select`: `Select.Trigger` now renders a default `"Select"` placeholder when no value is selected, where it previously rendered empty ([#78076](https://github.com/WordPress/gutenberg/pull/78076)).
- `Select`: `Select.Item` no longer renders its `value` as fallback item content. Pass item content explicitly as `children`. Migrate `<Select.Item value="Foo" />` to `<Select.Item value="Foo">Foo</Select.Item>` ([#77861](https://github.com/WordPress/gutenberg/pull/77861)).

### New Features

- Add `SelectControl` component ([#77809](https://github.com/WordPress/gutenberg/pull/77809)).

### Bug Fixes

- `Text`: Provide CSS defense values for every variant when rendered as either a paragraph or heading element ([#78172](https://github.com/WordPress/gutenberg/pull/78172)).
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/form/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './primitives';

export * from './input-control';
export * from './select-control';
9 changes: 9 additions & 0 deletions packages/ui/src/form/select-control/context.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createContext, useContext } from '@wordpress/element';
import type { SelectTriggerProps } from '../primitives/select/types';

export const SelectControlSizeContext =
createContext< SelectTriggerProps[ 'size' ] >( undefined );

export const useSelectControlSizeContext = () => {
return useContext( SelectControlSizeContext );
};
14 changes: 14 additions & 0 deletions packages/ui/src/form/select-control/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { SelectControl as _SelectControl } from './select-control';
import { Item } from './item';

Item.displayName = 'SelectControl.Item';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do the same for _SelectControl?

Suggested change
Item.displayName = 'SelectControl.Item';
_SelectControl.displayName = 'SelectControl';
Item.displayName = 'SelectControl.Item';

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Display name is working here without an explicit definition.


/**
* A complete select field with integrated label and description.
*/
export const SelectControl = Object.assign( _SelectControl, {
/**
* An item rendered inside a `SelectControl` popup.
*/
Item,
} );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we align this implementation with Button especially when it comes to the type assignment?

/**
* A versatile button component with multiple variants, tones, and sizes.
* Built on design tokens for consistent theming and accessibility.
*/
export const Button = Object.assign( ButtonButton, {
/**
* An icon component specifically designed to work well when rendered inside
* a `Button` component.
*/
Icon: ButtonIcon,
} ) as typeof ButtonButton & {
Icon: typeof ButtonIcon;
};

Whichever we choose (explicit type casting or implicit), we should use it consistently. Possible, we could also document the difference between "real" compound components (using barrel export) vs higher level components like SelectControl and Button (using Object.assign) in the package's Guidelines?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would prefer inference unless necessary, since casting can mask mistakes.

13 changes: 13 additions & 0 deletions packages/ui/src/form/select-control/item.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { forwardRef } from '@wordpress/element';
import { Select } from '../primitives';
import { useSelectControlSizeContext } from './context';
import type { SelectItemProps } from '../primitives/select/types';

export const Item = forwardRef< HTMLDivElement, SelectItemProps >(
function Item( { size: sizeProp, ...restProps }, ref ) {
const contextSize = useSelectControlSizeContext();
const size = sizeProp ?? contextSize;

return <Select.Item size={ size } ref={ ref } { ...restProps } />;
}
);
65 changes: 65 additions & 0 deletions packages/ui/src/form/select-control/select-control.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { forwardRef } from '@wordpress/element';
import { Field, Select } from '../primitives';
import { SelectControlSizeContext } from './context';
import { Item } from './item';
import type { SelectControlProps } from './types';

/**
* A complete select field with integrated label and description.
*/
export const SelectControl = forwardRef<
Comment on lines +7 to +10

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the JSDoc (for SelectControl and Item) be added in the Object.assign call in index.ts ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One unfortunate thing I noticed is that Storybook only picks up the JSDoc description on the forwardRef call, and VS Code only picks up the one on Object.assign 😅 So this might mean we need to duplicate the description for this particular export shape.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunate, but I guess necessary

HTMLButtonElement,
SelectControlProps
>( function SelectControl(
{
className,
children,
items,
label,
description,
details,
hideLabelFromVision,
placeholder,
size = 'default',
triggerContent,
...restProps
},
ref
) {
return (
<Field.Root className={ className }>
<Field.Label hideFromVision={ hideLabelFromVision }>
{ label }
</Field.Label>
<Select.Root items={ items } { ...restProps }>
<Select.Trigger
ref={ ref }
placeholder={ placeholder }
size={ size }
>
{ triggerContent }
</Select.Trigger>
<Select.Popup>
<SelectControlSizeContext.Provider value={ size }>
{ children !== undefined
? children
: items?.map( ( item ) => (
<Item
key={ item.value ?? 'null' }
value={ item }
label={ item.label }
disabled={ item.disabled }
>
{ item.label }
</Item>
) ) }
</SelectControlSizeContext.Provider>
</Select.Popup>
</Select.Root>
{ description && (
<Field.Description>{ description }</Field.Description>
) }
{ details && <Field.Details>{ details }</Field.Details> }
</Field.Root>
);
} );
220 changes: 220 additions & 0 deletions packages/ui/src/form/select-control/stories/index.story.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
import type { Meta, StoryObj } from '@storybook/react-vite';
import { SelectControl } from '../';
import {
WITH_DETAILS_DESCRIPTION,
DETAILS_EXAMPLE,
} from '../../stories/shared';

const meta: Meta< typeof SelectControl > = {
title: 'Design System/Components/Form/SelectControl',
component: SelectControl,
subcomponents: {
Item: SelectControl.Item,
},
argTypes: {
onValueChange: { action: 'onValueChange' },
},
};

export default meta;

type Story = StoryObj< typeof SelectControl >;

const defaultItems = [
{
value: '1',
label: 'Item 1',
},
{
value: '2',
label: 'Item 2',
},
];

export const Default: Story = {
args: {
items: defaultItems,
label: 'Label',
description: 'This is the description.',
},
};

/**
* When no value is selected, the trigger shows the default placeholder text.
*
* Use the `placeholder` prop to customize text shown.
* Prefer a concise label without a trailing ellipsis.
*/
export const WithCustomPlaceholder: Story = {
args: {
...Default.args,
placeholder: 'Choose an item',
},
};

const nullValueOptionItems = [
{
value: null,
label: 'Select theme',
},
{
value: 'system',
label: 'System default',
},
{
value: 'light',
label: 'Light',
},
{
value: 'dark',
label: 'Dark',
},
];

/**
* Use a `null` item value when users should be able to clear the selected value
* from the popup.
*/
export const WithNullValueOption: Story = {
args: {
items: nullValueOptionItems,
label: 'Theme',
description: 'Choose a theme preference.',
defaultValue: nullValueOptionItems[ 0 ],
},
};

export const VisuallyHiddenLabel: Story = {
args: {
...Default.args,
hideLabelFromVision: true,
},
};

export const WithDetails: Story = {
parameters: {
docs: { description: { story: WITH_DETAILS_DESCRIPTION } },
},
args: {
...Default.args,
description: undefined,
details: DETAILS_EXAMPLE,
},
};

const disabledOptionItems = [
{
value: '1',
label: 'Item 1',
},
{
value: '2',
label: 'Item 2',
disabled: true,
},
];

export const WithDisabledOption: Story = {
args: {
items: disabledOptionItems,
label: 'Label',
description: 'This is the description.',
defaultValue: disabledOptionItems[ 0 ],
},
};

const userOptions: React.ComponentProps< typeof SelectControl >[ 'items' ] = [
{
value: '1',
label: 'User 1 (Admin)',
},
{
value: '2',
label: 'User 2 (Editor)',
},
{
value: '3',
label: 'User 3 (Author)',
},
];

const User = ( { user }: { user: ( typeof userOptions )[ number ] } ) => (
<span
style={ {
display: 'flex',
alignItems: 'center',
gap: 8,
} }
>
<img
src={ `https://gravatar.com/avatar/?d=initials&name=${
user.value ?? 'null'
}` }
alt=""
width="20"
style={ {
borderRadius: '50%',
} }
/>
{ user.label }
</span>
);

/**
* To customize what is rendered inside the trigger element, pass a
* render function to the `triggerContent` prop.
*
* The item list can be customized by passing an array of
* `SelectControl.Item` as children. Note that the `label` prop of a `SelectControl.Item`
* is used as the string to match against in the typeahead functionality, while the
* item content is determined by `children`.
*/
export const WithCustomTriggerAndItems: Story = {
args: {
items: userOptions,
label: 'Label',
description: 'This is the description.',
triggerContent: ( item ) => <User user={ item } />,
children: (
<>
{ userOptions.map( ( item ) => (
<SelectControl.Item
key={ item.value }
value={ item }
label={ item.label }
>
<User user={ item } />
</SelectControl.Item>
) ) }
</>
),
defaultValue: userOptions[ 0 ],
},
};

/**
* By default, the `items` array is used to render both the Trigger
* and the Item list. Passing a custom `triggerContent` or `children` in addition
* to `items` will override that particular aspect of the behavior.
* In other words, if you pass both an `items` array and a custom `triggerContent`,
* the Item list in the popover will still be rendered based on the `items` array.
*/
export const WithItemsArrayAndPartialCustomization: Story = {
args: {
...Default.args,
children: (
<>
{ Default.args?.items?.map( ( item ) => (
<SelectControl.Item
key={ item.value ?? 'null' }
value={ item }
label={ item.label }
disabled={ item.disabled }
>
✨ { item.label }
</SelectControl.Item>
) ) }
</>
),
},
};
Loading
Loading