Skip to content

Commit 8e29020

Browse files
committed
createReactClass updated to es6 classes
1 parent cfafa66 commit 8e29020

File tree

4 files changed

+98
-76
lines changed

4 files changed

+98
-76
lines changed

README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,21 @@ npm install react-search-input --save
1717
## Example
1818

1919
```javascript
20+
import React, {Component} from 'react'
2021
import SearchInput, {createFilter} from 'react-search-input'
2122

2223
import emails from './mails'
2324

2425
const KEYS_TO_FILTERS = ['user.name', 'subject', 'dest.name']
2526

26-
const App = React.createClass({
27-
getInitialState () {
28-
return { searchTerm: '' }
29-
},
27+
class App extends Component {
28+
constructor (props) {
29+
super(props)
30+
this.state = {
31+
searchTerm: ''
32+
}
33+
this.searchUpdated = this.searchUpdated.bind(this)
34+
}
3035

3136
render () {
3237
const filteredEmails = emails.filter(createFilter(this.state.searchTerm, KEYS_TO_FILTERS))
@@ -44,12 +49,12 @@ const App = React.createClass({
4449
})}
4550
</div>
4651
)
47-
},
52+
}
4853

4954
searchUpdated (term) {
5055
this.setState({searchTerm: term})
5156
}
52-
})
57+
}
5358

5459
```
5560

example/src/app.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
import React from 'react'
2-
import ReactDOM from 'react-dom'
1+
import React, {Component} from 'react'
2+
import {render} from 'react-dom'
33

44
import SearchInput, {createFilter} from '../../lib/index'
55

66
import emails from './mails'
77

88
const KEYS_TO_FILTERS = ['user.name', 'subject', 'dest.name', 'id']
99

10-
const App = React.createClass({
11-
getInitialState () {
12-
return { searchTerm: '' }
13-
},
10+
class App extends Component {
11+
constructor (props) {
12+
super(props)
13+
this.state = {
14+
searchTerm: ''
15+
}
16+
this.searchUpdated = this.searchUpdated.bind(this)
17+
}
1418

1519
render () {
1620
const filteredEmails = emails.filter(createFilter(this.state.searchTerm, KEYS_TO_FILTERS))
@@ -28,11 +32,11 @@ const App = React.createClass({
2832
})}
2933
</div>
3034
)
31-
},
35+
}
3236

3337
searchUpdated (term) {
3438
this.setState({searchTerm: term})
3539
}
36-
})
40+
}
3741

38-
ReactDOM.render(<App />, document.getElementById('app'))
42+
render(<App />, document.getElementById('app'))

package.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
"description": "Simple react.js component for a search input, providing a filter function.",
55
"main": "lib/index.js",
66
"jsnext:main": "src/index.js",
7-
"files": [
8-
"lib",
9-
"src"
10-
],
7+
"files": ["lib", "src"],
118
"devDependencies": {
129
"babel-cli": "^6.8.0",
1310
"babel-jest": "^19.0.0",
@@ -46,7 +43,6 @@
4643
"homepage": "https://github.com/enkidevs/react-search-input",
4744
"dependencies": {
4845
"fuse.js": "^3.0.0",
49-
"prop-types": "^15.5.8",
50-
"create-react-class": "^15.5.2"
46+
"prop-types": "^15.5.8"
5147
}
5248
}

src/index.js

Lines changed: 72 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,44 @@
1-
import React from 'react'
1+
import React, {Component} from 'react'
22
import 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

89106
export default Search
90-
export { createFilter }
107+
export {createFilter}

0 commit comments

Comments
 (0)