Skip to content
This repository was archived by the owner on Jul 19, 2019. It is now read-only.

Commit b54f378

Browse files
committed
Replace deprecated React.createClass with class syntax
1 parent 351c8e3 commit b54f378

File tree

4 files changed

+56
-52
lines changed

4 files changed

+56
-52
lines changed

examples/async-data/app.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ import DOM from 'react-dom'
33
import Autocomplete from '../../lib/index'
44
import { getStates, styles, fakeRequest } from '../../lib/utils'
55

6-
let App = React.createClass({
6+
class App extends React.Component {
77

8-
getInitialState() {
9-
return {
8+
state = {
109
value: '',
1110
unitedStates: getStates(),
1211
loading: false
1312
}
14-
},
1513

1614
render() {
1715
return (
@@ -53,7 +51,7 @@ let App = React.createClass({
5351
</div>
5452
)
5553
}
56-
})
54+
}
5755

5856
DOM.render(<App/>, document.getElementById('container'))
5957

examples/custom-menu/app.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@ import DOM from 'react-dom'
33
import Autocomplete from '../../lib/index'
44
import { getStates, styles, fakeRequest } from '../../lib/utils'
55

6-
let App = React.createClass({
6+
class App extends React.Component {
77

8-
getInitialState() {
9-
return {
8+
constructor(props) {
9+
super(props)
10+
this.state = {
1011
value: '',
1112
unitedStates: getStates(),
1213
loading: false
1314
}
14-
},
15+
this.renderItems = this.renderItems.bind(this)
16+
}
1517

1618
render() {
1719
return (
@@ -56,7 +58,7 @@ let App = React.createClass({
5658
/>
5759
</div>
5860
)
59-
},
61+
}
6062

6163
renderItems(items) {
6264
return items.map((item, index) => {
@@ -75,7 +77,7 @@ let App = React.createClass({
7577
}
7678
})
7779
}
78-
})
80+
}
7981

8082
DOM.render(<App/>, document.getElementById('container'))
8183

examples/static-data/app.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ import DOM from 'react-dom'
33
import { getStates, matchStateToTerm, sortStates, styles } from '../../lib/utils'
44
import Autocomplete from '../../lib/index'
55

6-
let App = React.createClass({
7-
getInitialState() {
8-
return { value: 'Ma' }
9-
},
6+
class App extends React.Component {
7+
state = { value: 'Ma' }
108
render() {
119
return (
1210
<div>
@@ -35,7 +33,7 @@ let App = React.createClass({
3533
</div>
3634
)
3735
}
38-
})
36+
}
3937

4038
DOM.render(<App/>, document.getElementById('container'))
4139

lib/Autocomplete.js

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ const IMPERATIVE_API = [
1515
'setRangeText',
1616
]
1717

18-
let Autocomplete = React.createClass({
18+
class Autocomplete extends React.Component {
1919

20-
propTypes: {
20+
static propTypes = {
2121
/**
2222
* The items to display in the dropdown menu
2323
*/
@@ -120,10 +120,9 @@ let Autocomplete = React.createClass({
120120
*/
121121
open: PropTypes.bool,
122122
debug: PropTypes.bool,
123-
},
123+
}
124124

125-
getDefaultProps() {
126-
return {
125+
static defaultProps = {
127126
value: '',
128127
wrapperProps: {},
129128
wrapperStyle: {
@@ -148,22 +147,29 @@ let Autocomplete = React.createClass({
148147
autoHighlight: true,
149148
onMenuVisibilityChange() {},
150149
}
151-
},
152150

153-
getInitialState() {
154-
return {
151+
constructor(props) {
152+
super(props)
153+
this.state = {
155154
isOpen: false,
156155
highlightedIndex: null,
157156
}
158-
},
157+
this.exposeAPI = this.exposeAPI.bind(this)
158+
this.handleInputFocus = this.handleInputFocus.bind(this)
159+
this.handleInputBlur = this.handleInputBlur.bind(this)
160+
this.handleChange = this.handleChange.bind(this)
161+
this.handleKeyDown = this.handleKeyDown.bind(this)
162+
this.handleKeyUp = this.handleKeyUp.bind(this)
163+
this.handleInputClick = this.handleInputClick.bind(this)
164+
}
159165

160166
componentWillMount() {
161167
// this.refs is frozen, so we need to assign a new object to it
162168
this.refs = {}
163169
this._ignoreBlur = false
164170
this._performAutoCompleteOnUpdate = false
165171
this._performAutoCompleteOnKeyUp = false
166-
},
172+
}
167173

168174
componentWillReceiveProps(nextProps) {
169175
this._performAutoCompleteOnUpdate = true
@@ -176,13 +182,13 @@ let Autocomplete = React.createClass({
176182
this.state.highlightedIndex >= nextProps.items.length) {
177183
this.setState({ highlightedIndex: null })
178184
}
179-
},
185+
}
180186

181187
componentDidMount() {
182188
if (this.isOpen()) {
183189
this.setMenuPositions()
184190
}
185-
},
191+
}
186192

187193
componentDidUpdate(prevProps, prevState) {
188194
if ((this.state.isOpen && !prevState.isOpen) || ('open' in this.props && this.props.open && !prevProps.open))
@@ -201,12 +207,12 @@ let Autocomplete = React.createClass({
201207
if (this._ignoreBlur) {
202208
this.refs.input.focus()
203209
}
204-
},
210+
}
205211

206212
exposeAPI(el) {
207213
this.refs.input = el
208214
IMPERATIVE_API.forEach(ev => this[ev] = (el && el[ev] && el[ev].bind(el)))
209-
},
215+
}
210216

211217
maybeScrollItemIntoView() {
212218
if (this.isOpen() && this.state.highlightedIndex !== null) {
@@ -220,32 +226,32 @@ let Autocomplete = React.createClass({
220226
)
221227
}
222228
}
223-
},
229+
}
224230

225231
handleKeyDown(event) {
226-
if (this.keyDownHandlers[event.key])
227-
this.keyDownHandlers[event.key].call(this, event)
232+
if (Autocomplete.keyDownHandlers[event.key])
233+
Autocomplete.keyDownHandlers[event.key].call(this, event)
228234
else if (!this.isOpen()) {
229235
this.setState({
230236
isOpen: true
231237
})
232238
}
233-
},
239+
}
234240

235241
handleChange(event) {
236242
this._performAutoCompleteOnKeyUp = true
237243
this.setState({ highlightedIndex: null })
238244
this.props.onChange(event, event.target.value)
239-
},
245+
}
240246

241247
handleKeyUp() {
242248
if (this._performAutoCompleteOnKeyUp) {
243249
this._performAutoCompleteOnKeyUp = false
244250
this.maybeAutoCompleteText()
245251
}
246-
},
252+
}
247253

248-
keyDownHandlers: {
254+
static keyDownHandlers = {
249255
ArrowDown(event) {
250256
event.preventDefault()
251257
const itemsLength = this.getFilteredItems().length
@@ -323,7 +329,7 @@ let Autocomplete = React.createClass({
323329
// In case the user is currently hovering over the menu
324330
this.setIgnoreBlur(false)
325331
},
326-
},
332+
}
327333

328334
getFilteredItems() {
329335
let items = this.props.items
@@ -341,7 +347,7 @@ let Autocomplete = React.createClass({
341347
}
342348

343349
return items
344-
},
350+
}
345351

346352
maybeAutoCompleteText() {
347353
if (!this.props.autoHighlight || this.props.value === '')
@@ -358,7 +364,7 @@ let Autocomplete = React.createClass({
358364
) === 0)
359365
if (itemValueDoesMatch && highlightedIndex === null)
360366
this.setState({ highlightedIndex: 0 })
361-
},
367+
}
362368

363369
setMenuPositions() {
364370
const node = this.refs.input
@@ -372,11 +378,11 @@ let Autocomplete = React.createClass({
372378
menuLeft: rect.left + marginLeft,
373379
menuWidth: rect.width + marginLeft + marginRight
374380
})
375-
},
381+
}
376382

377383
highlightItemFromMouse(index) {
378384
this.setState({ highlightedIndex: index })
379-
},
385+
}
380386

381387
selectItemFromMouse(item) {
382388
const value = this.props.getItemValue(item)
@@ -389,11 +395,11 @@ let Autocomplete = React.createClass({
389395
this.setIgnoreBlur(false)
390396
this.props.onSelect(value, item)
391397
})
392-
},
398+
}
393399

394400
setIgnoreBlur(ignore) {
395401
this._ignoreBlur = ignore
396-
},
402+
}
397403

398404
renderMenu() {
399405
const items = this.getFilteredItems().map((item, index) => {
@@ -420,7 +426,7 @@ let Autocomplete = React.createClass({
420426
onMouseEnter: () => this.setIgnoreBlur(true),
421427
onMouseLeave: () => this.setIgnoreBlur(false),
422428
})
423-
},
429+
}
424430

425431
handleInputBlur(event) {
426432
if (this._ignoreBlur) {
@@ -434,7 +440,7 @@ let Autocomplete = React.createClass({
434440
if (onBlur) {
435441
onBlur(event)
436442
}
437-
},
443+
}
438444

439445
handleInputFocus(event) {
440446
if (this._ignoreBlur) {
@@ -445,28 +451,28 @@ let Autocomplete = React.createClass({
445451
if (onFocus) {
446452
onFocus(event)
447453
}
448-
},
454+
}
449455

450456
isInputFocused() {
451457
const el = this.refs.input
452458
return el.ownerDocument && (el === el.ownerDocument.activeElement)
453-
},
459+
}
454460

455461
handleInputClick() {
456462
// Input will not be focused if it's disabled
457463
if (this.isInputFocused() && !this.isOpen())
458464
this.setState({ isOpen: true })
459-
},
465+
}
460466

461467
composeEventHandlers(internal, external) {
462468
return external
463469
? e => { internal(e); external(e) }
464470
: internal
465-
},
471+
}
466472

467473
isOpen() {
468474
return 'open' in this.props ? this.props.open : this.state.isOpen
469-
},
475+
}
470476

471477
render() {
472478
if (this.props.debug) { // you don't like it, you love it
@@ -504,7 +510,7 @@ let Autocomplete = React.createClass({
504510
</div>
505511
)
506512
}
507-
})
513+
}
508514

509515
module.exports = Autocomplete
510516

0 commit comments

Comments
 (0)