Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix autofill scenarios by using data from onchange events #112

Merged
merged 1 commit into from
Oct 20, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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