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

Components: ComboboxControl supports disabled items #61294

Merged
merged 27 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
3f75550
add story
retrofox May 1, 2024
8d6155b
introduce is-disabled state
retrofox May 1, 2024
261b81d
add disabled style
retrofox May 1, 2024
bebe544
do not select disabled option
retrofox May 1, 2024
9a6f225
do not select disabled combobhox option
retrofox May 1, 2024
da90e1e
handle keyDown events with disabled options
retrofox May 1, 2024
77ce608
improve story
retrofox May 1, 2024
106a582
update docs
retrofox May 1, 2024
366e69b
fix skiping disabled items when press UP
retrofox May 1, 2024
123d3dc
improve process
retrofox May 1, 2024
e5e7999
fix uppecase in AndorrA name
retrofox May 1, 2024
393c2f3
minor jsdoc change
retrofox May 1, 2024
c0856f0
make disable option discoverable
retrofox May 3, 2024
d08495f
tab vs spaces - round one!
retrofox May 3, 2024
6dabc7c
rename story name
retrofox May 3, 2024
941881c
Merge branch 'trunk' into update/combobox-control-support-disabled-items
retrofox May 6, 2024
0e61a05
changelog
retrofox May 6, 2024
b03f3f9
rely on pointer-events: none to handle events
retrofox May 6, 2024
87290d8
tweak disable styles
retrofox May 6, 2024
d0bb447
Merge branch 'trunk' into update/combobox-control-support-disabled-items
retrofox May 7, 2024
36f8048
apply color opacity when disabled
retrofox May 7, 2024
5cd34a5
add story jsdoc
retrofox May 7, 2024
b2494d6
Merge branch 'trunk' into update/combobox-control-support-disabled-items
retrofox May 8, 2024
b29851d
move changelog entry to unreleased section
retrofox May 8, 2024
6809163
Merge branch 'trunk' into update/combobox-control-support-disabled-items
retrofox May 8, 2024
d9ee4e2
Merge branch 'trunk' into update/combobox-control-support-disabled-items
retrofox May 10, 2024
8bf196a
introduce @components-color-accent-transparent-40
retrofox May 10, 2024
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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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)).

### Bug Fix
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
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
Loading