Skip to content

Commit 09472b8

Browse files
author
Vital Kravtsov
committed
Use class instead React.createClass and use prop-types package instead React.PropTypes to prevent deprecation warning fix insin#70 fix insin#93
1 parent 8f3701d commit 09472b8

File tree

3 files changed

+61
-62
lines changed

3 files changed

+61
-62
lines changed

demo/src/index.js

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ const PATTERNS = [
1212
'1 1'
1313
]
1414

15-
const App = React.createClass({
16-
getInitialState() {
17-
return {
15+
class App extends React.Component {
16+
constructor(props) {
17+
super(props)
18+
19+
this.state = {
1820
card: '',
1921
expiry: '',
2022
ccv: '',
@@ -26,25 +28,25 @@ const App = React.createClass({
2628
pattern: '1111 1111',
2729
cardPattern: '1111 1111 1111 1111'
2830
}
29-
},
31+
}
3032

3133
_onChange(e) {
3234
const stateChange = {}
3335
stateChange[e.target.name] = e.target.value
3436
this.setState(stateChange)
35-
},
37+
}
3638

3739
_changePattern(e) {
3840
this.setState({pattern: e.target.value})
39-
},
41+
}
4042

4143
_onCardChange(e) {
4244
if(/^3[47]/.test(e.target.value)) {
4345
this.setState({cardPattern: "1111 111111 11111"})
4446
} else {
4547
this.setState({cardPattern: '1111 1111 1111 1111'})
4648
}
47-
},
49+
}
4850

4951
render() {
5052
return <div className="App">
@@ -111,24 +113,21 @@ const App = React.createClass({
111113
<footer><a href="https://github.com/insin/react-maskedinput">Source on GitHub</a></footer>
112114
</div>
113115
}
114-
})
116+
}
115117

116-
const CustomInput = React.createClass({
117-
render() {
118-
return <MaskedInput
119-
mask="1111-WW-11"
120-
placeholder="1234-WW-12"
121-
placeholderChar=" "
122-
size="11"
123-
{...this.props}
124-
formatCharacters={{
125-
'W': {
126-
validate(char) { return /\w/.test(char) },
127-
transform(char) { return char.toUpperCase() }
128-
}
118+
const CustomInput = (props) =>
119+
<MaskedInput
120+
mask="1111-WW-11"
121+
placeholder="1234-WW-12"
122+
placeholderChar=" "
123+
size="11"
124+
{...props}
125+
formatCharacters={{
126+
'W': {
127+
validate(char) { return /\w/.test(char) },
128+
transform(char) { return char.toUpperCase() }
129129
}
130-
}/>
131-
}
132-
})
130+
}}
131+
/>
133132

134133
render(<App/>, document.getElementById('demo'))

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"inputmask-core": "^2.1.1"
3333
},
3434
"peerDependencies": {
35-
"react": "0.14.x || 15.x.x"
35+
"react": "0.14.x || 15.x.x",
36+
"prop-types": "^15.5.8"
3637
},
3738
"devDependencies": {
3839
"eslint-config-jonnybuchanan": "2.0.3",

src/index.js

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
var React = require('react')
2-
var InputMask = require('inputmask-core')
1+
import React from 'react'
2+
import PropTypes from 'prop-types'
3+
import InputMask from 'inputmask-core'
34

45
var KEYCODE_Z = 90
56
var KEYCODE_Y = 89
@@ -57,20 +58,7 @@ function setSelection(el, selection) {
5758
catch (e) { /* not focused or not visible */ }
5859
}
5960

60-
var MaskedInput = React.createClass({
61-
propTypes: {
62-
mask: React.PropTypes.string.isRequired,
63-
64-
formatCharacters: React.PropTypes.object,
65-
placeholderChar: React.PropTypes.string
66-
},
67-
68-
getDefaultProps() {
69-
return {
70-
value: ''
71-
}
72-
},
73-
61+
class MaskedInput extends React.Component {
7462
componentWillMount() {
7563
var options = {
7664
pattern: this.props.mask,
@@ -81,7 +69,7 @@ var MaskedInput = React.createClass({
8169
options.placeholderChar = this.props.placeholderChar
8270
}
8371
this.mask = new InputMask(options)
84-
},
72+
}
8573

8674
componentWillReceiveProps(nextProps) {
8775
if (this.props.mask !== nextProps.mask && this.props.value !== nextProps.mask) {
@@ -102,34 +90,34 @@ var MaskedInput = React.createClass({
10290
else if (this.props.value !== nextProps.value) {
10391
this.mask.setValue(nextProps.value)
10492
}
105-
},
93+
}
10694

10795
componentWillUpdate(nextProps, nextState) {
10896
if (nextProps.mask !== this.props.mask) {
10997
this._updatePattern(nextProps)
11098
}
111-
},
99+
}
112100

113101
componentDidUpdate(prevProps) {
114102
if (prevProps.mask !== this.props.mask && this.mask.selection.start) {
115103
this._updateInputSelection()
116104
}
117-
},
105+
}
118106

119-
_updatePattern: function(props) {
107+
_updatePattern(props) {
120108
this.mask.setPattern(props.mask, {
121109
value: this.mask.getRawValue(),
122110
selection: getSelection(this.input)
123111
})
124-
},
112+
}
125113

126114
_updateMaskSelection() {
127115
this.mask.selection = getSelection(this.input)
128-
},
116+
}
129117

130118
_updateInputSelection() {
131119
setSelection(this.input, this.mask.selection)
132-
},
120+
}
133121

134122
_onChange(e) {
135123
// console.log('onChange', JSON.stringify(getSelection(this.input)), e.target.value)
@@ -152,7 +140,7 @@ var MaskedInput = React.createClass({
152140
if (this.props.onChange) {
153141
this.props.onChange(e)
154142
}
155-
},
143+
}
156144

157145
_onKeyDown(e) {
158146
// console.log('onKeyDown', JSON.stringify(getSelection(this.input)), e.key, e.target.value)
@@ -194,7 +182,7 @@ var MaskedInput = React.createClass({
194182
}
195183
}
196184
}
197-
},
185+
}
198186

199187
_onKeyPress(e) {
200188
// console.log('onKeyPress', JSON.stringify(getSelection(this.input)), e.key, e.target.value)
@@ -212,7 +200,7 @@ var MaskedInput = React.createClass({
212200
this.props.onChange(e)
213201
}
214202
}
215-
},
203+
}
216204

217205
_onPaste(e) {
218206
// console.log('onPaste', JSON.stringify(getSelection(this.input)), e.clipboardData.getData('Text'), e.target.value)
@@ -228,12 +216,12 @@ var MaskedInput = React.createClass({
228216
this.props.onChange(e)
229217
}
230218
}
231-
},
219+
}
232220

233221
_getDisplayValue() {
234222
var value = this.mask.getValue()
235223
return value === this.mask.emptyValue ? '' : value
236-
},
224+
}
237225

238226
_keyPressPropName() {
239227
if (typeof navigator !== 'undefined') {
@@ -242,7 +230,7 @@ var MaskedInput = React.createClass({
242230
: 'onKeyPress'
243231
}
244232
return 'onKeyPress'
245-
},
233+
}
246234

247235
_getEventHandlers() {
248236
return {
@@ -251,27 +239,38 @@ var MaskedInput = React.createClass({
251239
onPaste: this._onPaste,
252240
[this._keyPressPropName()]: this._onKeyPress
253241
}
254-
},
242+
}
255243

256244
focus() {
257245
this.input.focus()
258-
},
246+
}
259247

260248
blur() {
261249
this.input.blur()
262-
},
250+
}
263251

264252
render() {
265-
var ref = r => this.input = r
253+
var ref = r => { this.input = r }
266254
var maxLength = this.mask.pattern.length
267255
var value = this._getDisplayValue()
268256
var eventHandlers = this._getEventHandlers()
269257
var { size = maxLength, placeholder = this.mask.emptyValue } = this.props
270258

271-
var {placeholderChar, formatCharacters, ...cleanedProps} = this.props
259+
var { placeholderChar, formatCharacters, ...cleanedProps } = this.props // eslint-disable-line
272260
var inputProps = { ...cleanedProps, ...eventHandlers, ref, maxLength, value, size, placeholder }
273261
return <input {...inputProps} />
274262
}
275-
})
263+
}
264+
265+
MaskedInput.propTypes = {
266+
mask: PropTypes.string.isRequired,
267+
268+
formatCharacters: PropTypes.object,
269+
placeholderChar: PropTypes.string
270+
}
271+
272+
MaskedInput.defaultProps = {
273+
value: ''
274+
}
276275

277-
module.exports = MaskedInput
276+
export default MaskedInput

0 commit comments

Comments
 (0)