-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathTableToolbarSearch.js
158 lines (135 loc) · 3.79 KB
/
TableToolbarSearch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* Copyright IBM Corp. 2016, 2018
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import cx from 'classnames';
import PropTypes from 'prop-types';
import React, { useRef, useState, useEffect } from 'react';
import { settings } from 'carbon-components';
import Search from '../Search';
import setupGetInstanceId from './tools/instanceId';
const { prefix } = settings;
const getInstanceId = setupGetInstanceId();
const translationKeys = {
'carbon.table.toolbar.search.label': 'Filter table',
'carbon.table.toolbar.search.placeholder': 'Search',
};
const translateWithId = id => {
return translationKeys[id];
};
const TableToolbarSearch = ({
className,
searchContainerClass,
onChange: onChangeProp,
translateWithId: t,
placeHolderText,
labelText,
expanded: expandedProp,
defaultExpanded,
onExpand,
persistant,
id = `data-table-search-${getInstanceId()}`,
...rest
}) => {
const { current: controlled } = useRef(expandedProp !== undefined);
const [expandedState, setExpandedState] = useState(defaultExpanded);
const expanded = controlled ? expandedProp : expandedState;
const searchRef = useRef(null);
const [value, setValue] = useState('');
useEffect(() => {
if (searchRef.current) {
searchRef.current.querySelector('input').focus();
}
});
const searchContainerClasses = cx({
[searchContainerClass]: searchContainerClass,
[`${prefix}--toolbar-action`]: true,
[`${prefix}--toolbar-search-container-active`]: expanded,
[`${prefix}--toolbar-search-container-expandable`]: !persistant,
[`${prefix}--toolbar-search-container-persistant`]: persistant,
});
const searchClasses = cx({
className,
[`${prefix}--search-maginfier`]: true,
});
const handleExpand = (event, value = !expanded) => {
if (!controlled && !persistant) {
setExpandedState(value);
}
if (onExpand) {
onExpand(event, value);
}
};
const onChange = e => {
setValue(e.target.value);
if (onChangeProp) {
onChangeProp(e);
}
};
return (
<div
tabIndex="0"
role="searchbox"
ref={searchRef}
onClick={event => handleExpand(event, true)}
onFocus={event => handleExpand(event, true)}
onBlur={event => !value && handleExpand(event, false)}
className={searchContainerClasses}>
<Search
{...rest}
small
className={searchClasses}
value={value}
id={id}
aria-hidden={!expanded}
labelText={labelText || t('carbon.table.toolbar.search.label')}
placeHolderText={
placeHolderText || t('carbon.table.toolbar.search.placeholder')
}
onChange={onChange}
/>
</div>
);
};
TableToolbarSearch.propTypes = {
children: PropTypes.node,
/**
* Provide an optional class name for the search container
*/
className: PropTypes.string,
/**
* Provide an optional id for the search container
*/
id: PropTypes.string,
/**
* Provide an optional className for the overal container of the Search
*/
searchContainerClasses: PropTypes.string,
/**
* Provide an optional hook that is called each time the input is updated
*/
onChange: PropTypes.func,
/**
* Provide an optional placeholder text for the Search component
*/
placeHolderText: PropTypes.string,
/**
* Provide an optional label text for the Search component icon
*/
labelText: PropTypes.string,
/**
* Provide custom text for the component for each translation id
*/
translateWithId: PropTypes.func.isRequired,
/**
* Whether the search should be allowed to expand
*/
persistant: PropTypes.bool,
};
TableToolbarSearch.defaultProps = {
translateWithId,
persistant: false,
};
export default TableToolbarSearch;