Skip to content

Commit

Permalink
feat(search): add onClear callback prop to Search/DataTableSearch (#9737
Browse files Browse the repository at this point in the history
)

* feat(search): add onClear callback prop to Search/DataTableSearch

* feat(data-table-search): add onClear default

* feat(search): add onClear actions to stories

* fix(search): adjust test to account for shallow rendering

Co-authored-by: Taylor Jones <tay1orjones@users.noreply.github.com>
Co-authored-by: Abbey Hart <abbeyhrt@gmail.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored Oct 7, 2021
1 parent 0152e74 commit 3e0231d
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 1 deletion.
15 changes: 15 additions & 0 deletions packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1527,6 +1527,7 @@ Map {
},
"TableToolbarSearch": Object {
"defaultProps": Object {
"onClear": [Function],
"persistent": false,
"tabIndex": "0",
"translateWithId": [Function],
Expand Down Expand Up @@ -1559,6 +1560,9 @@ Map {
"onChange": Object {
"type": "func",
},
"onClear": Object {
"type": "func",
},
"onExpand": Object {
"type": "func",
},
Expand Down Expand Up @@ -2157,6 +2161,7 @@ Map {
},
"TableToolbarSearch" => Object {
"defaultProps": Object {
"onClear": [Function],
"persistent": false,
"tabIndex": "0",
"translateWithId": [Function],
Expand Down Expand Up @@ -2189,6 +2194,9 @@ Map {
"onChange": Object {
"type": "func",
},
"onClear": Object {
"type": "func",
},
"onExpand": Object {
"type": "func",
},
Expand Down Expand Up @@ -5314,6 +5322,7 @@ Map {
"defaultProps": Object {
"closeButtonLabelText": "Clear search input",
"onChange": [Function],
"onClear": [Function],
"placeholder": "",
"type": "text",
},
Expand Down Expand Up @@ -5353,6 +5362,9 @@ Map {
"onChange": Object {
"type": "func",
},
"onClear": Object {
"type": "func",
},
"onKeyDown": Object {
"type": "func",
},
Expand Down Expand Up @@ -5484,6 +5496,9 @@ Map {
"onChange": Object {
"type": "func",
},
"onClear": Object {
"type": "func",
},
"onKeyDown": Object {
"type": "func",
},
Expand Down
5 changes: 4 additions & 1 deletion packages/react/src/components/DataTable/DataTable-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,10 @@ export const WithToolbar = () => (
{...getTableContainerProps()}>
<TableToolbar {...getToolbarProps()} aria-label="data table toolbar">
<TableToolbarContent>
<TableToolbarSearch onChange={onInputChange} />
<TableToolbarSearch
onChange={onInputChange}
onClear={action('onClear')}
/>
<TableToolbarMenu light>
<TableToolbarAction onClick={action('Action 1 Click')}>
Action 1
Expand Down
8 changes: 8 additions & 0 deletions packages/react/src/components/DataTable/TableToolbarSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const TableToolbarSearch = ({
className,
searchContainerClass,
onChange: onChangeProp,
onClear,
translateWithId: t,
placeHolderText,
placeholder,
Expand Down Expand Up @@ -108,6 +109,7 @@ const TableToolbarSearch = ({
t('carbon.table.toolbar.search.placeholder')
}
onChange={onChange}
onClear={onClear}
onFocus={(event) => handleExpand(event, true)}
onBlur={(event) => !value && handleExpand(event, false)}
{...rest}
Expand Down Expand Up @@ -158,6 +160,11 @@ TableToolbarSearch.propTypes = {
*/
onChange: PropTypes.func,

/**
* Optional callback called when the search value is cleared.
*/
onClear: PropTypes.func,

/**
* Provide an optional hook that is called each time the input is expanded
*/
Expand Down Expand Up @@ -205,6 +212,7 @@ TableToolbarSearch.defaultProps = {
tabIndex: '0',
translateWithId,
persistent: false,
onClear: () => {},
};

export default TableToolbarSearch;
Original file line number Diff line number Diff line change
Expand Up @@ -1870,6 +1870,7 @@ exports[`DataTable should render 1`] = `
<TableToolbarSearch
id="custom-id"
onChange={[Function]}
onClear={[Function]}
persistent={true}
tabIndex="0"
translateWithId={[Function]}
Expand Down Expand Up @@ -2331,6 +2332,7 @@ exports[`DataTable should render 1`] = `
<TableToolbarSearch
id="custom-id"
onChange={[Function]}
onClear={[Function]}
persistent={true}
tabIndex="0"
translateWithId={[Function]}
Expand All @@ -2342,6 +2344,7 @@ exports[`DataTable should render 1`] = `
labelText="Filter table"
onBlur={[Function]}
onChange={[Function]}
onClear={[Function]}
onFocus={[Function]}
placeholder="Filter table"
tabIndex="0"
Expand Down Expand Up @@ -2928,6 +2931,7 @@ exports[`DataTable sticky header should render 1`] = `
<TableToolbarSearch
id="custom-id"
onChange={[Function]}
onClear={[Function]}
persistent={true}
tabIndex="0"
translateWithId={[Function]}
Expand Down Expand Up @@ -3392,6 +3396,7 @@ exports[`DataTable sticky header should render 1`] = `
<TableToolbarSearch
id="custom-id"
onChange={[Function]}
onClear={[Function]}
persistent={true}
tabIndex="0"
translateWithId={[Function]}
Expand All @@ -3403,6 +3408,7 @@ exports[`DataTable sticky header should render 1`] = `
labelText="Filter table"
onBlur={[Function]}
onChange={[Function]}
onClear={[Function]}
onFocus={[Function]}
placeholder="Filter table"
tabIndex="0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ exports[`DataTable.TableToolbarSearch should render 1`] = `
className="custom-class"
id="custom-id"
onChange={[MockFunction]}
onClear={[Function]}
persistent={false}
tabIndex="0"
translateWithId={[Function]}
Expand All @@ -16,6 +17,7 @@ exports[`DataTable.TableToolbarSearch should render 1`] = `
labelText="Filter table"
onBlur={[Function]}
onChange={[Function]}
onClear={[Function]}
onFocus={[Function]}
placeholder="Filter table"
tabIndex="0"
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/components/Search/Search-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const props = () => ({
placeholder: text('Placeholder text (placeholder)', 'Search'),
onChange: action('onChange'),
onKeyDown: action('onKeyDown'),
onClear: action('onClear'),
});

export default {
Expand Down
13 changes: 13 additions & 0 deletions packages/react/src/components/Search/Search-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,15 @@ describe('Search', () => {
describe('enabled textinput', () => {
const onClick = jest.fn();
const onChange = jest.fn();
const onClear = jest.fn();

const wrapper = shallow(
<Search
id="test"
labelText="testlabel"
onClick={onClick}
onChange={onChange}
onClear={onClear}
/>
);

Expand All @@ -208,6 +210,17 @@ describe('Search', () => {
input.simulate('change', eventObject);
expect(onChange).toHaveBeenCalledWith(eventObject);
});

it('should invoke onClear when input value is cleared', () => {
wrapper.setProps({ value: 'test' });
const focus = jest.fn();
input.getElement().ref({
focus,
});
wrapper.find('button').simulate('click', { target: { value: 'test' } });
expect(onClear).toHaveBeenCalled();
expect(focus).toHaveBeenCalled();
});
});
});
});
Expand Down
9 changes: 9 additions & 0 deletions packages/react/src/components/Search/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ export default class Search extends Component {
*/
onChange: PropTypes.func,

/**
* Optional callback called when the search value is cleared.
*/
onClear: PropTypes.func,

/**
* Provide a handler that is invoked on the key down event for the input
*/
Expand Down Expand Up @@ -120,6 +125,7 @@ export default class Search extends Component {
placeholder: '',
closeButtonLabelText: 'Clear search input',
onChange: () => {},
onClear: () => {},
};

state = {
Expand Down Expand Up @@ -150,6 +156,8 @@ export default class Search extends Component {
this.props.onChange(clearedEvt);
}

this.props.onClear();

this.setState({ hasContent: false }, () => this.input.focus());
};

Expand Down Expand Up @@ -183,6 +191,7 @@ export default class Search extends Component {
onChange,
onKeyDown,
renderIcon,
onClear, // eslint-disable-line no-unused-vars
...other
} = this.props;

Expand Down

0 comments on commit 3e0231d

Please sign in to comment.