Skip to content

Commit

Permalink
Fix autofill scenarios by using data from onchange events
Browse files Browse the repository at this point in the history
Side effects:
  1) Delete behavior now allows removal of more than one character
  2) Cut now shifts remaining characters left instead of leaving "hole"

Also fix binding of this._updateInputSelection (cf. insin#110)
  • Loading branch information
dwbruhn committed Sep 28, 2017
1 parent f4cc837 commit b092989
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class MaskedInput extends React.Component {
this._onKeyDown = this._onKeyDown.bind(this)
this._onPaste = this._onPaste.bind(this)
this._onKeyPress = this._onKeyPress.bind(this)
this._updateInputSelection = this._updateInputSelection.bind(this)
}

componentWillMount() {
Expand Down Expand Up @@ -132,20 +133,14 @@ class MaskedInput extends React.Component {
// console.log('onChange', JSON.stringify(getSelection(this.input)), e.target.value)

var maskValue = this.mask.getValue()
if (e.target.value !== maskValue) {
// Cut or delete operations will have shortened the value
if (e.target.value.length < maskValue.length) {
var sizeDiff = maskValue.length - e.target.value.length
this._updateMaskSelection()
this.mask.selection.end = this.mask.selection.start + sizeDiff
this.mask.backspace()
}
var value = this._getDisplayValue()
e.target.value = value
if (value) {
this._updateInputSelection()
}
var incomingValue = e.target.value
if (incomingValue !== maskValue) { // only modify mask if form contents actually changed
this._updateMaskSelection()
this.mask.setValue(incomingValue) // write the whole updated value into the mask
e.target.value = this._getDisplayValue() // update the form with pattern applied to the value
this._updateInputSelection()
}

if (this.props.onChange) {
this.props.onChange(e)
}
Expand Down

0 comments on commit b092989

Please sign in to comment.