Skip to content

Commit

Permalink
SearchControl: Deprecate onClose prop (#65988)
Browse files Browse the repository at this point in the history
* Soft deprecate onClose function prop and execute deprecated

* Update README and StoryBook JSDoc

* Expect console.warn due to deprecated func call and fix typo

* Address PR feedback

Remove WithOnClose storybook variation. Refactor deprecation message. Add @ignore for onClose in types.ts. Add deprecations section in CHANGELOG.md

* Replace toHaveWarned with toHaveWarnedWith

Co-authored-by: Vrishabhsk <vrishabhsk@git.wordpress.org>
Co-authored-by: mirka <0mirka00@git.wordpress.org>
Co-authored-by: ciampo <mciampini@git.wordpress.org>
  • Loading branch information
4 people authored Oct 10, 2024
1 parent d809650 commit 8d1dd6c
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 16 deletions.
4 changes: 4 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
- `Tabs`: fix skipping indication animation glitch ([#65878](https://github.com/WordPress/gutenberg/pull/65878)).
- `ToggleGroupControl`: Don't autoselect option on first group focus ([#65892](https://github.com/WordPress/gutenberg/pull/65892)).

### Deprecations

- `SearchControl`: Soft deprecate `onClose` prop ([#65988](https://github.com/WordPress/gutenberg/pull/65988)).

### Enhancements

- `Tabs`: revamped vertical orientation styles ([#65387](https://github.com/WordPress/gutenberg/pull/65387)).
Expand Down
2 changes: 2 additions & 0 deletions packages/components/src/search-control/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ A function that receives the value of the input.

#### onClose

_Note: this prop is deprecated._

When an `onClose` callback is provided, the search control will render a close button that will trigger the given callback.

Use this if you want the button to trigger your own logic to close the search field entirely, rather than just clearing the input value.
Expand Down
7 changes: 7 additions & 0 deletions packages/components/src/search-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { useInstanceId, useMergeRefs } from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
import { Icon, search, closeSmall } from '@wordpress/icons';
import { forwardRef, useMemo, useRef } from '@wordpress/element';
import deprecated from '@wordpress/deprecated';

/**
* Internal dependencies
Expand All @@ -31,6 +32,12 @@ function SuffixItem( {
return <Icon icon={ search } />;
}

if ( onClose ) {
deprecated( '`onClose` prop in wp.components.SearchControl', {
since: '6.8',
} );
}

const onReset = () => {
onChange( '' );
searchRef.current?.focus();
Expand Down
15 changes: 0 additions & 15 deletions packages/components/src/search-control/stories/index.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,3 @@ Default.args = {
help: 'Help text to explain the input.',
__nextHasNoMarginBottom: true,
};

/**
* When an `onClose` callback is provided, the search control will render a close button
* that will trigger the given callback.
*
* Use this if you want the button to trigger your own logic to close the search field entirely,
* rather than just clearing the input value.
*/
export const WithOnClose = Template.bind( {} );
WithOnClose.args = {
...Default.args,
};
WithOnClose.argTypes = {
onClose: { action: 'onClose' },
};
5 changes: 4 additions & 1 deletion packages/components/src/search-control/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,16 @@ describe( 'SearchControl', () => {
expect( onChangeSpy ).toHaveBeenLastCalledWith( '' );
} );

it( 'should should render a Close button (instead of Reset) when onClose function is provided', async () => {
it( 'should render a Close button (instead of Reset) when onClose function is provided', async () => {
const onChangeSpy = jest.fn();
const onCloseSpy = jest.fn();
render(
<Component onChange={ onChangeSpy } onClose={ onCloseSpy } />
);

expect( console ).toHaveWarnedWith(
'`onClose` prop in wp.components.SearchControl is deprecated since version 6.8.'
);
expect(
screen.queryByRole( 'button', { name: 'Close search' } )
).toBeVisible();
Expand Down
3 changes: 3 additions & 0 deletions packages/components/src/search-control/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ export type SearchControlProps = Pick< InputControlProps, 'help' | 'value' > & {
*
* Use this if you want the button to trigger your own logic to close the search field entirely,
* rather than just clearing the input value.
*
* @deprecated
* @ignore
*/
onClose?: () => void;
/** @ignore */
Expand Down

1 comment on commit 8d1dd6c

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 8d1dd6c.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/11273055349
📝 Reported issues:

Please sign in to comment.