-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathSearchField.js
205 lines (184 loc) · 5.28 KB
/
SearchField.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
* SearchField
*
* A universal search field component
*/
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { useIntl } from 'react-intl';
import noop from 'lodash/noop';
import TextField from '../TextField';
import TextArea from '../TextArea';
import Select from '../Select';
import TextFieldIcon from '../TextField/TextFieldIcon';
import css from './SearchField.css';
const INPUT_TYPES = {
INPUT: 'input',
TEXTAREA: 'textarea',
};
const INPUT_COMPONENTS = {
[INPUT_TYPES.INPUT]: TextField,
[INPUT_TYPES.TEXTAREA]: TextArea,
};
const TEXTAREA_DEFAULT_HEIGHT = 1;
// Accepts the same props as TextField
const propTypes = {
ariaLabel: PropTypes.string,
className: PropTypes.string,
clearSearchId: PropTypes.string,
disabled: PropTypes.bool,
id: PropTypes.string,
indexLabel: PropTypes.string,
indexName: PropTypes.string,
indexRef: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({ current: PropTypes.instanceOf(Element) }),
]),
inputClass: PropTypes.string,
inputRef: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({ current: PropTypes.instanceOf(Element) }),
]),
inputType: PropTypes.oneOf(Object.values(INPUT_TYPES)),
isCursorAtEnd: PropTypes.bool,
loading: PropTypes.bool,
lockWidth: PropTypes.bool,
newLineOnShiftEnter: PropTypes.bool,
onChange: PropTypes.func,
onChangeIndex: PropTypes.func,
onClear: PropTypes.func,
onSubmitSearch: PropTypes.func,
placeholder: PropTypes.string,
searchableIndexes: PropTypes.arrayOf(PropTypes.shape({
disabled: PropTypes.bool,
id: PropTypes.string,
label: PropTypes.string,
placeholder: PropTypes.string,
value: PropTypes.string,
})),
searchableIndexesPlaceholder: PropTypes.string,
searchableOptions: PropTypes.oneOfType([
PropTypes.node,
PropTypes.arrayOf(PropTypes.node),
]),
selectedIndex: PropTypes.string,
value: PropTypes.string,
};
const SearchField = (props) => {
const {
className,
placeholder,
id,
ariaLabel,
indexLabel: indexLabelProp,
indexName,
value,
onChange,
onClear,
loading = false,
clearSearchId,
searchableIndexes,
searchableOptions = null,
onChangeIndex,
selectedIndex,
searchableIndexesPlaceholder,
inputClass,
disabled,
inputType = INPUT_TYPES.INPUT,
onSubmitSearch = noop,
lockWidth = false,
newLineOnShiftEnter = false,
inputRef,
indexRef,
isCursorAtEnd = false,
...rest
} = props;
/**
* Search field has searchable indexes dropdown
*/
let searchableIndexesDropdown;
const hasSearchableIndexes = Array.isArray(searchableIndexes);
const intl = useIntl();
if (hasSearchableIndexes || searchableOptions) {
const indexLabel = indexLabelProp || intl.formatMessage({ id: 'stripes-components.searchFieldIndex' });
searchableIndexesDropdown = (
<Select
aria-label={indexLabelProp || indexLabel}
dataOptions={searchableOptions ? undefined : searchableIndexes}
disabled={loading}
id={`${id}-qindex`}
marginBottom0
onChange={onChangeIndex}
placeholder={searchableIndexesPlaceholder}
selectClass={css.select}
value={selectedIndex}
name={indexName}
inputRef={indexRef}
>
{searchableOptions}
</Select>
);
}
// Wrapper styles
const rootStyles = classNames(
css.searchFieldWrap,
{ [css.hasSearchableIndexes]: hasSearchableIndexes },
className,
);
// Search icon
const searchIcon = (<TextFieldIcon iconClassName={css.searchIcon} icon="search" />);
// Placeholder
let inputPlaceholder = placeholder;
if (!placeholder && hasSearchableIndexes && selectedIndex) {
const selectedIndexConfig = searchableIndexes.find(index => index.value === selectedIndex) || {};
inputPlaceholder = selectedIndexConfig.placeholder || '';
}
const getInputComponentProps = () => {
// TextField and TextArea have slightly different APIs so we need to pass props correctly
const commonProps = {
...rest,
'aria-label': rest['aria-label'] || ariaLabel,
disabled,
id,
loading,
onChange,
startControl: !hasSearchableIndexes && !searchableOptions ? searchIcon : null,
type: 'search',
value: value || '',
readOnly: loading || rest.readOnly,
placeholder: inputPlaceholder,
inputRef,
hasClearIcon: typeof onClear === 'function',
clearFieldId: clearSearchId,
onClearField: onClear,
};
const textFieldProps = {
focusedClass: css.isFocused,
inputClass: classNames(css.input, inputClass),
};
const textAreaProps = {
'aria-label': rest['aria-label'] || ariaLabel,
rootClass: rest.className,
lockWidth,
onSubmitSearch,
newLineOnShiftEnter,
rows: TEXTAREA_DEFAULT_HEIGHT,
isCursorAtEnd,
};
return {
...commonProps,
...(inputType === INPUT_TYPES.INPUT ? textFieldProps : {}),
...(inputType === INPUT_TYPES.TEXTAREA ? textAreaProps : {}),
}
};
const Component = INPUT_COMPONENTS[inputType];
return (
<div className={rootStyles}>
{searchableIndexesDropdown}
<Component {...getInputComponentProps()} />
</div>
);
};
SearchField.propTypes = propTypes;
export default SearchField;