Skip to content

Commit ec2da01

Browse files
feat(table-toolbar-search): add onBlur/onFocus handling (#10187)
* feat(table-toolbar-search): add onBlur/onFocus handling * feat(table-toolbar-search): add onBlur/onFocus handling * chore(table-toolbar-search): update PublicAPI snapshot * chore(data-table): add args to onFocus/onBlur actions * refactor(table-toolbar-search): change internal name to match practice * chore(table-toolbar-search): update story
1 parent bc6946d commit ec2da01

File tree

4 files changed

+93
-2
lines changed

4 files changed

+93
-2
lines changed

packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,6 +1558,9 @@ Map {
15581558
"labelText": Object {
15591559
"type": "string",
15601560
},
1561+
"onBlur": Object {
1562+
"type": "func",
1563+
},
15611564
"onChange": Object {
15621565
"type": "func",
15631566
},
@@ -1567,6 +1570,9 @@ Map {
15671570
"onExpand": Object {
15681571
"type": "func",
15691572
},
1573+
"onFocus": Object {
1574+
"type": "func",
1575+
},
15701576
"persistant": [Function],
15711577
"persistent": Object {
15721578
"type": "bool",
@@ -2190,6 +2196,9 @@ Map {
21902196
"labelText": Object {
21912197
"type": "string",
21922198
},
2199+
"onBlur": Object {
2200+
"type": "func",
2201+
},
21932202
"onChange": Object {
21942203
"type": "func",
21952204
},
@@ -2199,6 +2208,9 @@ Map {
21992208
"onExpand": Object {
22002209
"type": "func",
22012210
},
2211+
"onFocus": Object {
2212+
"type": "func",
2213+
},
22022214
"persistant": [Function],
22032215
"persistent": Object {
22042216
"type": "bool",

packages/react/src/components/DataTable/DataTable-story.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,17 @@ export const WithToolbar = () => (
216216
<TableToolbarSearch
217217
onChange={onInputChange}
218218
onClear={action('onClear')}
219+
onFocus={(event, handleExpand) => {
220+
action('onFocus')();
221+
handleExpand(event, true);
222+
}}
223+
onBlur={(event, handleExpand) => {
224+
action('onBlur')();
225+
const { value } = event.target;
226+
if (!value) {
227+
handleExpand(event, false);
228+
}
229+
}}
219230
/>
220231
<TableToolbarMenu light>
221232
<TableToolbarAction onClick={action('Action 1 Click')}>

packages/react/src/components/DataTable/TableToolbarSearch.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ const TableToolbarSearch = ({
3939
persistent,
4040
persistant,
4141
id,
42+
onBlur,
43+
onFocus,
4244
...rest
4345
}) => {
4446
const { current: controlled } = useRef(expandedProp !== undefined);
@@ -96,6 +98,9 @@ const TableToolbarSearch = ({
9698
}
9799
};
98100

101+
const handleOnFocus = (event) => handleExpand(event, true);
102+
const handleOnBlur = (event) => !value && handleExpand(event, false);
103+
99104
return (
100105
<Search
101106
disabled={disabled}
@@ -110,8 +115,10 @@ const TableToolbarSearch = ({
110115
}
111116
onChange={onChange}
112117
onClear={onClear}
113-
onFocus={(event) => handleExpand(event, true)}
114-
onBlur={(event) => !value && handleExpand(event, false)}
118+
onFocus={
119+
onFocus ? (event) => onFocus(event, handleExpand) : handleOnFocus
120+
}
121+
onBlur={onBlur ? (event) => onBlur(event, handleExpand) : handleOnBlur}
115122
{...rest}
116123
/>
117124
);
@@ -155,6 +162,13 @@ TableToolbarSearch.propTypes = {
155162
*/
156163
labelText: PropTypes.string,
157164

165+
/**
166+
* Provide an optional function to be called when the search input loses focus, this will be
167+
* passed the event as the first parameter and a function to handle the expanding of the search
168+
* input as the second
169+
*/
170+
onBlur: PropTypes.func,
171+
158172
/**
159173
* Provide an optional hook that is called each time the input is updated
160174
*/
@@ -170,6 +184,13 @@ TableToolbarSearch.propTypes = {
170184
*/
171185
onExpand: PropTypes.func,
172186

187+
/**
188+
* Provide an optional function to be called when the search input gains focus, this will be
189+
* passed the event as the first parameter and a function to handle the expanding of the search
190+
* input as the second.
191+
*/
192+
onFocus: PropTypes.func,
193+
173194
persistant: deprecate(
174195
PropTypes.bool,
175196
`\nThe prop \`persistant\` for TableToolbarSearch has been deprecated in favor of \`persistent\`. Please use \`persistent\` instead.`

packages/react/src/components/DataTable/__tests__/TableToolbarSearch-test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import React from 'react';
99
import { mount } from 'enzyme';
1010
import { TableToolbarSearch } from '../';
11+
import { settings } from 'carbon-components';
12+
13+
const { prefix } = settings;
1114

1215
describe('DataTable.TableToolbarSearch', () => {
1316
it('should render', () => {
@@ -26,4 +29,48 @@ describe('DataTable.TableToolbarSearch', () => {
2629
);
2730
expect(typeof wrapper.find('Search').prop('id')).toBe('string');
2831
});
32+
it('should pass handleExpand as second parameter to onBlur/onFocus when provided', () => {
33+
const onBlur = jest.fn().mockImplementation((event, handleExpand) => {
34+
const { value } = event.target;
35+
if (!value) {
36+
handleExpand(event, false);
37+
}
38+
});
39+
const onFocus = jest.fn().mockImplementation((event, handleExpand) => {
40+
handleExpand(event, true);
41+
});
42+
const wrapper = mount(
43+
<TableToolbarSearch
44+
onChange={jest.fn()}
45+
onBlur={onBlur}
46+
onFocus={onFocus}
47+
/>
48+
);
49+
wrapper.find('input').simulate('focus');
50+
expect(onFocus).toHaveBeenCalledTimes(1);
51+
expect(typeof onFocus.mock.calls[0][1]).toEqual('function');
52+
expect(wrapper.find('div').first().instance()).toHaveClass(
53+
`${prefix}--toolbar-search-container-active`
54+
);
55+
wrapper.find('input').simulate('blur', { target: { value: 'test' } });
56+
expect(onBlur).toHaveBeenCalledTimes(1);
57+
expect(typeof onBlur.mock.calls[0][1]).toEqual('function');
58+
59+
// should continue to be expanded, because the input has a value.
60+
expect(wrapper.find('div').first().instance()).toHaveClass(
61+
`${prefix}--toolbar-search-container-active`
62+
);
63+
});
64+
65+
it('should expand/contract as normal when no onBlur/onFocus provided', () => {
66+
const wrapper = mount(<TableToolbarSearch onChange={jest.fn()} />);
67+
wrapper.find('input').simulate('focus');
68+
expect(wrapper.find('div').first().instance()).toHaveClass(
69+
`${prefix}--toolbar-search-container-active`
70+
);
71+
wrapper.find('input').simulate('blur');
72+
expect(wrapper.find('div').first().instance()).not.toHaveClass(
73+
`${prefix}--toolbar-search-container-active`
74+
);
75+
});
2976
});

0 commit comments

Comments
 (0)