This repository has been archived by the owner on Oct 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 397
/
Search.js
178 lines (154 loc) · 4.21 KB
/
Search.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
/**
* 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 PropTypes from 'prop-types';
import React, { Component } from 'react';
import classNames from 'classnames';
import Search16 from '@carbon/icons-react/lib/search/16';
import Close16 from '@carbon/icons-react/lib/close/16';
import Close20 from '@carbon/icons-react/lib/close/20';
import { settings } from 'carbon-components';
const { prefix } = settings;
export default class Search extends Component {
static propTypes = {
/**
* Specify an optional className to be applied to the container node
*/
className: PropTypes.string,
/**
* Optional prop to specify the type of the `<input>`
*/
type: PropTypes.string,
/**
* Specify whether the Search should be a small variant
*/
small: PropTypes.bool,
/**
* Provide an optional placeholder text for the Search
*/
placeHolderText: PropTypes.string,
/**
* Provide an optional label text for the Search icon
*/
labelText: PropTypes.node.isRequired,
/**
* Specify a custom `id` for the input
*/
id: PropTypes.string,
/**
* Specify a label to be read by screen readers on the "close" button
*/
closeButtonLabelText: PropTypes.string,
/**
* Specify the value of the <input>
*/
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
/**
* Optionally provide the default value of the <input>
*/
defaultValue: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
};
static defaultProps = {
type: 'text',
small: false,
placeHolderText: '',
onChange: () => {},
};
state = {
hasContent: this.props.value || this.props.defaultValue || false,
prevValue: this.props.value,
};
static getDerivedStateFromProps({ value }, state) {
const { prevValue } = state;
return prevValue === value
? null
: {
hasContent: !!value,
prevValue: value,
};
}
clearInput = evt => {
if (!this.props.value) {
this.input.value = '';
this.props.onChange(evt);
} else {
const clearedEvt = Object.assign({}, evt.target, {
target: {
value: '',
},
});
this.props.onChange(clearedEvt);
}
this.setState({ hasContent: false }, () => this.input.focus());
};
handleChange = evt => {
this.setState({
hasContent: evt.target.value !== '',
});
this.props.onChange(evt);
};
render() {
const {
className,
type,
id = (this._inputId =
this._inputId ||
`search__input__id_${Math.random()
.toString(36)
.substr(2)}`),
placeHolderText,
labelText,
closeButtonLabelText,
small,
...other
} = this.props;
const { hasContent } = this.state;
const searchClasses = classNames({
[`${prefix}--search`]: true,
[`${prefix}--search--xl`]: !small,
[`${prefix}--search--sm`]: small,
[className]: className,
});
const clearClasses = classNames({
[`${prefix}--search-close`]: true,
[`${prefix}--search-close--hidden`]: !hasContent,
});
const CloseIconX = !small ? Close20 : Close16;
return (
<div
className={searchClasses}
role="search"
aria-labelledby={`${id}-label`}>
<Search16
className={`${prefix}--search-magnifier`}
aria-label={labelText}
role="img"
/>
<label id={`${id}-label`} htmlFor={id} className={`${prefix}--label`}>
{labelText}
</label>
<input
{...other}
type={type}
className={`${prefix}--search-input`}
id={id}
placeholder={placeHolderText}
onChange={this.handleChange}
ref={input => {
this.input = input;
}}
/>
<button
className={clearClasses}
onClick={this.clearInput}
type="button"
aria-label={closeButtonLabelText}>
<CloseIconX aria-label={closeButtonLabelText} role="img" />
</button>
</div>
);
}
}