react-hook-mask
Version:
React masked input component and hooks
35 lines • 1.77 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useWebMask = void 0;
var react_1 = __importDefault(require("react"));
var use_ref_mask_1 = require("./use-ref-mask");
var useWebMask = function (_a) {
var maskGenerator = _a.maskGenerator, value = _a.value, onChange = _a.onChange, keepMask = _a.keepMask, outerRef = _a.ref;
var getCursorPosition = react_1.default.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_1.default.useCallback(function (cursorPosition, el) {
if (el) {
el.selectionStart = cursorPosition;
el.selectionEnd = cursorPosition;
}
}, []);
var _b = (0, use_ref_mask_1.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_1.default.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 };
};
exports.useWebMask = useWebMask;
//# sourceMappingURL=use-web-mask.js.map