react-hook-mask
Version:
React masked input component and hooks
40 lines • 1.95 kB
JavaScript
import React from 'react';
import { useMask } from './use-mask';
var useCombinedRefs = function (innerRef, fwdRef) {
React.useEffect(function () {
[innerRef, fwdRef].forEach(function (ref) {
if (ref) {
if (typeof ref === 'function') {
ref(innerRef.current || null);
}
else {
ref.current = innerRef.current || null;
}
}
});
}, [innerRef, fwdRef]);
return innerRef;
};
export var useRefMask = function (_a) {
var maskGenerator = _a.maskGenerator, outerValue = _a.value, onChange = _a.onChange, keepMask = _a.keepMask, ref = _a.ref, getCursorPositionOuter = _a.getCursorPosition, setCursorPositionOuter = _a.setCursorPosition;
var innerRef = React.useRef(null);
var combinedRefs = useCombinedRefs(innerRef, ref);
var getCursorPosition = React.useCallback(function () {
var el = combinedRefs === null || combinedRefs === void 0 ? void 0 : combinedRefs.current;
return getCursorPositionOuter(el !== null && el !== void 0 ? el : undefined);
}, [combinedRefs, getCursorPositionOuter]);
var setCursorPosition = React.useCallback(function (cursorPosition) {
var el = combinedRefs === null || combinedRefs === void 0 ? void 0 : combinedRefs.current;
setCursorPositionOuter(cursorPosition, el !== null && el !== void 0 ? el : undefined);
}, [combinedRefs, setCursorPositionOuter]);
var _b = useMask({
outerValue: outerValue,
maskGenerator: maskGenerator,
getCursorPosition: getCursorPosition,
setCursorPosition: setCursorPosition,
onChange: onChange,
keepMask: keepMask,
}), displayValue = _b.displayValue, setDisplayValue = _b.setDisplayValue;
return { displayValue: displayValue, setDisplayValue: setDisplayValue, ref: combinedRefs };
};
//# sourceMappingURL=use-ref-mask.js.map