react-imask
Advanced tools
Comparing version 6.5.0 to 6.5.1-alpha.0
@@ -13,91 +13,2 @@ (function (global, factory) { | ||
function _iterableToArrayLimit(arr, i) { | ||
var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; | ||
if (null != _i) { | ||
var _s, | ||
_e, | ||
_x, | ||
_r, | ||
_arr = [], | ||
_n = !0, | ||
_d = !1; | ||
try { | ||
if (_x = (_i = _i.call(arr)).next, 0 === i) { | ||
if (Object(_i) !== _i) return; | ||
_n = !1; | ||
} else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); | ||
} catch (err) { | ||
_d = !0, _e = err; | ||
} finally { | ||
try { | ||
if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; | ||
} finally { | ||
if (_d) throw _e; | ||
} | ||
} | ||
return _arr; | ||
} | ||
} | ||
function _classCallCheck(instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a 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, _toPropertyKey(descriptor.key), descriptor); | ||
} | ||
} | ||
function _createClass(Constructor, protoProps, staticProps) { | ||
if (protoProps) _defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) _defineProperties(Constructor, staticProps); | ||
Object.defineProperty(Constructor, "prototype", { | ||
writable: false | ||
}); | ||
return Constructor; | ||
} | ||
function _inherits(subClass, superClass) { | ||
if (typeof superClass !== "function" && superClass !== null) { | ||
throw new TypeError("Super expression must either be null or a function"); | ||
} | ||
subClass.prototype = Object.create(superClass && superClass.prototype, { | ||
constructor: { | ||
value: subClass, | ||
writable: true, | ||
configurable: true | ||
} | ||
}); | ||
Object.defineProperty(subClass, "prototype", { | ||
writable: false | ||
}); | ||
if (superClass) _setPrototypeOf(subClass, superClass); | ||
} | ||
function _getPrototypeOf(o) { | ||
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { | ||
return o.__proto__ || Object.getPrototypeOf(o); | ||
}; | ||
return _getPrototypeOf(o); | ||
} | ||
function _setPrototypeOf(o, p) { | ||
_setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { | ||
o.__proto__ = p; | ||
return o; | ||
}; | ||
return _setPrototypeOf(o, p); | ||
} | ||
function _isNativeReflectConstruct() { | ||
if (typeof Reflect === "undefined" || !Reflect.construct) return false; | ||
if (Reflect.construct.sham) return false; | ||
if (typeof Proxy === "function") return true; | ||
try { | ||
Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
function _objectDestructuringEmpty(obj) { | ||
@@ -118,83 +29,6 @@ if (obj == null) throw new TypeError("Cannot destructure " + obj); | ||
} | ||
function _objectWithoutProperties(source, excluded) { | ||
if (source == null) return {}; | ||
var target = _objectWithoutPropertiesLoose(source, excluded); | ||
var key, i; | ||
if (Object.getOwnPropertySymbols) { | ||
var sourceSymbolKeys = Object.getOwnPropertySymbols(source); | ||
for (i = 0; i < sourceSymbolKeys.length; i++) { | ||
key = sourceSymbolKeys[i]; | ||
if (excluded.indexOf(key) >= 0) continue; | ||
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; | ||
target[key] = source[key]; | ||
} | ||
} | ||
return target; | ||
} | ||
function _assertThisInitialized(self) { | ||
if (self === void 0) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
} | ||
return self; | ||
} | ||
function _possibleConstructorReturn(self, call) { | ||
if (call && (typeof call === "object" || typeof call === "function")) { | ||
return call; | ||
} else if (call !== void 0) { | ||
throw new TypeError("Derived constructors may only return object or undefined"); | ||
} | ||
return _assertThisInitialized(self); | ||
} | ||
function _createSuper(Derived) { | ||
var hasNativeReflectConstruct = _isNativeReflectConstruct(); | ||
return function _createSuperInternal() { | ||
var Super = _getPrototypeOf(Derived), | ||
result; | ||
if (hasNativeReflectConstruct) { | ||
var NewTarget = _getPrototypeOf(this).constructor; | ||
result = Reflect.construct(Super, arguments, NewTarget); | ||
} else { | ||
result = Super.apply(this, arguments); | ||
} | ||
return _possibleConstructorReturn(this, result); | ||
}; | ||
} | ||
function _slicedToArray(arr, i) { | ||
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); | ||
} | ||
function _arrayWithHoles(arr) { | ||
if (Array.isArray(arr)) return arr; | ||
} | ||
function _unsupportedIterableToArray(o, minLen) { | ||
if (!o) return; | ||
if (typeof o === "string") return _arrayLikeToArray(o, minLen); | ||
var n = Object.prototype.toString.call(o).slice(8, -1); | ||
if (n === "Object" && o.constructor) n = o.constructor.name; | ||
if (n === "Map" || n === "Set") return Array.from(o); | ||
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); | ||
} | ||
function _arrayLikeToArray(arr, len) { | ||
if (len == null || len > arr.length) len = arr.length; | ||
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; | ||
return arr2; | ||
} | ||
function _nonIterableRest() { | ||
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); | ||
} | ||
function _toPrimitive(input, hint) { | ||
if (typeof input !== "object" || input === null) return input; | ||
var prim = input[Symbol.toPrimitive]; | ||
if (prim !== undefined) { | ||
var res = prim.call(input, hint || "default"); | ||
if (typeof res !== "object") return res; | ||
throw new TypeError("@@toPrimitive must return a primitive value."); | ||
} | ||
return (hint === "string" ? String : Number)(input); | ||
} | ||
function _toPropertyKey(arg) { | ||
var key = _toPrimitive(arg, "string"); | ||
return typeof key === "symbol" ? key : String(key); | ||
} | ||
var MASK_PROPS = { | ||
// TODO should be imported from core | ||
const MASK_PROPS = { | ||
// common | ||
@@ -240,9 +74,5 @@ mask: PropTypes__default["default"].oneOfType([PropTypes__default["default"].array, PropTypes__default["default"].func, PropTypes__default["default"].string, PropTypes__default["default"].instanceOf(RegExp), PropTypes__default["default"].oneOf([Date, Number, IMask__default["default"].Masked]), PropTypes__default["default"].instanceOf(IMask__default["default"].Masked)]), | ||
}; | ||
var MASK_PROPS_NAMES = Object.keys(MASK_PROPS).filter(function (p) { | ||
return p !== 'value'; | ||
}); | ||
var NON_MASK_OPTIONS_PROPS_NAMES = ['value', 'unmask', 'onAccept', 'onComplete', 'inputRef']; | ||
var MASK_OPTIONS_PROPS_NAMES = MASK_PROPS_NAMES.filter(function (pName) { | ||
return NON_MASK_OPTIONS_PROPS_NAMES.indexOf(pName) < 0; | ||
}); | ||
const MASK_PROPS_NAMES = Object.keys(MASK_PROPS).filter(p => p !== 'value'); | ||
const NON_MASK_OPTIONS_PROPS_NAMES = ['value', 'unmask', 'onAccept', 'onComplete', 'inputRef']; | ||
const MASK_OPTIONS_PROPS_NAMES = MASK_PROPS_NAMES.filter(pName => NON_MASK_OPTIONS_PROPS_NAMES.indexOf(pName) < 0); | ||
// TODO | ||
@@ -252,132 +82,99 @@ // 1. seems like it's wrong to have Opts as only mask options. Other component/input props should also be there. Where is "unmask" prop for instance? | ||
function IMaskMixin(ComposedComponent) { | ||
var MaskedComponent = /*#__PURE__*/function (_React$Component) { | ||
_inherits(MaskedComponent, _React$Component); | ||
var _super = _createSuper(MaskedComponent); | ||
function MaskedComponent(props) { | ||
var _this; | ||
_classCallCheck(this, MaskedComponent); | ||
_this = _super.call(this, props); | ||
_this._inputRef = _this._inputRef.bind(_assertThisInitialized(_this)); | ||
return _this; | ||
const MaskedComponent = class extends React__default["default"].Component { | ||
constructor(props) { | ||
super(props); | ||
this._inputRef = this._inputRef.bind(this); | ||
} | ||
_createClass(MaskedComponent, [{ | ||
key: "componentDidMount", | ||
value: function componentDidMount() { | ||
if (!this.props.mask) return; | ||
this.initMask(); | ||
} | ||
}, { | ||
key: "componentDidUpdate", | ||
value: function componentDidUpdate() { | ||
var props = this.props; | ||
var maskOptions = this._extractMaskOptionsFromProps(props); | ||
if (maskOptions.mask) { | ||
if (this.maskRef) { | ||
this.maskRef.updateOptions(maskOptions); | ||
if ('value' in props) this.maskValue = props.value; | ||
} else { | ||
this.initMask(maskOptions); | ||
} | ||
componentDidMount() { | ||
if (!this.props.mask) return; | ||
this.initMask(); | ||
} | ||
componentDidUpdate() { | ||
const props = this.props; | ||
const maskOptions = this._extractMaskOptionsFromProps(props); | ||
if (maskOptions.mask) { | ||
if (this.maskRef) { | ||
this.maskRef.updateOptions(maskOptions); | ||
if ('value' in props) this.maskValue = props.value; | ||
} else { | ||
this.destroyMask(); | ||
if ('value' in props) this.element.value = props.value; | ||
this.initMask(maskOptions); | ||
} | ||
} | ||
}, { | ||
key: "componentWillUnmount", | ||
value: function componentWillUnmount() { | ||
} else { | ||
this.destroyMask(); | ||
if ('value' in props) this.element.value = props.value; | ||
} | ||
}, { | ||
key: "_inputRef", | ||
value: function _inputRef(el) { | ||
this.element = el; | ||
if (this.props.inputRef) { | ||
if (Object.prototype.hasOwnProperty.call(this.props.inputRef, 'current')) this.props.inputRef.current = el;else this.props.inputRef(el); | ||
} | ||
} | ||
componentWillUnmount() { | ||
this.destroyMask(); | ||
} | ||
_inputRef(el) { | ||
this.element = el; | ||
if (this.props.inputRef) { | ||
if (Object.prototype.hasOwnProperty.call(this.props.inputRef, 'current')) this.props.inputRef.current = el;else this.props.inputRef(el); | ||
} | ||
}, { | ||
key: "initMask", | ||
value: function initMask() { | ||
var maskOptions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this._extractMaskOptionsFromProps(this.props); | ||
this.maskRef = IMask__default["default"](this.element, maskOptions).on('accept', this._onAccept.bind(this)).on('complete', this._onComplete.bind(this)); | ||
if ('value' in this.props) this.maskValue = this.props.value; | ||
} | ||
initMask() { | ||
let maskOptions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this._extractMaskOptionsFromProps(this.props); | ||
this.maskRef = IMask__default["default"](this.element, maskOptions).on('accept', this._onAccept.bind(this)).on('complete', this._onComplete.bind(this)); | ||
if ('value' in this.props) this.maskValue = this.props.value; | ||
} | ||
destroyMask() { | ||
if (this.maskRef) { | ||
this.maskRef.destroy(); | ||
delete this.maskRef; | ||
} | ||
}, { | ||
key: "destroyMask", | ||
value: function destroyMask() { | ||
if (this.maskRef) { | ||
this.maskRef.destroy(); | ||
delete this.maskRef; | ||
} | ||
} | ||
}, { | ||
key: "_extractMaskOptionsFromProps", | ||
value: function _extractMaskOptionsFromProps(props) { | ||
var cloneProps = Object.assign({}, (_objectDestructuringEmpty(props), props)); | ||
} | ||
_extractMaskOptionsFromProps(props) { | ||
const cloneProps = Object.assign({}, (_objectDestructuringEmpty(props), props)); | ||
// keep only mask options props | ||
Object.keys(cloneProps) | ||
// TODO why need cast to string? | ||
.filter(function (prop) { | ||
return MASK_OPTIONS_PROPS_NAMES.indexOf(prop) < 0; | ||
}).forEach(function (nonMaskProp) { | ||
delete cloneProps[nonMaskProp]; | ||
}); | ||
return cloneProps; | ||
} | ||
}, { | ||
key: "_extractNonMaskProps", | ||
value: function _extractNonMaskProps(props) { | ||
var cloneProps = Object.assign({}, (_objectDestructuringEmpty(props), props)); | ||
MASK_PROPS_NAMES.forEach(function (maskProp) { | ||
delete cloneProps[maskProp]; | ||
}); | ||
return cloneProps; | ||
} | ||
}, { | ||
key: "maskValue", | ||
get: function get() { | ||
if (this.props.unmask === 'typed') return this.maskRef.typedValue; | ||
if (this.props.unmask) return this.maskRef.unmaskedValue; | ||
return this.maskRef.value; | ||
}, | ||
set: function set(value) { | ||
value = value == null ? '' : value; | ||
if (this.props.unmask === 'typed') this.maskRef.typedValue = value;else if (this.props.unmask) this.maskRef.unmaskedValue = value;else this.maskRef.value = value; | ||
} | ||
}, { | ||
key: "_onAccept", | ||
value: function _onAccept(e) { | ||
if (this.props.onAccept && this.maskRef) this.props.onAccept(this.maskValue, this.maskRef, e); | ||
} | ||
}, { | ||
key: "_onComplete", | ||
value: function _onComplete(e) { | ||
if (this.props.onComplete && this.maskRef) this.props.onComplete(this.maskValue, this.maskRef, e); | ||
} | ||
}, { | ||
key: "render", | ||
value: function render() { | ||
return React__default["default"].createElement(ComposedComponent, Object.assign({}, this._extractNonMaskProps(this.props), { | ||
inputRef: this._inputRef | ||
})); | ||
} | ||
}]); | ||
return MaskedComponent; | ||
}(React__default["default"].Component); | ||
var nestedComponentName = ComposedComponent.displayName || ComposedComponent.name || 'Component'; | ||
// keep only mask options props | ||
Object.keys(cloneProps) | ||
// TODO why need cast to string? | ||
.filter(prop => MASK_OPTIONS_PROPS_NAMES.indexOf(prop) < 0).forEach(nonMaskProp => { | ||
delete cloneProps[nonMaskProp]; | ||
}); | ||
return cloneProps; | ||
} | ||
_extractNonMaskProps(props) { | ||
const cloneProps = Object.assign({}, (_objectDestructuringEmpty(props), props)); | ||
MASK_PROPS_NAMES.forEach(maskProp => { | ||
delete cloneProps[maskProp]; | ||
}); | ||
return cloneProps; | ||
} | ||
get maskValue() { | ||
if (this.props.unmask === 'typed') return this.maskRef.typedValue; | ||
if (this.props.unmask) return this.maskRef.unmaskedValue; | ||
return this.maskRef.value; | ||
} | ||
set maskValue(value) { | ||
value = value == null ? '' : value; | ||
if (this.props.unmask === 'typed') this.maskRef.typedValue = value;else if (this.props.unmask) this.maskRef.unmaskedValue = value;else this.maskRef.value = value; | ||
} | ||
_onAccept(e) { | ||
if (this.props.onAccept && this.maskRef) this.props.onAccept(this.maskValue, this.maskRef, e); | ||
} | ||
_onComplete(e) { | ||
if (this.props.onComplete && this.maskRef) this.props.onComplete(this.maskValue, this.maskRef, e); | ||
} | ||
render() { | ||
return React__default["default"].createElement(ComposedComponent, Object.assign({}, this._extractNonMaskProps(this.props), { | ||
inputRef: this._inputRef | ||
})); | ||
} | ||
}; | ||
const nestedComponentName = ComposedComponent.displayName || ComposedComponent.name || 'Component'; | ||
MaskedComponent.displayName = "IMask(".concat(nestedComponentName, ")"); | ||
MaskedComponent.propTypes = MASK_PROPS; | ||
return React__default["default"].forwardRef(function (props, ref) { | ||
return React__default["default"].createElement(MaskedComponent, Object.assign({}, props, { | ||
ref: ref | ||
})); | ||
}); | ||
return React__default["default"].forwardRef((props, ref) => React__default["default"].createElement(MaskedComponent, Object.assign({}, props, { | ||
ref | ||
}))); | ||
} | ||
var _excluded$1 = ["inputRef"]; | ||
var IMaskInput = IMaskMixin(function (_ref) { | ||
var inputRef = _ref.inputRef, | ||
props = _objectWithoutProperties(_ref, _excluded$1); | ||
const _excluded$1 = ["inputRef"]; | ||
const IMaskInput = IMaskMixin(_ref => { | ||
let { | ||
inputRef | ||
} = _ref, | ||
props = _objectWithoutPropertiesLoose(_ref, _excluded$1); | ||
return React__default["default"].createElement('input', Object.assign({}, props, { | ||
@@ -388,32 +185,18 @@ ref: inputRef | ||
var _excluded = ["value"], | ||
const _excluded = ["value"], | ||
_excluded2 = ["unmaskedValue"], | ||
_excluded3 = ["typedValue"]; | ||
function useIMask(opts) { | ||
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, | ||
onAccept = _ref.onAccept, | ||
onComplete = _ref.onComplete; | ||
var ref = React.useRef(null); | ||
var maskRef = React.useRef(null); | ||
var _useState = React.useState(false), | ||
_useState2 = _slicedToArray(_useState, 2), | ||
initialized = _useState2[0], | ||
setInitialized = _useState2[1]; | ||
var _useState3 = React.useState({}), | ||
_useState4 = _slicedToArray(_useState3, 2), | ||
lastAcceptState = _useState4[0], | ||
setLastAcceptState = _useState4[1]; | ||
var _useState5 = React.useState(''), | ||
_useState6 = _slicedToArray(_useState5, 2), | ||
value = _useState6[0], | ||
setValue = _useState6[1]; | ||
var _useState7 = React.useState(''), | ||
_useState8 = _slicedToArray(_useState7, 2), | ||
unmaskedValue = _useState8[0], | ||
setUnmaskedValue = _useState8[1]; | ||
var _useState9 = React.useState(), | ||
_useState10 = _slicedToArray(_useState9, 2), | ||
typedValue = _useState10[0], | ||
setTypedValue = _useState10[1]; | ||
var _destroyMask = React.useCallback(function () { | ||
let { | ||
onAccept, | ||
onComplete | ||
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
const ref = React.useRef(null); | ||
const maskRef = React.useRef(null); | ||
const [initialized, setInitialized] = React.useState(false); | ||
const [lastAcceptState, setLastAcceptState] = React.useState({}); | ||
const [value, setValue] = React.useState(''); | ||
const [unmaskedValue, setUnmaskedValue] = React.useState(''); | ||
const [typedValue, setTypedValue] = React.useState(); | ||
const _destroyMask = React.useCallback(() => { | ||
var _maskRef$current; | ||
@@ -423,4 +206,4 @@ (_maskRef$current = maskRef.current) === null || _maskRef$current === void 0 ? void 0 : _maskRef$current.destroy(); | ||
}, []); | ||
var _onAccept = React.useCallback(function (event) { | ||
var m = maskRef.current; | ||
const _onAccept = React.useCallback(event => { | ||
const m = maskRef.current; | ||
if (!m) return; | ||
@@ -437,9 +220,7 @@ setLastAcceptState({ | ||
}, [onAccept]); | ||
var _onComplete = React.useCallback(function () { | ||
return maskRef.current && (onComplete === null || onComplete === void 0 ? void 0 : onComplete(maskRef.current.value, maskRef.current)); | ||
}, [onComplete]); | ||
React.useEffect(function () { | ||
var el = ref.current; | ||
const _onComplete = React.useCallback(() => maskRef.current && (onComplete === null || onComplete === void 0 ? void 0 : onComplete(maskRef.current.value, maskRef.current)), [onComplete]); | ||
React.useEffect(() => { | ||
const el = ref.current; | ||
if (!el || !(opts !== null && opts !== void 0 && opts.mask)) return _destroyMask(); | ||
var mask = maskRef.current; | ||
const mask = maskRef.current; | ||
if (!mask) { | ||
@@ -455,8 +236,8 @@ if (el && opts !== null && opts !== void 0 && opts.mask) { | ||
}, [opts, _destroyMask, _onAccept]); | ||
React.useEffect(function () { | ||
React.useEffect(() => { | ||
if (!maskRef.current) return; | ||
var mask = maskRef.current; | ||
const mask = maskRef.current; | ||
mask.on('accept', _onAccept); | ||
mask.on('complete', _onComplete); | ||
return function () { | ||
return () => { | ||
mask.off('accept', _onAccept); | ||
@@ -466,35 +247,39 @@ mask.off('complete', _onComplete); | ||
}, [_onAccept, _onComplete]); | ||
React.useEffect(function () { | ||
var lastAcceptValue = lastAcceptState.value, | ||
state = _objectWithoutProperties(lastAcceptState, _excluded); | ||
var mask = maskRef.current; | ||
React.useEffect(() => { | ||
const { | ||
value: lastAcceptValue | ||
} = lastAcceptState, | ||
state = _objectWithoutPropertiesLoose(lastAcceptState, _excluded); | ||
const mask = maskRef.current; | ||
if (mask && initialized && lastAcceptValue !== value) mask.value = value; | ||
setLastAcceptState(state); | ||
}, [value]); | ||
React.useEffect(function () { | ||
var lastAcceptUnmaskedValue = lastAcceptState.unmaskedValue, | ||
state = _objectWithoutProperties(lastAcceptState, _excluded2); | ||
var mask = maskRef.current; | ||
React.useEffect(() => { | ||
const { | ||
unmaskedValue: lastAcceptUnmaskedValue | ||
} = lastAcceptState, | ||
state = _objectWithoutPropertiesLoose(lastAcceptState, _excluded2); | ||
const mask = maskRef.current; | ||
if (mask && initialized && lastAcceptUnmaskedValue !== unmaskedValue) mask.unmaskedValue = unmaskedValue; | ||
setLastAcceptState(state); | ||
}, [unmaskedValue]); | ||
React.useEffect(function () { | ||
var lastAcceptTypedValue = lastAcceptState.typedValue, | ||
state = _objectWithoutProperties(lastAcceptState, _excluded3); | ||
var mask = maskRef.current; | ||
React.useEffect(() => { | ||
const { | ||
typedValue: lastAcceptTypedValue | ||
} = lastAcceptState, | ||
state = _objectWithoutPropertiesLoose(lastAcceptState, _excluded3); | ||
const mask = maskRef.current; | ||
if (mask && initialized && lastAcceptTypedValue !== typedValue) mask.typedValue = typedValue; | ||
setLastAcceptState(state); | ||
}, [typedValue]); | ||
React.useEffect(function () { | ||
return _destroyMask; | ||
}, [_destroyMask]); | ||
React.useEffect(() => _destroyMask, [_destroyMask]); | ||
return { | ||
ref: ref, | ||
maskRef: maskRef, | ||
value: value, | ||
setValue: setValue, | ||
unmaskedValue: unmaskedValue, | ||
setUnmaskedValue: setUnmaskedValue, | ||
typedValue: typedValue, | ||
setTypedValue: setTypedValue | ||
ref, | ||
maskRef, | ||
value, | ||
setValue, | ||
unmaskedValue, | ||
setUnmaskedValue, | ||
typedValue, | ||
setTypedValue | ||
}; | ||
@@ -501,0 +286,0 @@ } |
112
esm/hook.js
@@ -1,35 +0,21 @@ | ||
import { _ as _slicedToArray, a as _objectWithoutProperties } from './_rollupPluginBabelHelpers-99d8e6d1.js'; | ||
import { _ as _objectWithoutPropertiesLoose } from './_rollupPluginBabelHelpers-2cac7098.js'; | ||
import IMask from 'imask/esm/imask'; | ||
import { useRef, useState, useCallback, useEffect } from 'react'; | ||
var _excluded = ["value"], | ||
const _excluded = ["value"], | ||
_excluded2 = ["unmaskedValue"], | ||
_excluded3 = ["typedValue"]; | ||
function useIMask(opts) { | ||
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, | ||
onAccept = _ref.onAccept, | ||
onComplete = _ref.onComplete; | ||
var ref = useRef(null); | ||
var maskRef = useRef(null); | ||
var _useState = useState(false), | ||
_useState2 = _slicedToArray(_useState, 2), | ||
initialized = _useState2[0], | ||
setInitialized = _useState2[1]; | ||
var _useState3 = useState({}), | ||
_useState4 = _slicedToArray(_useState3, 2), | ||
lastAcceptState = _useState4[0], | ||
setLastAcceptState = _useState4[1]; | ||
var _useState5 = useState(''), | ||
_useState6 = _slicedToArray(_useState5, 2), | ||
value = _useState6[0], | ||
setValue = _useState6[1]; | ||
var _useState7 = useState(''), | ||
_useState8 = _slicedToArray(_useState7, 2), | ||
unmaskedValue = _useState8[0], | ||
setUnmaskedValue = _useState8[1]; | ||
var _useState9 = useState(), | ||
_useState10 = _slicedToArray(_useState9, 2), | ||
typedValue = _useState10[0], | ||
setTypedValue = _useState10[1]; | ||
var _destroyMask = useCallback(function () { | ||
let { | ||
onAccept, | ||
onComplete | ||
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
const ref = useRef(null); | ||
const maskRef = useRef(null); | ||
const [initialized, setInitialized] = useState(false); | ||
const [lastAcceptState, setLastAcceptState] = useState({}); | ||
const [value, setValue] = useState(''); | ||
const [unmaskedValue, setUnmaskedValue] = useState(''); | ||
const [typedValue, setTypedValue] = useState(); | ||
const _destroyMask = useCallback(() => { | ||
var _maskRef$current; | ||
@@ -39,4 +25,4 @@ (_maskRef$current = maskRef.current) === null || _maskRef$current === void 0 ? void 0 : _maskRef$current.destroy(); | ||
}, []); | ||
var _onAccept = useCallback(function (event) { | ||
var m = maskRef.current; | ||
const _onAccept = useCallback(event => { | ||
const m = maskRef.current; | ||
if (!m) return; | ||
@@ -53,9 +39,7 @@ setLastAcceptState({ | ||
}, [onAccept]); | ||
var _onComplete = useCallback(function () { | ||
return maskRef.current && (onComplete === null || onComplete === void 0 ? void 0 : onComplete(maskRef.current.value, maskRef.current)); | ||
}, [onComplete]); | ||
useEffect(function () { | ||
var el = ref.current; | ||
const _onComplete = useCallback(() => maskRef.current && (onComplete === null || onComplete === void 0 ? void 0 : onComplete(maskRef.current.value, maskRef.current)), [onComplete]); | ||
useEffect(() => { | ||
const el = ref.current; | ||
if (!el || !(opts !== null && opts !== void 0 && opts.mask)) return _destroyMask(); | ||
var mask = maskRef.current; | ||
const mask = maskRef.current; | ||
if (!mask) { | ||
@@ -71,8 +55,8 @@ if (el && opts !== null && opts !== void 0 && opts.mask) { | ||
}, [opts, _destroyMask, _onAccept]); | ||
useEffect(function () { | ||
useEffect(() => { | ||
if (!maskRef.current) return; | ||
var mask = maskRef.current; | ||
const mask = maskRef.current; | ||
mask.on('accept', _onAccept); | ||
mask.on('complete', _onComplete); | ||
return function () { | ||
return () => { | ||
mask.off('accept', _onAccept); | ||
@@ -82,35 +66,39 @@ mask.off('complete', _onComplete); | ||
}, [_onAccept, _onComplete]); | ||
useEffect(function () { | ||
var lastAcceptValue = lastAcceptState.value, | ||
state = _objectWithoutProperties(lastAcceptState, _excluded); | ||
var mask = maskRef.current; | ||
useEffect(() => { | ||
const { | ||
value: lastAcceptValue | ||
} = lastAcceptState, | ||
state = _objectWithoutPropertiesLoose(lastAcceptState, _excluded); | ||
const mask = maskRef.current; | ||
if (mask && initialized && lastAcceptValue !== value) mask.value = value; | ||
setLastAcceptState(state); | ||
}, [value]); | ||
useEffect(function () { | ||
var lastAcceptUnmaskedValue = lastAcceptState.unmaskedValue, | ||
state = _objectWithoutProperties(lastAcceptState, _excluded2); | ||
var mask = maskRef.current; | ||
useEffect(() => { | ||
const { | ||
unmaskedValue: lastAcceptUnmaskedValue | ||
} = lastAcceptState, | ||
state = _objectWithoutPropertiesLoose(lastAcceptState, _excluded2); | ||
const mask = maskRef.current; | ||
if (mask && initialized && lastAcceptUnmaskedValue !== unmaskedValue) mask.unmaskedValue = unmaskedValue; | ||
setLastAcceptState(state); | ||
}, [unmaskedValue]); | ||
useEffect(function () { | ||
var lastAcceptTypedValue = lastAcceptState.typedValue, | ||
state = _objectWithoutProperties(lastAcceptState, _excluded3); | ||
var mask = maskRef.current; | ||
useEffect(() => { | ||
const { | ||
typedValue: lastAcceptTypedValue | ||
} = lastAcceptState, | ||
state = _objectWithoutPropertiesLoose(lastAcceptState, _excluded3); | ||
const mask = maskRef.current; | ||
if (mask && initialized && lastAcceptTypedValue !== typedValue) mask.typedValue = typedValue; | ||
setLastAcceptState(state); | ||
}, [typedValue]); | ||
useEffect(function () { | ||
return _destroyMask; | ||
}, [_destroyMask]); | ||
useEffect(() => _destroyMask, [_destroyMask]); | ||
return { | ||
ref: ref, | ||
maskRef: maskRef, | ||
value: value, | ||
setValue: setValue, | ||
unmaskedValue: unmaskedValue, | ||
setUnmaskedValue: setUnmaskedValue, | ||
typedValue: typedValue, | ||
setTypedValue: setTypedValue | ||
ref, | ||
maskRef, | ||
value, | ||
setValue, | ||
unmaskedValue, | ||
setUnmaskedValue, | ||
typedValue, | ||
setTypedValue | ||
}; | ||
@@ -117,0 +105,0 @@ } |
@@ -6,4 +6,4 @@ import 'imask/esm'; | ||
export { default as useIMask } from './hook.js'; | ||
import './_rollupPluginBabelHelpers-99d8e6d1.js'; | ||
import './_rollupPluginBabelHelpers-2cac7098.js'; | ||
import 'react'; | ||
import 'prop-types'; |
@@ -1,2 +0,2 @@ | ||
import { a as _objectWithoutProperties } from './_rollupPluginBabelHelpers-99d8e6d1.js'; | ||
import { _ as _objectWithoutPropertiesLoose } from './_rollupPluginBabelHelpers-2cac7098.js'; | ||
import React from 'react'; | ||
@@ -7,6 +7,8 @@ import IMaskMixin from './mixin.js'; | ||
var _excluded = ["inputRef"]; | ||
var IMaskInput = IMaskMixin(function (_ref) { | ||
var inputRef = _ref.inputRef, | ||
props = _objectWithoutProperties(_ref, _excluded); | ||
const _excluded = ["inputRef"]; | ||
const IMaskInput = IMaskMixin(_ref => { | ||
let { | ||
inputRef | ||
} = _ref, | ||
props = _objectWithoutPropertiesLoose(_ref, _excluded); | ||
return React.createElement('input', Object.assign({}, props, { | ||
@@ -13,0 +15,0 @@ ref: inputRef |
209
esm/mixin.js
@@ -1,2 +0,2 @@ | ||
import { b as _inherits, c as _createSuper, d as _createClass, e as _classCallCheck, f as _assertThisInitialized, g as _objectDestructuringEmpty } from './_rollupPluginBabelHelpers-99d8e6d1.js'; | ||
import { a as _objectDestructuringEmpty } from './_rollupPluginBabelHelpers-2cac7098.js'; | ||
import React from 'react'; | ||
@@ -6,3 +6,5 @@ import PropTypes from 'prop-types'; | ||
var MASK_PROPS = { | ||
// TODO should be imported from core | ||
const MASK_PROPS = { | ||
// common | ||
@@ -48,9 +50,5 @@ mask: PropTypes.oneOfType([PropTypes.array, PropTypes.func, PropTypes.string, PropTypes.instanceOf(RegExp), PropTypes.oneOf([Date, Number, IMask.Masked]), PropTypes.instanceOf(IMask.Masked)]), | ||
}; | ||
var MASK_PROPS_NAMES = Object.keys(MASK_PROPS).filter(function (p) { | ||
return p !== 'value'; | ||
}); | ||
var NON_MASK_OPTIONS_PROPS_NAMES = ['value', 'unmask', 'onAccept', 'onComplete', 'inputRef']; | ||
var MASK_OPTIONS_PROPS_NAMES = MASK_PROPS_NAMES.filter(function (pName) { | ||
return NON_MASK_OPTIONS_PROPS_NAMES.indexOf(pName) < 0; | ||
}); | ||
const MASK_PROPS_NAMES = Object.keys(MASK_PROPS).filter(p => p !== 'value'); | ||
const NON_MASK_OPTIONS_PROPS_NAMES = ['value', 'unmask', 'onAccept', 'onComplete', 'inputRef']; | ||
const MASK_OPTIONS_PROPS_NAMES = MASK_PROPS_NAMES.filter(pName => NON_MASK_OPTIONS_PROPS_NAMES.indexOf(pName) < 0); | ||
// TODO | ||
@@ -60,128 +58,93 @@ // 1. seems like it's wrong to have Opts as only mask options. Other component/input props should also be there. Where is "unmask" prop for instance? | ||
function IMaskMixin(ComposedComponent) { | ||
var MaskedComponent = /*#__PURE__*/function (_React$Component) { | ||
_inherits(MaskedComponent, _React$Component); | ||
var _super = _createSuper(MaskedComponent); | ||
function MaskedComponent(props) { | ||
var _this; | ||
_classCallCheck(this, MaskedComponent); | ||
_this = _super.call(this, props); | ||
_this._inputRef = _this._inputRef.bind(_assertThisInitialized(_this)); | ||
return _this; | ||
const MaskedComponent = class extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this._inputRef = this._inputRef.bind(this); | ||
} | ||
_createClass(MaskedComponent, [{ | ||
key: "componentDidMount", | ||
value: function componentDidMount() { | ||
if (!this.props.mask) return; | ||
this.initMask(); | ||
} | ||
}, { | ||
key: "componentDidUpdate", | ||
value: function componentDidUpdate() { | ||
var props = this.props; | ||
var maskOptions = this._extractMaskOptionsFromProps(props); | ||
if (maskOptions.mask) { | ||
if (this.maskRef) { | ||
this.maskRef.updateOptions(maskOptions); | ||
if ('value' in props) this.maskValue = props.value; | ||
} else { | ||
this.initMask(maskOptions); | ||
} | ||
componentDidMount() { | ||
if (!this.props.mask) return; | ||
this.initMask(); | ||
} | ||
componentDidUpdate() { | ||
const props = this.props; | ||
const maskOptions = this._extractMaskOptionsFromProps(props); | ||
if (maskOptions.mask) { | ||
if (this.maskRef) { | ||
this.maskRef.updateOptions(maskOptions); | ||
if ('value' in props) this.maskValue = props.value; | ||
} else { | ||
this.destroyMask(); | ||
if ('value' in props) this.element.value = props.value; | ||
this.initMask(maskOptions); | ||
} | ||
} | ||
}, { | ||
key: "componentWillUnmount", | ||
value: function componentWillUnmount() { | ||
} else { | ||
this.destroyMask(); | ||
if ('value' in props) this.element.value = props.value; | ||
} | ||
}, { | ||
key: "_inputRef", | ||
value: function _inputRef(el) { | ||
this.element = el; | ||
if (this.props.inputRef) { | ||
if (Object.prototype.hasOwnProperty.call(this.props.inputRef, 'current')) this.props.inputRef.current = el;else this.props.inputRef(el); | ||
} | ||
} | ||
componentWillUnmount() { | ||
this.destroyMask(); | ||
} | ||
_inputRef(el) { | ||
this.element = el; | ||
if (this.props.inputRef) { | ||
if (Object.prototype.hasOwnProperty.call(this.props.inputRef, 'current')) this.props.inputRef.current = el;else this.props.inputRef(el); | ||
} | ||
}, { | ||
key: "initMask", | ||
value: function initMask() { | ||
var maskOptions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this._extractMaskOptionsFromProps(this.props); | ||
this.maskRef = IMask(this.element, maskOptions).on('accept', this._onAccept.bind(this)).on('complete', this._onComplete.bind(this)); | ||
if ('value' in this.props) this.maskValue = this.props.value; | ||
} | ||
initMask() { | ||
let maskOptions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this._extractMaskOptionsFromProps(this.props); | ||
this.maskRef = IMask(this.element, maskOptions).on('accept', this._onAccept.bind(this)).on('complete', this._onComplete.bind(this)); | ||
if ('value' in this.props) this.maskValue = this.props.value; | ||
} | ||
destroyMask() { | ||
if (this.maskRef) { | ||
this.maskRef.destroy(); | ||
delete this.maskRef; | ||
} | ||
}, { | ||
key: "destroyMask", | ||
value: function destroyMask() { | ||
if (this.maskRef) { | ||
this.maskRef.destroy(); | ||
delete this.maskRef; | ||
} | ||
} | ||
}, { | ||
key: "_extractMaskOptionsFromProps", | ||
value: function _extractMaskOptionsFromProps(props) { | ||
var cloneProps = Object.assign({}, (_objectDestructuringEmpty(props), props)); | ||
} | ||
_extractMaskOptionsFromProps(props) { | ||
const cloneProps = Object.assign({}, (_objectDestructuringEmpty(props), props)); | ||
// keep only mask options props | ||
Object.keys(cloneProps) | ||
// TODO why need cast to string? | ||
.filter(function (prop) { | ||
return MASK_OPTIONS_PROPS_NAMES.indexOf(prop) < 0; | ||
}).forEach(function (nonMaskProp) { | ||
delete cloneProps[nonMaskProp]; | ||
}); | ||
return cloneProps; | ||
} | ||
}, { | ||
key: "_extractNonMaskProps", | ||
value: function _extractNonMaskProps(props) { | ||
var cloneProps = Object.assign({}, (_objectDestructuringEmpty(props), props)); | ||
MASK_PROPS_NAMES.forEach(function (maskProp) { | ||
delete cloneProps[maskProp]; | ||
}); | ||
return cloneProps; | ||
} | ||
}, { | ||
key: "maskValue", | ||
get: function get() { | ||
if (this.props.unmask === 'typed') return this.maskRef.typedValue; | ||
if (this.props.unmask) return this.maskRef.unmaskedValue; | ||
return this.maskRef.value; | ||
}, | ||
set: function set(value) { | ||
value = value == null ? '' : value; | ||
if (this.props.unmask === 'typed') this.maskRef.typedValue = value;else if (this.props.unmask) this.maskRef.unmaskedValue = value;else this.maskRef.value = value; | ||
} | ||
}, { | ||
key: "_onAccept", | ||
value: function _onAccept(e) { | ||
if (this.props.onAccept && this.maskRef) this.props.onAccept(this.maskValue, this.maskRef, e); | ||
} | ||
}, { | ||
key: "_onComplete", | ||
value: function _onComplete(e) { | ||
if (this.props.onComplete && this.maskRef) this.props.onComplete(this.maskValue, this.maskRef, e); | ||
} | ||
}, { | ||
key: "render", | ||
value: function render() { | ||
return React.createElement(ComposedComponent, Object.assign({}, this._extractNonMaskProps(this.props), { | ||
inputRef: this._inputRef | ||
})); | ||
} | ||
}]); | ||
return MaskedComponent; | ||
}(React.Component); | ||
var nestedComponentName = ComposedComponent.displayName || ComposedComponent.name || 'Component'; | ||
// keep only mask options props | ||
Object.keys(cloneProps) | ||
// TODO why need cast to string? | ||
.filter(prop => MASK_OPTIONS_PROPS_NAMES.indexOf(prop) < 0).forEach(nonMaskProp => { | ||
delete cloneProps[nonMaskProp]; | ||
}); | ||
return cloneProps; | ||
} | ||
_extractNonMaskProps(props) { | ||
const cloneProps = Object.assign({}, (_objectDestructuringEmpty(props), props)); | ||
MASK_PROPS_NAMES.forEach(maskProp => { | ||
delete cloneProps[maskProp]; | ||
}); | ||
return cloneProps; | ||
} | ||
get maskValue() { | ||
if (this.props.unmask === 'typed') return this.maskRef.typedValue; | ||
if (this.props.unmask) return this.maskRef.unmaskedValue; | ||
return this.maskRef.value; | ||
} | ||
set maskValue(value) { | ||
value = value == null ? '' : value; | ||
if (this.props.unmask === 'typed') this.maskRef.typedValue = value;else if (this.props.unmask) this.maskRef.unmaskedValue = value;else this.maskRef.value = value; | ||
} | ||
_onAccept(e) { | ||
if (this.props.onAccept && this.maskRef) this.props.onAccept(this.maskValue, this.maskRef, e); | ||
} | ||
_onComplete(e) { | ||
if (this.props.onComplete && this.maskRef) this.props.onComplete(this.maskValue, this.maskRef, e); | ||
} | ||
render() { | ||
return React.createElement(ComposedComponent, Object.assign({}, this._extractNonMaskProps(this.props), { | ||
inputRef: this._inputRef | ||
})); | ||
} | ||
}; | ||
const nestedComponentName = ComposedComponent.displayName || ComposedComponent.name || 'Component'; | ||
MaskedComponent.displayName = "IMask(".concat(nestedComponentName, ")"); | ||
MaskedComponent.propTypes = MASK_PROPS; | ||
return React.forwardRef(function (props, ref) { | ||
return React.createElement(MaskedComponent, Object.assign({}, props, { | ||
ref: ref | ||
})); | ||
}); | ||
return React.forwardRef((props, ref) => React.createElement(MaskedComponent, Object.assign({}, props, { | ||
ref | ||
}))); | ||
} | ||
export { IMaskMixin as default }; |
{ | ||
"name": "react-imask", | ||
"version": "6.5.0", | ||
"version": "6.5.1-alpha.0", | ||
"license": "MIT", | ||
@@ -23,3 +23,3 @@ "author": "Alexey Kryazhev", | ||
"dependencies": { | ||
"imask": "^6.5.0", | ||
"imask": "^6.5.1-alpha.0", | ||
"prop-types": "^15.8.1" | ||
@@ -33,3 +33,3 @@ }, | ||
}, | ||
"gitHead": "acad8efa189ad24f47932eaecf654b00d36f7ce7", | ||
"gitHead": "6d48a02bf6ecff7c39f52400a85638d42485288d", | ||
"devDependencies": { | ||
@@ -36,0 +36,0 @@ "@types/react": "^18.0.20", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
77570
753
1
Updatedimask@^6.5.1-alpha.0