1- import React from 'react'
1+ import React , { Component } from 'react'
22import PropTypes from 'prop-types'
3- import createReactClass from 'create-react-class'
4- import { createFilter } from './util'
3+ import { createFilter } from './util'
54
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 = {
349 searchTerm : this . props . value || ''
3510 }
36- } ,
11+ this . updateSearch = this . updateSearch . bind ( this )
12+ this . filter = this . filter . bind ( this )
13+ }
3714
3815 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+ ) {
4020 const e = {
4121 target : {
4222 value : nextProps . value
4323 }
4424 }
4525 this . updateSearch ( e )
4626 }
47- } ,
27+ }
4828
4929 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
5142 inputProps . type = inputProps . type || 'search'
5243 inputProps . value = this . state . searchTerm
5344 inputProps . onChange = this . updateSearch
@@ -58,33 +49,59 @@ const Search = createReactClass({
5849 < input { ...inputProps } />
5950 </ div >
6051 )
61- } ,
52+ }
6253
6354 updateSearch ( e ) {
6455 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+ }
7164
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+ }
7872
7973 filter ( keys ) {
8074 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+ } )
8680 }
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+ }
88105
89106export default Search
90- export { createFilter }
107+ export { createFilter }
0 commit comments