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
45var KEYCODE_Z = 90
56var 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