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

CustomSelectControl V2: prevent keyboard event propagation in legacy wrapper #62907

Merged
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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Internal

- `CustomSelectControlV2`: prevent keyboard event propagation in legacy wrapper. ([#62907](https://github.com/WordPress/gutenberg/pull/62907))
- `CustomSelectControlV2`: add root element wrapper. ([#62803](https://github.com/WordPress/gutenberg/pull/62803))
- `CustomSelectControlV2`: fix popover styles. ([#62821](https://github.com/WordPress/gutenberg/pull/62821))
- `CustomSelectControlV2`: fix trigger text alignment in RTL languages ([#62869](https://github.com/WordPress/gutenberg/pull/62869)).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { createContext, useMemo } from '@wordpress/element';
import { createContext, useCallback, useMemo } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';

/**
Expand All @@ -14,6 +14,7 @@ import type {
CustomSelectStore,
CustomSelectButtonProps,
CustomSelectButtonSize,
_CustomSelectInternalProps,
_CustomSelectProps,
} from './types';
import type { WordPressComponentProps } from '../context';
Expand Down Expand Up @@ -80,7 +81,10 @@ const CustomSelectButton = ( {
};

function _CustomSelect(
props: _CustomSelectProps & CustomSelectStore & CustomSelectButtonSize
props: _CustomSelectInternalProps &
_CustomSelectProps &
CustomSelectStore &
CustomSelectButtonSize
) {
const {
children,
Expand All @@ -89,9 +93,20 @@ function _CustomSelect(
size,
store,
className,
isLegacy = false,
...restProps
} = props;

const onSelectPopoverKeyDown: React.KeyboardEventHandler< HTMLDivElement > =
useCallback(
( e ) => {
if ( isLegacy ) {
e.stopPropagation();
}
},
[ isLegacy ]
);

return (
// Where should `restProps` be forwarded to?
<div className={ className }>
Expand All @@ -117,6 +132,7 @@ function _CustomSelect(
store={ store }
sameWidth
slide={ false }
onKeyDown={ onSelectPopoverKeyDown }
>
<CustomSelectContext.Provider value={ { store } }>
{ children }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ function CustomSelectControl( props: LegacyCustomSelectProps ) {
'components-custom-select-control',
classNameProp
) }
isLegacy
{ ...restProps }
>
{ children }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,7 @@ describe.each( [
} );

describe( 'Keyboard behavior and accessibility', () => {
// skip reason: legacy v2 doesn't currently implement this behavior
it.skip( 'Captures the keypress event and does not let it propagate', async () => {
it( 'Captures the keypress event and does not let it propagate', async () => {
const onKeyDown = jest.fn();

render(
Expand Down
13 changes: 10 additions & 3 deletions packages/components/src/custom-select-control-v2/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ export type CustomSelectButtonProps = {
value?: string | string[];
};

// Props only exposed on the internal implementation
export type _CustomSelectInternalProps = {
/**
* True if the consumer is emulating the legacy component behavior and look
*/
isLegacy?: boolean;
};
Comment on lines +53 to +58
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I needed a way to allow the internal implementation to expose a isLegacy flag, without forwarding it to the public API of the V2 component. Not sure if we think there's a better way to do that? @mirka

Copy link
Member

Choose a reason for hiding this comment

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

Can't think of a better way, TBH.

What a bummer that we have to do it with hacks like this. It's an indication that the legacy adapter approach has probably too many downsides and won't be a recommended one in the future.

Copy link
Contributor Author

@ciampo ciampo Jul 1, 2024

Choose a reason for hiding this comment

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

The legacy adapted approach definitely has its pros and cons.

It may not be worth keeping a common underlying implementation for V1 and V2 if we then have to abstract it and complicate it with extra props and exceptions. Let's keep it as-is for now and look back at it once V2 legacy wrapper has shipped and we look at V2


// Props that are exposed in exported components
export type _CustomSelectProps = CustomSelectButtonProps & {
/**
* Additional className added to the root wrapper element.
Expand All @@ -70,9 +79,7 @@ export type _CustomSelectProps = CustomSelectButtonProps & {
label: string;
};

export type CustomSelectProps = _CustomSelectProps &
CustomSelectButtonProps &
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed CustomSelectButtonProps since it's already part of _CustomSelectProps

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mirka — just making sure that you see this change, since you refactored these types last

Copy link
Member

Choose a reason for hiding this comment

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

Good catch, thanks 🙏

CustomSelectSize;
export type CustomSelectProps = _CustomSelectProps & CustomSelectSize;

/**
* The legacy object structure for the options array.
Expand Down
Loading