diff --git a/CHANGES.md b/CHANGES.md index 076d0ce..190088f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,7 @@ +Updated to [inputmask-core@2.0.0](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 [inputmask-core@1.2.0](https://github.com/insin/inputmask-core/blob/master/CHANGES.md#120--2015-03-26) diff --git a/package.json b/package.json index 883ce7c..6f2b63b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "react-maskedinput", "description": "Masked React component", - "version": "1.1.0", + "version": "2.0.0", "main": "./lib/index.js", "standalone": "MaskedInput", "homepage": "https://github.com/insin/react-maskedinput", @@ -14,7 +14,7 @@ "react-component" ], "dependencies": { - "inputmask-core": "^1.2.0" + "inputmask-core": "^2.0.0" }, "peerDependencies": { "react": ">=0.12.0" diff --git a/src/index.jsx b/src/index.jsx index a4b2020..0c8d6a1 100644 --- a/src/index.jsx +++ b/src/index.jsx @@ -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, @@ -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()