@@ -2,8 +2,8 @@ import React from 'react'
22import PropTypes from 'prop-types'
33import InputMask from 'inputmask-core'
44
5- var KEYCODE_Z = 90
6- var KEYCODE_Y = 89
5+ let KEYCODE_Z = 90
6+ let KEYCODE_Y = 89
77
88function isUndo ( e ) {
99 return ( e . ctrlKey || e . metaKey ) && e . keyCode === ( e . shiftKey ? KEYCODE_Y : KEYCODE_Z )
@@ -14,17 +14,16 @@ function isRedo(e) {
1414}
1515
1616function getSelection ( el ) {
17- var start , end , rangeEl , clone
18-
17+ let start , end
1918 if ( el . selectionStart !== undefined ) {
2019 start = el . selectionStart
2120 end = el . selectionEnd
2221 }
2322 else {
2423 try {
2524 el . focus ( )
26- rangeEl = el . createTextRange ( )
27- clone = rangeEl . duplicate ( )
25+ let rangeEl = el . createTextRange ( )
26+ let clone = rangeEl . duplicate ( )
2827
2928 rangeEl . moveToBookmark ( document . selection . createRange ( ) . getBookmark ( ) )
3029 clone . setEndPoint ( 'EndToStart' , rangeEl )
@@ -39,16 +38,14 @@ function getSelection (el) {
3938}
4039
4140function setSelection ( el , selection ) {
42- var rangeEl
43-
4441 try {
4542 if ( el . selectionStart !== undefined ) {
4643 el . focus ( )
4744 el . setSelectionRange ( selection . start , selection . end )
4845 }
4946 else {
5047 el . focus ( )
51- rangeEl = el . createTextRange ( )
48+ let rangeEl = el . createTextRange ( )
5249 rangeEl . collapse ( true )
5350 rangeEl . moveStart ( 'character' , selection . start )
5451 rangeEl . moveEnd ( 'character' , selection . end - selection . start )
@@ -59,18 +56,19 @@ function setSelection(el, selection) {
5956}
6057
6158class MaskedInput extends React . Component {
62- constructor ( props ) {
63- super ( props )
64-
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- this . _updateInputSelection = this . _updateInputSelection . bind ( this )
59+ static propTypes = {
60+ mask : PropTypes . string . isRequired ,
61+
62+ formatCharacters : PropTypes . object ,
63+ placeholderChar : PropTypes . string
64+ }
65+
66+ static defaultProps = {
67+ value : ''
7068 }
7169
7270 componentWillMount ( ) {
73- var options = {
71+ let options = {
7472 pattern : this . props . mask ,
7573 value : this . props . value ,
7674 formatCharacters : this . props . formatCharacters
@@ -129,11 +127,11 @@ class MaskedInput extends React.Component {
129127 setSelection ( this . input , this . mask . selection )
130128 }
131129
132- _onChange ( e ) {
130+ _onChange = ( e ) => {
133131 // console.log('onChange', JSON.stringify(getSelection(this.input)), e.target.value)
134132
135- var maskValue = this . mask . getValue ( )
136- var incomingValue = e . target . value
133+ let maskValue = this . mask . getValue ( )
134+ let incomingValue = e . target . value
137135 if ( incomingValue !== maskValue ) { // only modify mask if form contents actually changed
138136 this . _updateMaskSelection ( )
139137 this . mask . setValue ( incomingValue ) // write the whole updated value into the mask
@@ -146,7 +144,7 @@ class MaskedInput extends React.Component {
146144 }
147145 }
148146
149- _onKeyDown ( e ) {
147+ _onKeyDown = ( e ) => {
150148 // console.log('onKeyDown', JSON.stringify(getSelection(this.input)), e.key, e.target.value)
151149
152150 if ( isUndo ( e ) ) {
@@ -176,7 +174,7 @@ class MaskedInput extends React.Component {
176174 e . preventDefault ( )
177175 this . _updateMaskSelection ( )
178176 if ( this . mask . backspace ( ) ) {
179- var value = this . _getDisplayValue ( )
177+ let value = this . _getDisplayValue ( )
180178 e . target . value = value
181179 if ( value ) {
182180 this . _updateInputSelection ( )
@@ -188,7 +186,7 @@ class MaskedInput extends React.Component {
188186 }
189187 }
190188
191- _onKeyPress ( e ) {
189+ _onKeyPress = ( e ) => {
192190 // console.log('onKeyPress', JSON.stringify(getSelection(this.input)), e.key, e.target.value)
193191
194192 // Ignore modified key presses
@@ -206,7 +204,7 @@ class MaskedInput extends React.Component {
206204 }
207205 }
208206
209- _onPaste ( e ) {
207+ _onPaste = ( e ) => {
210208 // console.log('onPaste', JSON.stringify(getSelection(this.input)), e.clipboardData.getData('Text'), e.target.value)
211209
212210 e . preventDefault ( )
@@ -215,15 +213,15 @@ class MaskedInput extends React.Component {
215213 if ( this . mask . paste ( e . clipboardData . getData ( 'Text' ) ) ) {
216214 e . target . value = this . mask . getValue ( )
217215 // Timeout needed for IE
218- setTimeout ( this . _updateInputSelection . bind ( this ) , 0 )
216+ setTimeout ( ( ) => this . _updateInputSelection ( ) , 0 )
219217 if ( this . props . onChange ) {
220218 this . props . onChange ( e )
221219 }
222220 }
223221 }
224222
225223 _getDisplayValue ( ) {
226- var value = this . mask . getValue ( )
224+ let value = this . mask . getValue ( )
227225 return value === this . mask . emptyValue ? '' : value
228226 }
229227
@@ -254,27 +252,16 @@ class MaskedInput extends React.Component {
254252 }
255253
256254 render ( ) {
257- var ref = r => { this . input = r }
258- var maxLength = this . mask . pattern . length
259- var value = this . _getDisplayValue ( )
260- var eventHandlers = this . _getEventHandlers ( )
261- var { size = maxLength , placeholder = this . mask . emptyValue } = this . props
262-
263- var { placeholderChar, formatCharacters, ...cleanedProps } = this . props // eslint-disable-line
264- var inputProps = { ...cleanedProps , ...eventHandlers , ref, maxLength, value, size, placeholder }
255+ let ref = r => { this . input = r }
256+ let maxLength = this . mask . pattern . length
257+ let value = this . _getDisplayValue ( )
258+ let eventHandlers = this . _getEventHandlers ( )
259+ let { size = maxLength , placeholder = this . mask . emptyValue } = this . props
260+
261+ let { placeholderChar, formatCharacters, ...cleanedProps } = this . props // eslint-disable-line no-unused-vars
262+ let inputProps = { ...cleanedProps , ...eventHandlers , ref, maxLength, value, size, placeholder }
265263 return < input { ...inputProps } />
266264 }
267265}
268266
269- MaskedInput . propTypes = {
270- mask : PropTypes . string . isRequired ,
271-
272- formatCharacters : PropTypes . object ,
273- placeholderChar : PropTypes . string
274- }
275-
276- MaskedInput . defaultProps = {
277- value : ''
278- }
279-
280267export default MaskedInput
0 commit comments