Skip to content

Commit

Permalink
Components: ComboboxControl supports disabled items (#61294)
Browse files Browse the repository at this point in the history
Co-authored-by: retrofox <[email protected]>
Co-authored-by: alexstine <[email protected]>
Co-authored-by: mirka <[email protected]>
Co-authored-by: tyxla <[email protected]>
Co-authored-by: jameskoster <[email protected]>
Co-authored-by: afercia <[email protected]>
Co-authored-by: jasmussen <[email protected]>
  • Loading branch information
8 people authored May 14, 2024
1 parent e2a0a54 commit 82c5e41
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

- `PaletteEdit`: Use consistent spacing and metrics. ([#61368](https://github.com/WordPress/gutenberg/pull/61368)).
- `FormTokenField`: Hide label when not defined ([#61336](https://github.com/WordPress/gutenberg/pull/61336)).
- `ComboboxControl`: supports disabled items ([#61294](https://github.com/WordPress/gutenberg/pull/61294)).
- Upgraded the @types/react and @types/react-dom packages ([#60796](https://github.com/WordPress/gutenberg/pull/60796)).
- `Placeholder`: Tweak placeholder style ([#61590](https://github.com/WordPress/gutenberg/pull/61590)).

Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/combobox-control/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ If this property is added, a help text will be generated using help property as

The options that can be chosen from.

- Type: `Array<{ value: string, label: string }>`
- Type: `Array<{ value: string, label: string, disabled?: boolean }>`
- Required: Yes

#### onFilterValueChange
Expand Down
6 changes: 6 additions & 0 deletions packages/components/src/combobox-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ const getIndexOfMatchingSuggestion = (
* {
* value: 'normal',
* label: 'Normal',
* disabled: true,
* },
* {
* value: 'large',
* label: 'Large',
* disabled: false,
* },
* ];
*
Expand Down Expand Up @@ -165,6 +167,10 @@ function ComboboxControl( props: ComboboxControlProps ) {
const onSuggestionSelected = (
newSelectedSuggestion: ComboboxControlOption
) => {
if ( newSelectedSuggestion.disabled ) {
return;
}

setValue( newSelectedSuggestion.value );
speak( messages.selected, 'assertive' );
setSelectedSuggestion( newSelectedSuggestion );
Expand Down
28 changes: 28 additions & 0 deletions packages/components/src/combobox-control/stories/index.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ const countries = [
{ name: 'Albania', code: 'AL' },
{ name: 'Algeria', code: 'DZ' },
{ name: 'American Samoa', code: 'AS' },
{ name: 'Andorra', code: 'AD' },
{ name: 'Angola', code: 'AO' },
{ name: 'Anguilla', code: 'AI' },
{ name: 'Antarctica', code: 'AQ' },
{ name: 'Antigua and Barbuda', code: 'AG' },
{ name: 'Argentina', code: 'AR' },
{ name: 'Armenia', code: 'AM' },
{ name: 'Aruba', code: 'AW' },
{ name: 'Australia', code: 'AU' },
{ name: 'Austria', code: 'AT' },
{ name: 'Azerbaijan', code: 'AZ' },
];

const meta: Meta< typeof ComboboxControl > = {
Expand Down Expand Up @@ -111,3 +122,20 @@ WithCustomRenderItem.args = {
);
},
};

/**
* You can disable options in the list
* by setting the `disabled` property to true
* for individual items in the option object.
*/
export const WithDisabledOptions = Template.bind( {} );
const optionsWithDisabledOptions = countryOptions.map( ( option, index ) => ( {
...option,
disabled: index % 3 === 0, // Disable options at index 0, 3, 6, etc.
} ) );

WithDisabledOptions.args = {
allowReset: false,
label: 'Select a country',
options: optionsWithDisabledOptions,
};
1 change: 1 addition & 0 deletions packages/components/src/combobox-control/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { BaseControlProps } from '../base-control/types';
export type ComboboxControlOption = {
label: string;
value: string;
disabled?: boolean;
[ key: string ]: any;
};

Expand Down
9 changes: 9 additions & 0 deletions packages/components/src/form-token-field/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,13 @@
background: $components-color-accent;
color: $white;
}

&[aria-disabled="true"] {
pointer-events: none;
color: $gray-600;

&.is-selected {
background-color: $components-color-accent-transparent-40;
}
}
}
22 changes: 14 additions & 8 deletions packages/components/src/form-token-field/suggestions-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ const handleMouseDown: MouseEventHandler = ( e ) => {
e.preventDefault();
};

export function SuggestionsList< T extends string | { value: string } >( {
export function SuggestionsList<
T extends string | { value: string; disabled?: boolean },
>( {
selectedIndex,
scrollIntoView,
match,
Expand Down Expand Up @@ -103,10 +105,18 @@ export function SuggestionsList< T extends string | { value: string } >( {
>
{ suggestions.map( ( suggestion, index ) => {
const matchText = computeSuggestionMatch( suggestion );
const isSelected = index === selectedIndex;
const isDisabled =
typeof suggestion === 'object' && suggestion?.disabled;
const key =
typeof suggestion === 'object' && 'value' in suggestion
? suggestion?.value
: displayTransform( suggestion );

const className = clsx(
'components-form-token-field__suggestion',
{
'is-selected': index === selectedIndex,
'is-selected': isSelected,
}
);

Expand Down Expand Up @@ -134,16 +144,12 @@ export function SuggestionsList< T extends string | { value: string } >( {
id={ `components-form-token-suggestions-${ instanceId }-${ index }` }
role="option"
className={ className }
key={
typeof suggestion === 'object' &&
'value' in suggestion
? suggestion?.value
: displayTransform( suggestion )
}
key={ key }
onMouseDown={ handleMouseDown }
onClick={ handleClick( suggestion ) }
onMouseEnter={ handleHover( suggestion ) }
aria-selected={ index === selectedIndex }
aria-disabled={ isDisabled }
>
{ output }
</li>
Expand Down
4 changes: 4 additions & 0 deletions packages/components/src/utils/theme-variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
// `--wp-admin-theme-color`. If the `--wp-admin-theme-color` variable is not
// defined, fallback to the default theme color (WP blueberry).
$components-color-accent: var(--wp-components-color-accent, var(--wp-admin-theme-color, #3858e9));

// Define accent color transparent variants.
$components-color-accent-transparent-40: rgba(var(--wp-components-color-accent--rgb, var(--wp-admin-theme-color--rgb)), 0.04);

$components-color-accent-darker-10: var(--wp-components-color-accent-darker-10, var(--wp-admin-theme-color-darker-10, #2145e6));
$components-color-accent-darker-20: var(--wp-components-color-accent-darker-20, var(--wp-admin-theme-color-darker-20, #183ad6));

Expand Down

0 comments on commit 82c5e41

Please sign in to comment.