UNPKG

react-maskedinput-autofill

Version:
1,438 lines (1,261 loc) 60.9 kB
/*! * react-maskedinput-autofill 3.3.6 - https://github.com/aesopwolf/react-maskedinput * MIT Licensed */ (function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(require("react")); else if(typeof define === 'function' && define.amd) define(["react"], factory); else if(typeof exports === 'object') exports["MaskedInput"] = factory(require("react")); else root["MaskedInput"] = factory(root["React"]); })(this, function(__WEBPACK_EXTERNAL_MODULE_9__) { return /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) /******/ return installedModules[moduleId].exports; /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ exports: {}, /******/ id: moduleId, /******/ loaded: false /******/ }; /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ // Flag the module as loaded /******/ module.loaded = true; /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ // Load entry module and return exports /******/ return __webpack_require__(0); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ function(module, exports, __webpack_require__) { 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var PropTypes = __webpack_require__(8); var React = __webpack_require__(9); var InputMask = __webpack_require__(5); 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); } function getSelection(el) { var start, end, rangeEl, clone; if (el.selectionStart !== undefined) { start = el.selectionStart; end = el.selectionEnd; } else { try { el.focus(); rangeEl = el.createTextRange(); clone = rangeEl.duplicate(); rangeEl.moveToBookmark(document.selection.createRange().getBookmark()); clone.setEndPoint('EndToStart', rangeEl); start = clone.text.length; end = start + rangeEl.text.length; } catch (e) {/* not focused or not visible */} } return { start: start, end: end }; } function setSelection(el, selection) { var rangeEl; try { if (el.selectionStart !== undefined) { el.focus(); el.setSelectionRange(selection.start, selection.end); } else { el.focus(); rangeEl = el.createTextRange(); rangeEl.collapse(true); rangeEl.moveStart('character', selection.start); rangeEl.moveEnd('character', selection.end - selection.start); rangeEl.select(); } } catch (e) {/* not focused or not visible */} } var MaskedInput = (function (_React$Component) { _inherits(MaskedInput, _React$Component); function MaskedInput(props) { _classCallCheck(this, MaskedInput); _get(Object.getPrototypeOf(MaskedInput.prototype), 'constructor', this).call(this, props); this.updatePattern = this.updatePattern.bind(this); this.updateMaskSelection = this.updateMaskSelection.bind(this); this.updateInputSelection = this.updateInputSelection.bind(this); this.onChange = this.onChange.bind(this); this.onKeyDown = this.onKeyDown.bind(this); this.onKeyPress = this.onKeyPress.bind(this); this.onPaste = this.onPaste.bind(this); this.getDisplayValue = this.getDisplayValue.bind(this); this.keyPressPropName = this.keyPressPropName.bind(this); this.getEventHandlers = this.getEventHandlers.bind(this); } _createClass(MaskedInput, [{ key: 'componentWillMount', value: function componentWillMount() { var options = { pattern: this.props.mask, value: this.props.value, formatCharacters: this.props.formatCharacters }; if (this.props.placeholderChar) { options.placeholderChar = this.props.placeholderChar; } this.mask = new InputMask(options); } }, { key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { if (this.props.mask !== nextProps.mask && this.props.value !== nextProps.mask) { // if we get a new value and a new mask at the same time // check if the mask.value is still the initial value // - if so use the nextProps value // - otherwise the `this.mask` has a value for us (most likely from paste action) if (this.mask.getValue() === this.mask.emptyValue) { this.mask.setPattern(nextProps.mask, { value: nextProps.value }); } else { this.mask.setPattern(nextProps.mask, { value: this.mask.getRawValue() }); } } else if (this.props.mask !== nextProps.mask) { this.mask.setPattern(nextProps.mask, { value: this.mask.getRawValue() }); } else if (this.props.value !== nextProps.value) { this.mask.setValue(nextProps.value); } } }, { key: 'componentWillUpdate', value: function componentWillUpdate(nextProps, nextState) { if (nextProps.mask !== this.props.mask) { this.updatePattern(nextProps); } } }, { key: 'componentDidUpdate', value: function componentDidUpdate(prevProps) { if (prevProps.mask !== this.props.mask && this.mask.selection.start) { this.updateInputSelection(); } } }, { key: 'updatePattern', value: function updatePattern(props) { this.mask.setPattern(props.mask, { value: this.mask.getRawValue(), selection: getSelection(this.input) }); } }, { key: 'updateMaskSelection', value: function updateMaskSelection() { this.mask.selection = getSelection(this.input); } }, { key: 'updateInputSelection', value: function updateInputSelection() { setSelection(this.input, this.mask.selection); } }, { key: 'onChange', value: function onChange(e) { // 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(); } this.mask.setValue(e.target.value); var value = this.getDisplayValue(); e.target.value = value; if (value) { this.updateInputSelection(); } } if (this.props.onChange) { this.props.onChange(e); } } }, { key: 'onKeyDown', value: function onKeyDown(e) { // console.log('onKeyDown', JSON.stringify(getSelection(this.input)), e.key, e.target.value) if (isUndo(e)) { e.preventDefault(); if (this.mask.undo()) { e.target.value = this.getDisplayValue(); this.updateInputSelection(); if (this.props.onChange) { this.props.onChange(e); } } return; } else if (isRedo(e)) { e.preventDefault(); if (this.mask.redo()) { e.target.value = this.getDisplayValue(); this.updateInputSelection(); if (this.props.onChange) { this.props.onChange(e); } } return; } if (e.key === 'Backspace') { e.preventDefault(); this.updateMaskSelection(); if (this.mask.backspace()) { var value = this.getDisplayValue(); e.target.value = value; if (value) { this.updateInputSelection(); } if (this.props.onChange) { this.props.onChange(e); } } } } }, { key: 'onKeyPress', value: function onKeyPress(e) { // console.log('onKeyPress', JSON.stringify(getSelection(this.input)), e.key, e.target.value) // Ignore modified key presses // Ignore enter key to allow form submission if (e.metaKey || e.altKey || e.ctrlKey || e.key === 'Enter') { return; } e.preventDefault(); this.updateMaskSelection(); if (this.mask.input(e.key || e.data)) { e.target.value = this.mask.getValue(); this.updateInputSelection(); if (this.props.onChange) { this.props.onChange(e); } } } }, { key: 'onPaste', value: function onPaste(e) { // console.log('onPaste', JSON.stringify(getSelection(this.input)), e.clipboardData.getData('Text'), e.target.value) e.preventDefault(); this.updateMaskSelection(); // getData value needed for IE also works in FF & Chrome if (this.mask.paste(e.clipboardData.getData('Text'))) { e.target.value = this.mask.getValue(); // Timeout needed for IE setTimeout(this.updateInputSelection, 0); if (this.props.onChange) { this.props.onChange(e); } } else { this.mask.setValue(e.clipboardData.getData('Text')); var value = this.getDisplayValue(); e.target.value = value; if (value) { this.updateInputSelection(); } } } }, { key: 'getDisplayValue', value: function getDisplayValue() { var value = this.mask.getValue(); return value === this.mask.emptyValue ? '' : value; } }, { key: 'keyPressPropName', value: function keyPressPropName() { if (typeof navigator !== 'undefined') { return navigator.userAgent.match(/Android/i) ? 'onBeforeInput' : 'onKeyPress'; } return 'onKeyPress'; } }, { key: 'getEventHandlers', value: function getEventHandlers() { return _defineProperty({ onChange: this.onChange, onKeyDown: this.onKeyDown, onPaste: this.onPaste }, this.keyPressPropName(), this.onKeyPress); } }, { key: 'focus', value: function focus() { this.input.focus(); } }, { key: 'blur', value: function blur() { this.input.blur(); } }, { key: 'render', value: function render() { var _this = this; var ref = function ref(r) { return _this.input = r; }; var maxLength = this.mask.pattern.length; var value = this.getDisplayValue(); var eventHandlers = this.getEventHandlers(); var _props = this.props; var _props$size = _props.size; var size = _props$size === undefined ? maxLength : _props$size; var _props$placeholder = _props.placeholder; var placeholder = _props$placeholder === undefined ? this.mask.emptyValue : _props$placeholder; var _props2 = this.props; var placeholderChar = _props2.placeholderChar; var formatCharacters = _props2.formatCharacters; var cleanedProps = _objectWithoutProperties(_props2, ['placeholderChar', 'formatCharacters']); var inputProps = _extends({}, cleanedProps, eventHandlers, { ref: ref, maxLength: maxLength, value: value, size: size, placeholder: placeholder }); return React.createElement('input', inputProps); } }]); return MaskedInput; })(React.Component); MaskedInput.propTypes = { mask: PropTypes.string.isRequired, formatCharacters: PropTypes.object, onChange: PropTypes.func, placeholderChar: PropTypes.string, value: PropTypes.string }; MaskedInput.defaultProps = { formatCharacters: null, onChange: function onChange() {}, placeholderChar: null, value: '' }; exports['default'] = MaskedInput; module.exports = exports['default']; /***/ }, /* 1 */ /***/ function(module, exports) { "use strict"; /** * Copyright (c) 2013-present, Facebook, Inc. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. * * */ function makeEmptyFunction(arg) { return function () { return arg; }; } /** * This function accepts and discards inputs; it has no side effects. This is * primarily useful idiomatically for overridable function endpoints which * always need to be callable, since JS lacks a null-call idiom ala Cocoa. */ var emptyFunction = function emptyFunction() {}; emptyFunction.thatReturns = makeEmptyFunction; emptyFunction.thatReturnsFalse = makeEmptyFunction(false); emptyFunction.thatReturnsTrue = makeEmptyFunction(true); emptyFunction.thatReturnsNull = makeEmptyFunction(null); emptyFunction.thatReturnsThis = function () { return this; }; emptyFunction.thatReturnsArgument = function (arg) { return arg; }; module.exports = emptyFunction; /***/ }, /* 2 */ /***/ function(module, exports, __webpack_require__) { /** * Copyright (c) 2013-present, Facebook, Inc. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. * */ 'use strict'; /** * Use invariant() to assert state which your program assumes to be true. * * Provide sprintf-style format (only %s is supported) and arguments * to provide information about what broke and what you were * expecting. * * The invariant message will be stripped in production, but the invariant * will remain to ensure logic does not differ in production. */ var validateFormat = function validateFormat(format) {}; if (true) { validateFormat = function validateFormat(format) { if (format === undefined) { throw new Error('invariant requires an error message argument'); } }; } function invariant(condition, format, a, b, c, d, e, f) { validateFormat(format); if (!condition) { var error; if (format === undefined) { error = new Error('Minified exception occurred; use the non-minified dev environment ' + 'for the full error message and additional helpful warnings.'); } else { var args = [a, b, c, d, e, f]; var argIndex = 0; error = new Error(format.replace(/%s/g, function () { return args[argIndex++]; })); error.name = 'Invariant Violation'; } error.framesToPop = 1; // we don't care about invariant's own frame throw error; } } module.exports = invariant; /***/ }, /* 3 */ /***/ function(module, exports, __webpack_require__) { /** * Copyright 2014-2015, Facebook, Inc. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. * */ 'use strict'; var emptyFunction = __webpack_require__(1); /** * Similar to invariant but only logs a warning if the condition is not met. * This can be used to log issues in development environments in critical * paths. Removing the logging code for production environments will keep the * same logic and follow the same code paths. */ var warning = emptyFunction; if (true) { (function () { var printWarning = function printWarning(format) { for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { args[_key - 1] = arguments[_key]; } var argIndex = 0; var message = 'Warning: ' + format.replace(/%s/g, function () { return args[argIndex++]; }); if (typeof console !== 'undefined') { console.error(message); } try { // --- Welcome to debugging React --- // This error was thrown as a convenience so that you can use this stack // to find the callsite that caused this warning to fire. throw new Error(message); } catch (x) {} }; warning = function warning(condition, format) { if (format === undefined) { throw new Error('`warning(condition, format, ...args)` requires a warning ' + 'message argument'); } if (format.indexOf('Failed Composite propType: ') === 0) { return; // Ignore CompositeComponent proptype check. } if (!condition) { for (var _len2 = arguments.length, args = Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) { args[_key2 - 2] = arguments[_key2]; } printWarning.apply(undefined, [format].concat(args)); } }; })(); } module.exports = warning; /***/ }, /* 4 */ /***/ function(module, exports) { /** * Copyright 2013-present, Facebook, Inc. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. */ 'use strict'; var ReactPropTypesSecret = 'SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED'; module.exports = ReactPropTypesSecret; /***/ }, /* 5 */ /***/ function(module, exports) { 'use strict' function extend(dest, src) { if (src) { var props = Object.keys(src) for (var i = 0, l = props.length; i < l ; i++) { dest[props[i]] = src[props[i]] } } return dest } function copy(obj) { return extend({}, obj) } /** * Merge an object defining format characters into the defaults. * Passing null/undefined for en existing format character removes it. * Passing a definition for an existing format character overrides it. * @param {?Object} formatCharacters. */ function mergeFormatCharacters(formatCharacters) { var merged = copy(DEFAULT_FORMAT_CHARACTERS) if (formatCharacters) { var chars = Object.keys(formatCharacters) for (var i = 0, l = chars.length; i < l ; i++) { var char = chars[i] if (formatCharacters[char] == null) { delete merged[char] } else { merged[char] = formatCharacters[char] } } } return merged } var ESCAPE_CHAR = '\\' var DIGIT_RE = /^\d$/ var LETTER_RE = /^[A-Za-z]$/ var ALPHANNUMERIC_RE = /^[\dA-Za-z]$/ var DEFAULT_PLACEHOLDER_CHAR = '_' var DEFAULT_FORMAT_CHARACTERS = { '*': { validate: function(char) { return ALPHANNUMERIC_RE.test(char) } }, '1': { validate: function(char) { return DIGIT_RE.test(char) } }, 'a': { validate: function(char) { return LETTER_RE.test(char) } }, 'A': { validate: function(char) { return LETTER_RE.test(char) }, transform: function(char) { return char.toUpperCase() } }, '#': { validate: function(char) { return ALPHANNUMERIC_RE.test(char) }, transform: function(char) { return char.toUpperCase() } } } /** * @param {string} source * @patam {?Object} formatCharacters */ function Pattern(source, formatCharacters, placeholderChar, isRevealingMask) { if (!(this instanceof Pattern)) { return new Pattern(source, formatCharacters, placeholderChar) } /** Placeholder character */ this.placeholderChar = placeholderChar || DEFAULT_PLACEHOLDER_CHAR /** Format character definitions. */ this.formatCharacters = formatCharacters || DEFAULT_FORMAT_CHARACTERS /** Pattern definition string with escape characters. */ this.source = source /** Pattern characters after escape characters have been processed. */ this.pattern = [] /** Length of the pattern after escape characters have been processed. */ this.length = 0 /** Index of the first editable character. */ this.firstEditableIndex = null /** Index of the last editable character. */ this.lastEditableIndex = null /** Lookup for indices of editable characters in the pattern. */ this._editableIndices = {} /** If true, only the pattern before the last valid value character shows. */ this.isRevealingMask = isRevealingMask || false this._parse() } Pattern.prototype._parse = function parse() { var sourceChars = this.source.split('') var patternIndex = 0 var pattern = [] for (var i = 0, l = sourceChars.length; i < l; i++) { var char = sourceChars[i] if (char === ESCAPE_CHAR) { if (i === l - 1) { throw new Error('InputMask: pattern ends with a raw ' + ESCAPE_CHAR) } char = sourceChars[++i] } else if (char in this.formatCharacters) { if (this.firstEditableIndex === null) { this.firstEditableIndex = patternIndex } this.lastEditableIndex = patternIndex this._editableIndices[patternIndex] = true } pattern.push(char) patternIndex++ } if (this.firstEditableIndex === null) { throw new Error( 'InputMask: pattern "' + this.source + '" does not contain any editable characters.' ) } this.pattern = pattern this.length = pattern.length } /** * @param {Array<string>} value * @return {Array<string>} */ Pattern.prototype.formatValue = function format(value) { var valueBuffer = new Array(this.length) var valueIndex = 0 for (var i = 0, l = this.length; i < l ; i++) { if (this.isEditableIndex(i)) { if (this.isRevealingMask && value.length <= valueIndex && !this.isValidAtIndex(value[valueIndex], i)) { break } valueBuffer[i] = (value.length > valueIndex && this.isValidAtIndex(value[valueIndex], i) ? this.transform(value[valueIndex], i) : this.placeholderChar) valueIndex++ } else { valueBuffer[i] = this.pattern[i] // Also allow the value to contain static values from the pattern by // advancing its index. if (value.length > valueIndex && value[valueIndex] === this.pattern[i]) { valueIndex++ } } } return valueBuffer } /** * @param {number} index * @return {boolean} */ Pattern.prototype.isEditableIndex = function isEditableIndex(index) { return !!this._editableIndices[index] } /** * @param {string} char * @param {number} index * @return {boolean} */ Pattern.prototype.isValidAtIndex = function isValidAtIndex(char, index) { return this.formatCharacters[this.pattern[index]].validate(char) } Pattern.prototype.transform = function transform(char, index) { var format = this.formatCharacters[this.pattern[index]] return typeof format.transform == 'function' ? format.transform(char) : char } function InputMask(options) { if (!(this instanceof InputMask)) { return new InputMask(options) } options = extend({ formatCharacters: null, pattern: null, isRevealingMask: false, placeholderChar: DEFAULT_PLACEHOLDER_CHAR, selection: {start: 0, end: 0}, value: '' }, options) if (options.pattern == null) { throw new Error('InputMask: you must provide a pattern.') } if (typeof options.placeholderChar !== 'string' || options.placeholderChar.length > 1) { throw new Error('InputMask: placeholderChar should be a single character or an empty string.') } this.placeholderChar = options.placeholderChar this.formatCharacters = mergeFormatCharacters(options.formatCharacters) this.setPattern(options.pattern, { value: options.value, selection: options.selection, isRevealingMask: options.isRevealingMask }) } // Editing /** * Applies a single character of input based on the current selection. * @param {string} char * @return {boolean} true if a change has been made to value or selection as a * result of the input, false otherwise. */ InputMask.prototype.input = function input(char) { // Ignore additional input if the cursor's at the end of the pattern if (this.selection.start === this.selection.end && this.selection.start === this.pattern.length) { return false } var selectionBefore = copy(this.selection) var valueBefore = this.getValue() var inputIndex = this.selection.start // If the cursor or selection is prior to the first editable character, make // sure any input given is applied to it. if (inputIndex < this.pattern.firstEditableIndex) { inputIndex = this.pattern.firstEditableIndex } // Bail out or add the character to input if (this.pattern.isEditableIndex(inputIndex)) { if (!this.pattern.isValidAtIndex(char, inputIndex)) { return false } this.value[inputIndex] = this.pattern.transform(char, inputIndex) } // If multiple characters were selected, blank the remainder out based on the // pattern. var end = this.selection.end - 1 while (end > inputIndex) { if (this.pattern.isEditableIndex(end)) { this.value[end] = this.placeholderChar } end-- } // Advance the cursor to the next character this.selection.start = this.selection.end = inputIndex + 1 // Skip over any subsequent static characters while (this.pattern.length > this.selection.start && !this.pattern.isEditableIndex(this.selection.start)) { this.selection.start++ this.selection.end++ } // History if (this._historyIndex != null) { // Took more input after undoing, so blow any subsequent history away this._history.splice(this._historyIndex, this._history.length - this._historyIndex) this._historyIndex = null } if (this._lastOp !== 'input' || selectionBefore.start !== selectionBefore.end || this._lastSelection !== null && selectionBefore.start !== this._lastSelection.start) { this._history.push({value: valueBefore, selection: selectionBefore, lastOp: this._lastOp}) } this._lastOp = 'input' this._lastSelection = copy(this.selection) return true } /** * Attempts to delete from the value based on the current cursor position or * selection. * @return {boolean} true if the value or selection changed as the result of * backspacing, false otherwise. */ InputMask.prototype.backspace = function backspace() { // If the cursor is at the start there's nothing to do if (this.selection.start === 0 && this.selection.end === 0) { return false } var selectionBefore = copy(this.selection) var valueBefore = this.getValue() // No range selected - work on the character preceding the cursor if (this.selection.start === this.selection.end) { if (this.pattern.isEditableIndex(this.selection.start - 1)) { this.value[this.selection.start - 1] = this.placeholderChar } this.selection.start-- this.selection.end-- } // Range selected - delete characters and leave the cursor at the start of the selection else { var end = this.selection.end - 1 while (end >= this.selection.start) { if (this.pattern.isEditableIndex(end)) { this.value[end] = this.placeholderChar } end-- } this.selection.end = this.selection.start } // History if (this._historyIndex != null) { // Took more input after undoing, so blow any subsequent history away this._history.splice(this._historyIndex, this._history.length - this._historyIndex) } if (this._lastOp !== 'backspace' || selectionBefore.start !== selectionBefore.end || this._lastSelection !== null && selectionBefore.start !== this._lastSelection.start) { this._history.push({value: valueBefore, selection: selectionBefore, lastOp: this._lastOp}) } this._lastOp = 'backspace' this._lastSelection = copy(this.selection) return true } /** * Attempts to paste a string of input at the current cursor position or over * the top of the current selection. * Invalid content at any position will cause the paste to be rejected, and it * may contain static parts of the mask's pattern. * @param {string} input * @return {boolean} true if the paste was successful, false otherwise. */ InputMask.prototype.paste = function paste(input) { // This is necessary because we're just calling input() with each character // and rolling back if any were invalid, rather than checking up-front. var initialState = { value: this.value.slice(), selection: copy(this.selection), _lastOp: this._lastOp, _history: this._history.slice(), _historyIndex: this._historyIndex, _lastSelection: copy(this._lastSelection) } // If there are static characters at the start of the pattern and the cursor // or selection is within them, the static characters must match for a valid // paste. if (this.selection.start < this.pattern.firstEditableIndex) { for (var i = 0, l = this.pattern.firstEditableIndex - this.selection.start; i < l; i++) { if (input.charAt(i) !== this.pattern.pattern[i]) { return false } } // Continue as if the selection and input started from the editable part of // the pattern. input = input.substring(this.pattern.firstEditableIndex - this.selection.start) this.selection.start = this.pattern.firstEditableIndex } for (i = 0, l = input.length; i < l && this.selection.start <= this.pattern.lastEditableIndex; i++) { var valid = this.input(input.charAt(i)) // Allow static parts of the pattern to appear in pasted input - they will // already have been stepped over by input(), so verify that the value // deemed invalid by input() was the expected static character. if (!valid) { if (this.selection.start > 0) { // XXX This only allows for one static character to be skipped var patternIndex = this.selection.start - 1 if (!this.pattern.isEditableIndex(patternIndex) && input.charAt(i) === this.pattern.pattern[patternIndex]) { continue } } extend(this, initialState) return false } } return true } // History InputMask.prototype.undo = function undo() { // If there is no history, or nothing more on the history stack, we can't undo if (this._history.length === 0 || this._historyIndex === 0) { return false } var historyItem if (this._historyIndex == null) { // Not currently undoing, set up the initial history index this._historyIndex = this._history.length - 1 historyItem = this._history[this._historyIndex] // Add a new history entry if anything has changed since the last one, so we // can redo back to the initial state we started undoing from. var value = this.getValue() if (historyItem.value !== value || historyItem.selection.start !== this.selection.start || historyItem.selection.end !== this.selection.end) { this._history.push({value: value, selection: copy(this.selection), lastOp: this._lastOp, startUndo: true}) } } else { historyItem = this._history[--this._historyIndex] } this.value = historyItem.value.split('') this.selection = historyItem.selection this._lastOp = historyItem.lastOp return true } InputMask.prototype.redo = function redo() { if (this._history.length === 0 || this._historyIndex == null) { return false } var historyItem = this._history[++this._historyIndex] // If this is the last history item, we're done redoing if (this._historyIndex === this._history.length - 1) { this._historyIndex = null // If the last history item was only added to start undoing, remove it if (historyItem.startUndo) { this._history.pop() } } this.value = historyItem.value.split('') this.selection = historyItem.selection this._lastOp = historyItem.lastOp return true } // Getters & setters InputMask.prototype.setPattern = function setPattern(pattern, options) { options = extend({ selection: {start: 0, end: 0}, value: '' }, options) this.pattern = new Pattern(pattern, this.formatCharacters, this.placeholderChar, options.isRevealingMask) this.setValue(options.value) this.emptyValue = this.pattern.formatValue([]).join('') this.selection = options.selection this._resetHistory() } InputMask.prototype.setSelection = function setSelection(selection) { this.selection = copy(selection) if (this.selection.start === this.selection.end) { if (this.selection.start < this.pattern.firstEditableIndex) { this.selection.start = this.selection.end = this.pattern.firstEditableIndex return true } // Set selection to the first editable, non-placeholder character before the selection // OR to the beginning of the pattern var index = this.selection.start while (index >= this.pattern.firstEditableIndex) { if (this.pattern.isEditableIndex(index - 1) && this.value[index - 1] !== this.placeholderChar || index === this.pattern.firstEditableIndex) { this.selection.start = this.selection.end = index break } index-- } return true } return false } InputMask.prototype.setValue = function setValue(value) { if (value == null) { value = '' } this.value = this.pattern.formatValue(value.split('')) } InputMask.prototype.getValue = function getValue() { return this.value.join('') } InputMask.prototype.getRawValue = function getRawValue() { var rawValue = [] for (var i = 0; i < this.value.length; i++) { if (this.pattern._editableIndices[i] === true) { rawValue.push(this.value[i]) } } return rawValue.join('') } InputMask.prototype._resetHistory = function _resetHistory() { this._history = [] this._historyIndex = null this._lastOp = null this._lastSelection = copy(this.selection) } InputMask.Pattern = Pattern module.exports = InputMask /***/ }, /* 6 */ /***/ function(module, exports, __webpack_require__) { /** * Copyright 2013-present, Facebook, Inc. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. */ 'use strict'; if (true) { var invariant = __webpack_require__(2); var warning = __webpack_require__(3); var ReactPropTypesSecret = __webpack_require__(4); var loggedTypeFailures = {}; } /** * Assert that the values match with the type specs. * Error messages are memorized and will only be shown once. * * @param {object} typeSpecs Map of name to a ReactPropType * @param {object} values Runtime values that need to be type-checked * @param {string} location e.g. "prop", "context", "child context" * @param {string} componentName Name of the component for error messages. * @param {?Function} getStack Returns the component stack. * @private */ function checkPropTypes(typeSpecs, values, location, componentName, getStack) { if (true) { for (var typeSpecName in typeSpecs) { if (typeSpecs.hasOwnProperty(typeSpecName)) { var error; // Prop type validation may throw. In case they do, we don't want to // fail the render phase where it didn't fail before. So we log it. // After these have been cleaned up, we'll let them throw. try { // This is intentionally an invariant that gets caught. It's the same // behavior as without this statement except with a better message. invariant(typeof typeSpecs[typeSpecName] === 'function', '%s: %s type `%s` is invalid; it must be a function, usually from ' + 'React.PropTypes.', componentName || 'React class', location, typeSpecName); error = typeSpecs[typeSpecName](values, typeSpecName, componentName, location, null, ReactPropTypesSecret); } catch (ex) { error = ex; } warning(!error || error instanceof Error, '%s: type specification of %s `%s` is invalid; the type checker ' + 'function must return `null` or an `Error` but returned a %s. ' + 'You may have forgotten to pass an argument to the type checker ' + 'creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and ' + 'shape all require an argument).', componentName || 'React class', location, typeSpecName, typeof error); if (error instanceof Error && !(error.message in loggedTypeFailures)) { // Only monitor this failure once because there tends to be a lot of the // same error. loggedTypeFailures[error.message] = true; var stack = getStack ? getStack() : ''; warning(false, 'Failed %s type: %s%s', location, error.message, stack != null ? stack : ''); } } } } } module.exports = checkPropTypes; /***/ }, /* 7 */ /***/ function(module, exports, __webpack_require__) { /** * Copyright 2013-present, Facebook, Inc. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. */ 'use strict'; var emptyFunction = __webpack_require__(1); var invariant = __webpack_require__(2); var warning = __webpack_require__(3); var ReactPropTypesSecret = __webpack_require__(4); var checkPropTypes = __webpack_require__(6); module.exports = function(isValidElement, throwOnDirectAccess) { /* global Symbol */ var ITERATOR_SYMBOL = typeof Symbol === 'function' && Symbol.iterator; var FAUX_ITERATOR_SYMBOL = '@@iterator'; // Before Symbol spec. /** * Returns the iterator method function contained on the iterable object. * * Be sure to invoke the function with the iterable as context: * * var iteratorFn = getIteratorFn(myIterable); * if (iteratorFn) { * var iterator = iteratorFn.call(myIterable); * ... * } * * @param {?object} maybeIterable * @return {?function} */ function getIteratorFn(maybeIterable) { var iteratorFn = maybeIterable && (ITERATOR_SYMBOL && maybeIterable[ITERATOR_SYMBOL] || maybeIterable[FAUX_ITERATOR_SYMBOL]); if (typeof iteratorFn === 'function') { return iteratorFn; } } /** * Collection of methods that allow declaration and validation of props that are * supplied to React components. Example usage: * * var Props = require('ReactPropTypes'); * var MyArticle = React.createClass({ * propTypes: { * // An optional string prop named "description". * description: Props.string, * * // A required enum prop named "category". * category: Props.oneOf(['News','Photos']).isRequired, * * // A prop named "dialog" that requires an instance of Dialog. * dialog: Props.instanceOf(Dialog).isRequired * }, * render: function() { ... } * }); * * A more formal specification of how these methods are used: * * type := array|bool|func|object|number|string|oneOf([...])|instanceOf(...) * decl := ReactPropTypes.{type}(.isRequired)? * * Each and every declaration produces a function with the same signature. This * allows the creation of custom validation functions. For example: * * var MyLink = React.createClass({ * propTypes: { * // An optional string or URI prop named "href". * href: function(props, propName, componentName) { * var propValue = props[propName]; * if (propValue != null && typeof propValue !== 'string' && * !(propValue instanceof URI)) { * return new Error( * 'Expected a string or an URI for ' + propName + ' in ' + * componentName * ); * } * } * }, * render: function() {...} * }); * * @internal */ var ANONYMOUS = '<<anonymous>>'; // Important! // Keep this list in sync with production version in `./factoryWithThrowingShims.js`. var ReactPropTypes = { array: createPrimitiveTypeChecker('array'), bool: createPrimitiveTypeChecker('boolean'), func: createPrimitiveTypeChecker('function'), number: createPrimitiveTypeChecker('number'), object: createPrimitiveTypeChecker('object'), string: createPrimitiveTypeChecker('string'), symbol: createPrimitiveTypeChecker('symbol'), any: createAnyTypeChecker(), arrayOf: createArrayOfTypeChecker, element: createElementTypeChecker(), instanceOf: createInstanceTypeChecker, node: createNodeChecker(), objectOf: createObjectOfTypeChecker, oneOf: createEnumTypeChecker, oneOfType: createUnionTypeChecker, shape: createShapeTypeChecker }; /** * inlined Object.is polyfill to avoid requiring consumers ship their own * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is */ /*eslint-disable no-self-compare*/ function is(x, y) { // SameValue algorithm if (x === y) { // Steps 1-5, 7-10 // Steps 6.b-6.e: +0 != -0 return x !== 0 || 1 / x === 1 / y; } else { // Step 6.a: NaN == NaN return x !== x && y !== y; } } /*eslint-enable no-self-compare*/ /** * We use an Error-like object for backward compatibility as people may call * PropTypes directly and inspect their output. However, we don't use real * Errors anymore. We don't inspect their stack anyway, and creating them * is prohibitively expensive if they are created too often, such as what * happens in oneOfType() for any type before the one that matched. */ function PropTypeError(message) { this.message = message; this.stack = ''; } // Make `instanceof Error` still work for returned errors. PropTypeError.prototype = Error.prototype; function createChainableTypeChecker(validate) { if (true) { var manualPropTypeCallCache = {}; var manualPropTypeWarningCount = 0; } function checkType(isRequired, props, propName, componentName, location, propFullName, secret) { componentName = componentName || ANONYMOUS; propFullName = propFullName || propName; if (secret !== ReactPropTypesSecret) { if (throwOnDirectAccess) { // New behavior only for users of `prop-types` package invariant( false, 'Calling PropTypes validators directly is not supported by the `prop-types` package. ' + 'Use `PropTypes.checkPropTypes()` to call them. ' + 'Read more at http://fb.me/use-check-prop-types' ); } else if (("development") !== 'production' && typeof console !== 'undefined') { // Old behavior for people using React.PropTypes var cacheKey = componentName + ':' + propName; if ( !manualPropTypeCallCache[cacheKey] && // Avoid spamming the console because they are often not actionable except for lib authors manualPropTypeWarningCount < 3 ) { warning( false, 'You are manually calling a React.PropTypes validation ' + 'function for the `%s` prop on `%s`. This is deprecated ' + 'and will throw in the standalone `prop-types` package. ' + 'You may be seeing this warning due to a third-party PropTypes ' + 'library. See https://fb.me/react-warning-dont-call-proptypes ' + 'for details.', propFullName, componentName ); manualPropTypeCallCache[cacheKey] = true; manualPropTypeWarningCount++; } } } if (props[propName] == null) { if (isRequired) { if (props[propName] === null) { return new PropTypeError('The ' + location + ' `' + propFullName + '` is marked as required ' + ('in `' + componentName + '`, but its value is `null`.')); } return new PropTypeError('The ' + location + ' `' + propFullName + '` is marked as required in ' + ('`' + componentName + '`, but its value is `undefined`.')); } return null; } else { return validate(props, propName, componentName, location, propFullName); } } var chainedCheckType = checkType.bind(null, false); chainedCheckType.isRequired = checkType.bind(null, true); return chainedCheckType; } function createPrimitiveTypeChecker(expectedType) { function validate(props, propName, componentName, location, propFullName, secret) { var propValue = props[propName]; var propType = getPropType(propValue); if (propType !== expectedType) { // `propValue` being instance of, say, date/regexp, pass the 'object' // check, but we can offer a more precise error message here rather than // 'of type `object`'. var preciseType = getPreciseType(propValue); return new PropTypeError('Invalid ' + location + ' `' + propFullName + '` of type ' + ('`' + preciseType + '` supplied to `' + componentName + '`, expected ') + ('`' + expectedType + '`.')); } return null; } return createChainableTypeChecker(validate); } function createAnyTypeChecker() { return createChainableTypeChecker(emptyFunction.thatReturnsNull); } function createArrayOfTypeChecker(typeChecker) { function validate(props, propName, componentName, location, propFullName) { if (typeof typeChecker !== 'function') { return new PropTypeError('Property `' + propFullName + '` of component `' + componentName + '` has invalid PropType notation inside arrayOf.'); } var propValue = props[propName]; if (!Array.isArray(propValue)) { var propType = getPropType(propValue); return new PropTypeError('Invalid ' + location + ' `' + propFullName + '` of type ' + ('`' + propType + '` supplied to `' + componentName + '`, expecte