Repository navigation
Add SelectControl component to @wordpress/ui #77809
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
Changes from all commits
0821342
1091be5
dd8bca2
94738db
dcfd380
5d0de3e
a7e1a39
da22ae2
2711347
0a4bb4a
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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export * from './primitives'; | ||
|
|
||
| export * from './input-control'; | ||
| export * from './select-control'; |
| 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 ); | ||
| }; |
| 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'; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||
| * A complete select field with integrated label and description. | ||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||
| export const SelectControl = Object.assign( _SelectControl, { | ||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||
| * An item rendered inside a `SelectControl` popup. | ||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||
| Item, | ||||||||||||||||||||||||||||
| } ); | ||||||||||||||||||||||||||||
|
Contributor
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. Should we align this implementation with gutenberg/packages/ui/src/button/index.ts Lines 4 to 16 in 253bb2a
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
Member
Author
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. I think I would prefer inference unless necessary, since casting can mask mistakes. |
||||||||||||||||||||||||||||
| 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 } />; | ||
| } | ||
| ); |
| 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
Contributor
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. Should the JSDoc (for
Member
Author
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. One unfortunate thing I noticed is that Storybook only picks up the JSDoc description on the
Contributor
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. 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> | ||
| ); | ||
| } ); | ||
| 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> | ||
| ) ) } | ||
| </> | ||
| ), | ||
| }, | ||
| }; |
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.
Should we do the same for
_SelectControl?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.
Display name is working here without an explicit definition.