forked from taskcluster/taskcluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.jsx
165 lines (153 loc) · 3.93 KB
/
index.jsx
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
import React, { PureComponent } from 'react';
import { object, func, string, bool } from 'prop-types';
import classNames from 'classnames';
import { withStyles } from '@material-ui/core/styles';
import { fade } from '@material-ui/core/styles/colorManipulator';
import FormControl from '@material-ui/core/FormControl';
import MagnifyIcon from 'mdi-react/MagnifyIcon';
import { THEME } from '../../utils/constants';
@withStyles(theme => ({
root: {
background: fade(THEME.PRIMARY_DARK, 0.5),
borderRadius: 2,
'&:hover': {
background: fade(THEME.PRIMARY_DARK, 0.9),
},
'& $input': {
transition: theme.transitions.create('width'),
width: 200,
'&:focus': {
width: 300,
},
},
},
formControl: {
width: '100%',
},
search: {
width: theme.spacing.unit * 6,
height: '100%',
position: 'absolute',
pointerEvents: 'none',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
'& svg': {
fill: fade(theme.palette.common.white, 0.5),
},
},
input: {
font: 'inherit',
paddingTop: theme.spacing.unit,
paddingRight: theme.spacing.unit,
paddingBottom: theme.spacing.unit,
paddingLeft: theme.spacing.unit * 6,
border: 0,
display: 'block',
verticalAlign: 'middle',
whiteSpace: 'normal',
background: 'none',
margin: 0, // Reset for Safari
color: fade(THEME.PRIMARY_TEXT_DARK, 0.5),
width: '100%',
'&:focus': {
color: fade(THEME.PRIMARY_TEXT_DARK, 0.9),
outline: 0,
},
},
}))
/**
* An app-bar compatible controlled search field.
*/
export default class Search extends PureComponent {
static defaultProps = {
spellCheck: false,
value: undefined,
onChange: null,
defaultValue: null,
formProps: {},
};
static propTypes = {
/**
* A function to execute when the search form has been submitted.
* The function will provided with the search value trimmed from any
* leading/trailing whitespaces. */
onSubmit: func.isRequired,
/** Set to `true` to enable spell-check on the search field. */
spellCheck: bool,
/**
* The input value, required for a controlled component.
*/
value: string,
/**
* Callback fired when the value is changed.
*
* You can pull out the new value by accessing `event.target.value`.
*/
onChange: func,
/**
* The default value of the `Input` element.
*
* To use it on a controlled input requires the `value` prop
* to initially be set to null.
* */
defaultValue: string,
/**
* Properties applied to the form element.
* */
formProps: object,
};
constructor(props) {
super(props);
this.isControlled =
'value' in props && props.value !== undefined && props.value !== null;
}
state = {
value: '',
};
handleInputChange = e => {
const { onChange } = this.props;
if (this.isControlled) {
return onChange(e);
}
this.setState({ value: e.target.value });
};
handleInputSubmit = e => {
e.preventDefault();
this.props.onSubmit(this.state.value.trim());
};
render() {
const {
classes,
onSubmit,
onChange,
spellCheck,
className,
formProps,
...props
} = this.props;
const { value } = this.state;
return (
<form
onSubmit={this.handleInputSubmit}
{...formProps}
className={classNames(classes.root, formProps.className)}>
<FormControl className={classes.formControl}>
<div className={classes.search}>
<MagnifyIcon />
</div>
<input
id="adornment-search"
spellCheck={spellCheck}
placeholder="Search"
className={classNames(classes.input, className)}
type="text"
value={value}
onChange={this.handleInputChange}
{...props}
/>
</FormControl>
</form>
);
}
}