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

Fix ComboboxControl reset button when using the keyboard. #63410

Merged
merged 5 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 @@

### Bug Fixes

- `ComboboxControl`: Fix ComboboxControl reset button when using the keyboard. ([#63410](https://github.com/WordPress/gutenberg/pull/63410))
- `Button`: Never apply `aria-disabled` to anchor ([#63376](https://github.com/WordPress/gutenberg/pull/63376)).

### Internal
Expand Down
10 changes: 10 additions & 0 deletions packages/components/src/combobox-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,15 @@ function ComboboxControl( props: ComboboxControlProps ) {
inputContainer.current?.focus();
};

// Stop propagation of the keydown event when pressing Enter on the Reset
// button to prevent calling the onKeydown callback on the container div
// element which actually sets the selected suggestion.
const handleResetStopPropagation: React.KeyboardEventHandler<
HTMLButtonElement
> = ( event ) => {
event.stopPropagation();
};

// Update current selections when the filter input changes.
useEffect( () => {
const hasMatchingSuggestions = matchingSuggestions.length > 0;
Expand Down Expand Up @@ -350,6 +359,7 @@ function ComboboxControl( props: ComboboxControlProps ) {
// eslint-disable-next-line no-restricted-syntax
disabled={ ! value }
onClick={ handleOnReset }
onKeyDown={ handleResetStopPropagation }
label={ __( 'Reset' ) }
/>
</FlexItem>
Expand Down
149 changes: 149 additions & 0 deletions packages/components/src/combobox-control/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,153 @@ describe.each( [
expect( onChangeSpy ).toHaveBeenCalledWith( targetOption.value );
expect( input ).toHaveValue( targetOption.label );
} );

it( 'should render with Reset button disabled', () => {
render(
<Component
options={ timezones }
label={ defaultLabelText }
allowReset
/>
);

const resetButton = screen.getByRole( 'button', { name: 'Reset' } );

expect( resetButton ).toBeInTheDocument();
expect( resetButton ).toBeVisible();
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
expect( resetButton ).toBeInTheDocument();
expect( resetButton ).toBeVisible();
expect( resetButton ).toBeVisible();

toBeVisible() should already imply that the element is in the document

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, I just copied and pasted from the existing test should render with visible label, will fix it there as well

expect( resetButton ).toBeDisabled();
} );

it( 'should render with Reset button enabled after option selection', async () => {
const user = await userEvent.setup();
const targetOption = timezones[ 13 ];

render(
<Component
options={ timezones }
label={ defaultLabelText }
allowReset
/>
);

// Pressing tab selects the input and shows the options.
await user.tab();
// Type enough characters to ensure a predictable search result.
await user.keyboard( getOptionSearchString( targetOption ) );
// Pressing Enter/Return selects the currently focused option.
await user.keyboard( '{Enter}' );

const resetButton = screen.getByRole( 'button', { name: 'Reset' } );

expect( resetButton ).toBeInTheDocument();
expect( resetButton ).toBeVisible();
expect( resetButton ).toBeEnabled();
} );
Copy link
Contributor

Choose a reason for hiding this comment

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

This test feels superflous, since the next three tests (click/enter/spacebar on the button) would also fail if the button was disabled. At most we can add the expect( resetButton ).toBeEnabled() check in each test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK


it( 'should reset input when clicking the Reset button', async () => {
const user = await userEvent.setup();
const targetOption = timezones[ 13 ];

render(
<Component
options={ timezones }
label={ defaultLabelText }
allowReset
/>
);

// Pressing tab selects the input and shows the options.
await user.tab();
// Type enough characters to ensure a predictable search result.
await user.keyboard( getOptionSearchString( targetOption ) );
// Pressing Enter/Return selects the currently focused option.
await user.keyboard( '{Enter}' );

const input = getInput( defaultLabelText );

expect( input ).toHaveValue( targetOption.label );

const resetButton = screen.getByRole( 'button', { name: 'Reset' } );
await user.click( resetButton );

expect( input ).toHaveValue( '' );
expect( resetButton ).toBeDisabled();
expect( input ).toHaveFocus();
} );

it( 'should reset input when pressing the Reset button with the Enter key', async () => {
const user = await userEvent.setup();
const targetOption = timezones[ 13 ];

render(
<Component
options={ timezones }
label={ defaultLabelText }
allowReset
/>
);

// Pressing tab selects the input and shows the options.
await user.tab();
// Type enough characters to ensure a predictable search result.
await user.keyboard( getOptionSearchString( targetOption ) );
// Pressing Enter/Return selects the currently focused option.
await user.keyboard( '{Enter}' );

const input = getInput( defaultLabelText );

expect( input ).toHaveValue( targetOption.label );

// Pressing tab moves focus to the Reset buttons
await user.tab();

const resetButton = screen.getByRole( 'button', { name: 'Reset' } );

expect( resetButton ).toHaveFocus();

// Pressing Enter/Return resets the input.
await user.keyboard( '{Enter}' );

expect( input ).toHaveValue( '' );
expect( resetButton ).toBeDisabled();
expect( input ).toHaveFocus();
} );

it( 'should reset input when pressing the Reset button with the Spacebar key', async () => {
const user = await userEvent.setup();
const targetOption = timezones[ 13 ];

render(
<Component
options={ timezones }
label={ defaultLabelText }
allowReset
/>
);

// Pressing tab selects the input and shows the options.
await user.tab();
// Type enough characters to ensure a predictable search result.
await user.keyboard( getOptionSearchString( targetOption ) );
// Pressing Enter/Return selects the currently focused option.
await user.keyboard( '{Enter}' );

const input = getInput( defaultLabelText );

expect( input ).toHaveValue( targetOption.label );

// Pressing tab moves focus to the Reset buttons.
await user.tab();

const resetButton = screen.getByRole( 'button', { name: 'Reset' } );

expect( resetButton ).toHaveFocus();

// Pressing Spacebar resets the input.
await user.keyboard( ' ' );

expect( input ).toHaveValue( '' );
expect( resetButton ).toBeDisabled();
expect( input ).toHaveFocus();
} );
} );
Loading