-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathindex.js
executable file
·119 lines (109 loc) · 2.87 KB
/
index.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
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import Input from '../Input'
import SearchIcon from '../icon/Search'
import ClearIcon from '../icon/Clear'
import { withForwardedRef, refShape } from '../../modules/withForwardedRef'
class InputSearch extends Component {
constructor(props) {
super(props)
this.state = {
hover: false,
focus: false,
}
}
static iconSizes = {
small: 14,
regular: 16,
large: 18,
}
static separatorHeight = {
small: 28,
regular: 36,
large: 44,
}
handleClickClear = event => {
this.props.onChange &&
this.props.onChange({
...event,
target: {
...event.target,
value: '',
},
})
this.props.onClear && this.props.onClear(event)
}
handleSubmit = event => {
const { onSubmit, value } = this.props
onSubmit &&
onSubmit({
...event,
target: {
...event.target,
value,
},
preventDefault: event.preventDefault || (() => {}),
})
}
handleHovering = hover => {
this.setState({ hover })
}
handleFocus = focus => {
this.setState({ focus })
}
render() {
const { hover, focus } = this.state
const { disabled, size, value } = this.props
const iconSize =
InputSearch.iconSizes[size] || InputSearch.iconSizes.regular
return (
<Input
{...this.props}
onFocus={() => this.handleFocus(true)}
onBlur={() => this.handleFocus(false)}
onMouseEnter={() => this.handleHovering(true)}
onMouseLeave={() => this.handleHovering(false)}
onKeyUp={e => e.key === 'Enter' && this.handleSubmit(e)}
type="search"
suffix={
<div className="flex flex-row items-center">
{value && (
<span
tabIndex={0}
onClick={this.handleClickClear}
className="pointer mr4 c-muted-3">
{!disabled && <ClearIcon size={iconSize} />}
</span>
)}
<div
className={`mh2 bw1 bl ${
focus ? 'b--muted-2' : hover ? 'b--muted-3' : 'b--muted-4'
}`}
style={{
height:
InputSearch.separatorHeight[size] ||
InputSearch.separatorHeight.regular,
}}
/>
<span
className="pointer pl4 c-link flex"
onClick={this.handleSubmit}>
<SearchIcon size={iconSize} />
</span>
</div>
}
/>
)
}
}
InputSearch.propTypes = {
disabled: PropTypes.bool,
/** @ignore Forwarded Ref */
forwardedRef: refShape,
onChange: PropTypes.func,
onSubmit: PropTypes.func,
onClear: PropTypes.func,
size: PropTypes.string,
value: PropTypes.string,
}
export default withForwardedRef(InputSearch)