Skip to content

Commit

Permalink
Updated to inputmask-core 2.0.0
Browse files Browse the repository at this point in the history
Added undo/redo when Ctrl/Command + Z/Y are used
  • Loading branch information
insin committed Apr 7, 2015
1 parent a22fb9d commit e4a0032
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Updated to [[email protected]](https://github.com/insin/inputmask-core/blob/master/CHANGES.md#200--2015-04-03)

Added undo/redo when Ctrl/Command + Z/Y are used.

## 1.1.0 / 2015-03-26

Updated to [[email protected]](https://github.com/insin/inputmask-core/blob/master/CHANGES.md#120--2015-03-26)
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "react-maskedinput",
"description": "Masked <input/> React component",
"version": "1.1.0",
"version": "2.0.0",
"main": "./lib/index.js",
"standalone": "MaskedInput",
"homepage": "https://github.com/insin/react-maskedinput",
Expand All @@ -14,7 +14,7 @@
"react-component"
],
"dependencies": {
"inputmask-core": "^1.2.0"
"inputmask-core": "^2.0.0"
},
"peerDependencies": {
"react": ">=0.12.0"
Expand Down
30 changes: 30 additions & 0 deletions src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ var {getSelection, setSelection} = require('react/lib/ReactInputSelection')

var InputMask = require('inputmask-core')

var KEYCODE_Z = 90
var KEYCODE_Y = 89

function isUndo(e) {
return (e.ctrlKey || e.metaKey) && e.keyCode === (e.shiftKey ? KEYCODE_Y : KEYCODE_Z)
}

function isRedo(e) {
return (e.ctrlKey || e.metaKey) && e.keyCode === (e.shiftKey ? KEYCODE_Z : KEYCODE_Y)
}

var MaskedInput = React.createClass({
propTypes: {
pattern: React.PropTypes.string.isRequired,
Expand Down Expand Up @@ -60,6 +71,25 @@ var MaskedInput = React.createClass({
_onKeyDown(e) {
// console.log('onKeyDown', JSON.stringify(getSelection(this.getDOMNode())), e.key, e.target.value)

if (isUndo(e)) {
e.preventDefault()
if (this.mask.undo()) {
e.target.value = this._getDisplayValue()
this._updateInputSelection()
this.props.onChange(e)
}
return
}
else if (isRedo(e)) {
e.preventDefault()
if (this.mask.redo()) {
e.target.value = this._getDisplayValue()
this._updateInputSelection()
this.props.onChange(e)
}
return
}

if (e.key == 'Backspace') {
e.preventDefault()
this._updateMaskSelection()
Expand Down

0 comments on commit e4a0032

Please sign in to comment.