Skip to content

Commit b092989

Browse files
committed
Fix autofill scenarios by using data from onchange events
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)
1 parent f4cc837 commit b092989

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

src/index.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class MaskedInput extends React.Component {
6666
this._onKeyDown = this._onKeyDown.bind(this)
6767
this._onPaste = this._onPaste.bind(this)
6868
this._onKeyPress = this._onKeyPress.bind(this)
69+
this._updateInputSelection = this._updateInputSelection.bind(this)
6970
}
7071

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

134135
var maskValue = this.mask.getValue()
135-
if (e.target.value !== maskValue) {
136-
// Cut or delete operations will have shortened the value
137-
if (e.target.value.length < maskValue.length) {
138-
var sizeDiff = maskValue.length - e.target.value.length
139-
this._updateMaskSelection()
140-
this.mask.selection.end = this.mask.selection.start + sizeDiff
141-
this.mask.backspace()
142-
}
143-
var value = this._getDisplayValue()
144-
e.target.value = value
145-
if (value) {
146-
this._updateInputSelection()
147-
}
136+
var incomingValue = e.target.value
137+
if (incomingValue !== maskValue) { // only modify mask if form contents actually changed
138+
this._updateMaskSelection()
139+
this.mask.setValue(incomingValue) // write the whole updated value into the mask
140+
e.target.value = this._getDisplayValue() // update the form with pattern applied to the value
141+
this._updateInputSelection()
148142
}
143+
149144
if (this.props.onChange) {
150145
this.props.onChange(e)
151146
}

0 commit comments

Comments
 (0)