Repository navigation
Expand file tree
/
Copy pathSearchField.js
More file actions
122 lines (110 loc) · 3.04 KB
/
Copy pathSearchField.js
File metadata and controls
122 lines (110 loc) · 3.04 KB
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
/**
* SearchField
*
* A universal search field component
*/
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import TextField from '../TextField';
import Select from '../Select';
import TextFieldIcon from '../TextField/TextFieldIcon';
import css from './SearchField.css';
// Accepts the same props as TextField
const propTypes = {
ariaLabel: PropTypes.string,
className: PropTypes.string,
clearSearchId: PropTypes.string,
id: PropTypes.string,
inputClass: PropTypes.string,
inputRef: PropTypes.object,
loading: PropTypes.bool,
onChange: PropTypes.func,
onChangeIndex: PropTypes.func,
onClear: 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,
selectedIndex: PropTypes.string,
value: PropTypes.string,
};
const SearchField = (props) => {
const {
className,
placeholder,
id,
ariaLabel,
value,
onChange,
onClear,
loading,
clearSearchId,
searchableIndexes,
onChangeIndex,
selectedIndex,
searchableIndexesPlaceholder,
inputClass,
...rest
} = props;
/**
* Search field has searchable indexes dropdown
*/
let searchableIndexesDropdown;
const hasSearchableIndexes = Array.isArray(searchableIndexes);
if (hasSearchableIndexes) {
searchableIndexesDropdown = (
<Select
dataOptions={searchableIndexes}
id={`${id}-qindex`}
marginBottom0
onChange={onChangeIndex}
placeholder={searchableIndexesPlaceholder}
selectClass={css.select}
value={selectedIndex}
/>
);
}
// 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 || '';
}
return (
<div className={rootStyles}>
{searchableIndexesDropdown}
<TextField
{...rest}
aria-label={rest['aria-label'] || ariaLabel}
clearFieldId={clearSearchId}
focusedClass={css.isFocused}
id={id}
hasClearIcon={typeof onClear === 'function' && loading !== true}
inputClass={classNames(css.input, inputClass)}
loading={loading}
onChange={onChange}
onClearField={onClear}
placeholder={inputPlaceholder}
startControl={!hasSearchableIndexes ? searchIcon : null}
type="search"
value={value || ''}
/>
</div>
);
};
SearchField.propTypes = propTypes;
export default SearchField;