Skip to content

Commit 86d6b59

Browse files
authored
Merge pull request insin#94 from krvital/master
Use class instead React.createClass
2 parents 8f3701d + e1fc6e2 commit 86d6b59

File tree

3 files changed

+68
-60
lines changed

3 files changed

+68
-60
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: 43 additions & 35 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,19 +58,15 @@ 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,
61+
class MaskedInput extends React.Component {
62+
constructor(props) {
63+
super(props)
6364

64-
formatCharacters: React.PropTypes.object,
65-
placeholderChar: React.PropTypes.string
66-
},
67-
68-
getDefaultProps() {
69-
return {
70-
value: ''
71-
}
72-
},
65+
this._onChange = this._onChange.bind(this)
66+
this._onKeyDown = this._onKeyDown.bind(this)
67+
this._onPaste = this._onPaste.bind(this)
68+
this._onKeyPress = this._onKeyPress.bind(this)
69+
}
7370

7471
componentWillMount() {
7572
var options = {
@@ -81,7 +78,7 @@ var MaskedInput = React.createClass({
8178
options.placeholderChar = this.props.placeholderChar
8279
}
8380
this.mask = new InputMask(options)
84-
},
81+
}
8582

8683
componentWillReceiveProps(nextProps) {
8784
if (this.props.mask !== nextProps.mask && this.props.value !== nextProps.mask) {
@@ -102,34 +99,34 @@ var MaskedInput = React.createClass({
10299
else if (this.props.value !== nextProps.value) {
103100
this.mask.setValue(nextProps.value)
104101
}
105-
},
102+
}
106103

107104
componentWillUpdate(nextProps, nextState) {
108105
if (nextProps.mask !== this.props.mask) {
109106
this._updatePattern(nextProps)
110107
}
111-
},
108+
}
112109

113110
componentDidUpdate(prevProps) {
114111
if (prevProps.mask !== this.props.mask && this.mask.selection.start) {
115112
this._updateInputSelection()
116113
}
117-
},
114+
}
118115

119-
_updatePattern: function(props) {
116+
_updatePattern(props) {
120117
this.mask.setPattern(props.mask, {
121118
value: this.mask.getRawValue(),
122119
selection: getSelection(this.input)
123120
})
124-
},
121+
}
125122

126123
_updateMaskSelection() {
127124
this.mask.selection = getSelection(this.input)
128-
},
125+
}
129126

130127
_updateInputSelection() {
131128
setSelection(this.input, this.mask.selection)
132-
},
129+
}
133130

134131
_onChange(e) {
135132
// console.log('onChange', JSON.stringify(getSelection(this.input)), e.target.value)
@@ -152,7 +149,7 @@ var MaskedInput = React.createClass({
152149
if (this.props.onChange) {
153150
this.props.onChange(e)
154151
}
155-
},
152+
}
156153

157154
_onKeyDown(e) {
158155
// console.log('onKeyDown', JSON.stringify(getSelection(this.input)), e.key, e.target.value)
@@ -194,7 +191,7 @@ var MaskedInput = React.createClass({
194191
}
195192
}
196193
}
197-
},
194+
}
198195

199196
_onKeyPress(e) {
200197
// console.log('onKeyPress', JSON.stringify(getSelection(this.input)), e.key, e.target.value)
@@ -212,7 +209,7 @@ var MaskedInput = React.createClass({
212209
this.props.onChange(e)
213210
}
214211
}
215-
},
212+
}
216213

217214
_onPaste(e) {
218215
// console.log('onPaste', JSON.stringify(getSelection(this.input)), e.clipboardData.getData('Text'), e.target.value)
@@ -228,12 +225,12 @@ var MaskedInput = React.createClass({
228225
this.props.onChange(e)
229226
}
230227
}
231-
},
228+
}
232229

233230
_getDisplayValue() {
234231
var value = this.mask.getValue()
235232
return value === this.mask.emptyValue ? '' : value
236-
},
233+
}
237234

238235
_keyPressPropName() {
239236
if (typeof navigator !== 'undefined') {
@@ -242,7 +239,7 @@ var MaskedInput = React.createClass({
242239
: 'onKeyPress'
243240
}
244241
return 'onKeyPress'
245-
},
242+
}
246243

247244
_getEventHandlers() {
248245
return {
@@ -251,27 +248,38 @@ var MaskedInput = React.createClass({
251248
onPaste: this._onPaste,
252249
[this._keyPressPropName()]: this._onKeyPress
253250
}
254-
},
251+
}
255252

256253
focus() {
257254
this.input.focus()
258-
},
255+
}
259256

260257
blur() {
261258
this.input.blur()
262-
},
259+
}
263260

264261
render() {
265-
var ref = r => this.input = r
262+
var ref = r => { this.input = r }
266263
var maxLength = this.mask.pattern.length
267264
var value = this._getDisplayValue()
268265
var eventHandlers = this._getEventHandlers()
269266
var { size = maxLength, placeholder = this.mask.emptyValue } = this.props
270267

271-
var {placeholderChar, formatCharacters, ...cleanedProps} = this.props
268+
var { placeholderChar, formatCharacters, ...cleanedProps } = this.props // eslint-disable-line
272269
var inputProps = { ...cleanedProps, ...eventHandlers, ref, maxLength, value, size, placeholder }
273270
return <input {...inputProps} />
274271
}
275-
})
272+
}
273+
274+
MaskedInput.propTypes = {
275+
mask: PropTypes.string.isRequired,
276+
277+
formatCharacters: PropTypes.object,
278+
placeholderChar: PropTypes.string
279+
}
280+
281+
MaskedInput.defaultProps = {
282+
value: ''
283+
}
276284

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

0 commit comments

Comments
 (0)