UNPKG

@harvest-profit/npk

Version:
102 lines 4.62 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = require("react"); const Input_module_css_1 = __importDefault(require("./Input.module.css")); const BaseInput_1 = __importStar(require("../BaseInput")); const useMask_1 = __importDefault(require("../hooks/useMask")); const masks_1 = __importDefault(require("./masks")); const Input = ({ selectAllOnFocus = true, mask, value: externalValue, onChange: onExternalChange, debounce = false, readOnly = false, formatOptions = {}, ...props }) => { const inputProps = (0, BaseInput_1.useBaseInput)(props); const maskFunction = (typeof mask === 'string') ? masks_1.default[mask] : (mask || (() => null)); const inputMask = (0, useMask_1.default)({ mask: maskFunction(formatOptions), ...props }); const ref = (0, react_1.useRef)(null); const debounceRef = (0, react_1.useRef)(); const [internalValue, setInternalValue] = (0, react_1.useState)(inputMask.formatter(externalValue)); const [isFocused, setIsFocused] = (0, react_1.useState)(false); const onInternalChange = (value) => { setInternalValue(value); if (debounceRef.current) clearTimeout(debounceRef.current); debounceRef.current = setTimeout(() => { if (onExternalChange) onExternalChange(value); }, debounce ? (typeof debounce === 'number' ? debounce : 500) : 0); }; (0, react_1.useEffect)(() => { if (!isFocused) setInternalValue(inputMask.formatter(externalValue)); }, [externalValue]); const onFocus = () => { setIsFocused(true); if (!selectAllOnFocus) return; setTimeout(() => { if (ref.current) ref.current.select(); }, 30); }; const onBlur = () => { setIsFocused(false); if (debounceRef.current) clearTimeout(debounceRef.current); onInternalChange(inputMask.formatter(internalValue)); if (onExternalChange) onExternalChange(inputMask.formatter(internalValue)); }; const handleChangeEvent = (e) => { if (props.type === 'file') { onInternalChange(e.target.files); return; } onInternalChange(e.target.value); }; if (readOnly) { return ((0, jsx_runtime_1.jsx)(BaseInput_1.default, { ...props, variant: "plain", children: (0, jsx_runtime_1.jsx)("span", { ...inputProps, className: Input_module_css_1.default.Input, ref: ref, children: internalValue || '' }) })); } let InputComponent = 'input'; if (props.type === 'textarea') InputComponent = 'textarea'; return ((0, jsx_runtime_1.jsx)(BaseInput_1.default, { ...props, children: (0, jsx_runtime_1.jsx)(InputComponent, { ...inputProps, className: Input_module_css_1.default.Input, value: internalValue || '', onChange: handleChangeEvent, onFocus: onFocus, onBlur: onBlur, onKeyDown: inputMask.onKeyDown, ref: ref }) })); }; exports.default = Input; //# sourceMappingURL=Input.js.map