react-hook-mask
Version:
React masked input component and hooks
28 lines • 1.43 kB
JavaScript
import React from 'react';
import { useRefMask } from './use-ref-mask';
export var useWebMask = function (_a) {
var maskGenerator = _a.maskGenerator, value = _a.value, onChange = _a.onChange, keepMask = _a.keepMask, outerRef = _a.ref;
var getCursorPosition = React.useCallback(function (el) {
var _a;
var cursorPosition = (_a = el === null || el === void 0 ? void 0 : el.selectionStart) !== null && _a !== void 0 ? _a : 0;
return cursorPosition;
}, []);
var setCursorPosition = React.useCallback(function (cursorPosition, el) {
if (el) {
el.selectionStart = cursorPosition;
el.selectionEnd = cursorPosition;
}
}, []);
var _b = useRefMask({
value: value,
maskGenerator: maskGenerator,
getCursorPosition: getCursorPosition,
setCursorPosition: setCursorPosition,
onChange: onChange,
keepMask: keepMask,
ref: outerRef,
}), displayValue = _b.displayValue, setDisplayValue = _b.setDisplayValue, ref = _b.ref;
var handleOnChange = React.useCallback(function (e) { var _a, _b; return setDisplayValue((_b = (_a = e === null || e === void 0 ? void 0 : e.target) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : ''); }, [setDisplayValue]);
return { value: displayValue, onChange: handleOnChange, ref: ref };
};
//# sourceMappingURL=use-web-mask.js.map