1
- import React from 'react'
1
+ import React , { Component } from 'react'
2
2
import PropTypes from 'prop-types'
3
- import createReactClass from 'create-react-class'
4
- import { createFilter } from './util'
3
+ import { createFilter } from './util'
5
4
6
- const Search = createReactClass ( {
7
- propTypes : {
8
- className : PropTypes . string ,
9
- inputClassName : PropTypes . string ,
10
- onChange : PropTypes . func ,
11
- caseSensitive : PropTypes . bool ,
12
- sortResults : PropTypes . bool ,
13
- fuzzy : PropTypes . bool ,
14
- throttle : PropTypes . number ,
15
- filterKeys : PropTypes . oneOf ( [
16
- PropTypes . string ,
17
- PropTypes . arrayOf ( PropTypes . string )
18
- ] ) ,
19
- value : PropTypes . string
20
- } ,
21
-
22
- getDefaultProps ( ) {
23
- return {
24
- className : '' ,
25
- onChange ( ) { } ,
26
- caseSensitive : false ,
27
- fuzzy : false ,
28
- throttle : 200
29
- }
30
- } ,
31
-
32
- getInitialState ( ) {
33
- return {
5
+ class Search extends Component {
6
+ constructor ( props ) {
7
+ super ( props )
8
+ this . state = {
34
9
searchTerm : this . props . value || ''
35
10
}
36
- } ,
11
+ this . updateSearch = this . updateSearch . bind ( this )
12
+ this . filter = this . filter . bind ( this )
13
+ }
37
14
38
15
componentWillReceiveProps ( nextProps ) {
39
- if ( typeof nextProps . value !== 'undefined' && nextProps . value !== this . props . value ) {
16
+ if (
17
+ typeof nextProps . value !== 'undefined' &&
18
+ nextProps . value !== this . props . value
19
+ ) {
40
20
const e = {
41
21
target : {
42
22
value : nextProps . value
43
23
}
44
24
}
45
25
this . updateSearch ( e )
46
26
}
47
- } ,
27
+ }
48
28
49
29
render ( ) {
50
- const { className, onChange, caseSensitive, sortResults, throttle, filterKeys, value, fuzzy, inputClassName, ...inputProps } = this . props // eslint-disable-line no-unused-vars
30
+ const {
31
+ className,
32
+ onChange,
33
+ caseSensitive,
34
+ sortResults,
35
+ throttle,
36
+ filterKeys,
37
+ value,
38
+ fuzzy,
39
+ inputClassName,
40
+ ...inputProps
41
+ } = this . props // eslint-disable-line no-unused-vars
51
42
inputProps . type = inputProps . type || 'search'
52
43
inputProps . value = this . state . searchTerm
53
44
inputProps . onChange = this . updateSearch
@@ -58,33 +49,59 @@ const Search = createReactClass({
58
49
< input { ...inputProps } />
59
50
</ div >
60
51
)
61
- } ,
52
+ }
62
53
63
54
updateSearch ( e ) {
64
55
const searchTerm = e . target . value
65
- this . setState ( {
66
- searchTerm : searchTerm
67
- } , ( ) => {
68
- if ( this . _throttleTimeout ) {
69
- clearTimeout ( this . _throttleTimeout )
70
- }
56
+ this . setState (
57
+ {
58
+ searchTerm : searchTerm
59
+ } ,
60
+ ( ) => {
61
+ if ( this . _throttleTimeout ) {
62
+ clearTimeout ( this . _throttleTimeout )
63
+ }
71
64
72
- this . _throttleTimeout = setTimeout (
73
- ( ) => this . props . onChange ( searchTerm ) ,
74
- this . props . throttle
75
- )
76
- } )
77
- } ,
65
+ this . _throttleTimeout = setTimeout (
66
+ ( ) => this . props . onChange ( searchTerm ) ,
67
+ this . props . throttle
68
+ )
69
+ }
70
+ )
71
+ }
78
72
79
73
filter ( keys ) {
80
74
const { filterKeys, caseSensitive, fuzzy, sortResults} = this . props
81
- return createFilter (
82
- this . state . searchTerm ,
83
- keys || filterKeys ,
84
- { caseSensitive , fuzzy , sortResults}
85
- )
75
+ return createFilter ( this . state . searchTerm , keys || filterKeys , {
76
+ caseSensitive ,
77
+ fuzzy ,
78
+ sortResults
79
+ } )
86
80
}
87
- } )
81
+ }
82
+
83
+ Search . defaultProps = {
84
+ className : '' ,
85
+ onChange ( ) { } ,
86
+ caseSensitive : false ,
87
+ fuzzy : false ,
88
+ throttle : 200
89
+ }
90
+
91
+ Search . propTypes = {
92
+ className : PropTypes . string ,
93
+ inputClassName : PropTypes . string ,
94
+ onChange : PropTypes . func ,
95
+ caseSensitive : PropTypes . bool ,
96
+ sortResults : PropTypes . bool ,
97
+ fuzzy : PropTypes . bool ,
98
+ throttle : PropTypes . number ,
99
+ filterKeys : PropTypes . oneOf ( [
100
+ PropTypes . string ,
101
+ PropTypes . arrayOf ( PropTypes . string )
102
+ ] ) ,
103
+ value : PropTypes . string
104
+ }
88
105
89
106
export default Search
90
- export { createFilter }
107
+ export { createFilter }
0 commit comments