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,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