Skip to content
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 @@ -7,6 +7,7 @@
- `Button`: Update font weight to `500` ([#70787](https://github.com/WordPress/gutenberg/pull/70787)).
- `TabPanel`: Update tab font weight ([#72455](https://github.com/WordPress/gutenberg/pull/72455)).
- Consistently use font-weight 499 instead of 500 ([#72473](https://github.com/WordPress/gutenberg/pull/72473)).
- `ComboboxControl`: Show reset button only when there is a value ([#72595](https://github.com/WordPress/gutenberg/pull/72595)).

### Bug Fixes

Expand Down
5 changes: 1 addition & 4 deletions packages/components/src/combobox-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -365,13 +365,10 @@ function ComboboxControl( props: ComboboxControlProps ) {
/>
</FlexBlock>
{ isLoading && <Spinner /> }
{ allowReset && (
{ allowReset && Boolean( value ) && ! isExpanded && (
<Button
size="small"
icon={ closeSmall }
// Disable reason: Focus returns to input field when reset is clicked.
// eslint-disable-next-line no-restricted-syntax
disabled={ ! value }
onClick={ handleOnReset }
onKeyDown={ handleResetStopPropagation }
label={ __( 'Reset' ) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ export const Default = Template.bind( {} );
Default.args = {
__next40pxDefaultSize: true,
__nextHasNoMarginBottom: true,
allowReset: false,
label: 'Select a country',
options: countryOptions,
};
Expand Down
27 changes: 20 additions & 7 deletions packages/components/src/combobox-control/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ describe.each( [
expect( input ).toHaveValue( targetOption.label );
} );

it( 'should render with Reset button disabled', () => {
it( 'should not render Reset button when no value is set', () => {
render(
<Component
options={ timezones }
Expand All @@ -365,10 +365,23 @@ describe.each( [
/>
);

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

expect( resetButton ).not.toBeInTheDocument();
} );

it( 'should not render Reset button when allowReset is false', () => {
render(
<Component
options={ timezones }
label={ defaultLabelText }
allowReset={ false }
/>
);

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

expect( resetButton ).toBeVisible();
expect( resetButton ).toBeDisabled();
expect( resetButton ).not.toBeInTheDocument();
} );

it( 'should reset input when clicking the Reset button', async () => {
Expand Down Expand Up @@ -400,8 +413,8 @@ describe.each( [

await user.click( resetButton );

expect( resetButton ).not.toBeInTheDocument();
expect( input ).toHaveValue( '' );
expect( resetButton ).toBeDisabled();
Copy link
Member

Choose a reason for hiding this comment

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

Can we still test for not.toBeInTheDocument?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure 🫡

expect( input ).toHaveFocus();
} );

Expand Down Expand Up @@ -439,8 +452,8 @@ describe.each( [
// Pressing Enter/Return resets the input.
await user.keyboard( '{Enter}' );

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

Expand Down Expand Up @@ -478,8 +491,8 @@ describe.each( [
// Pressing Spacebar resets the input.
await user.keyboard( '[Space]' );

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