UNPKG

infinity-forge

Version:
97 lines 5.32 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.InputCurrency = InputCurrency; var jsx_runtime_1 = require("react/jsx-runtime"); var react_1 = require("react"); var formik_1 = require("formik"); var input_control_1 = require("../input-control/index.js"); var components_1 = require("./components/index.js"); function InputCurrency(props) { var _a = (0, formik_1.useField)({ name: props.name }), field = _a[0], _ = _a[1], handlers = _a[2]; var _b = (0, react_1.useState)(field.value), tempValue = _b[0], setTempValue = _b[1]; function handleChange(value) { var valueToString = typeof value === 'number' ? String(value) : value; if (valueToString) { var numericValue = parseFloat((valueToString === null || valueToString === void 0 ? void 0 : valueToString.includes(',')) ? valueToString === null || valueToString === void 0 ? void 0 : valueToString.replace(/\./g, '').replace(',', '.') : valueToString); if (props.min && numericValue < Number(props.min)) { if (props.errorMessageMin) { handlers.setError(props.errorMessageMin(valueToString)); } handlers.setValue(Number(props.min)); (props === null || props === void 0 ? void 0 : props.onChangeDispatcher) && props.onChangeDispatcher(Number(props.min)); return; } if (props.max && numericValue > Number(props.max || 0)) { if (props.errorMessageMax) { handlers.setError(props.errorMessageMax(valueToString)); } } else { if (props.onChangeMode !== 'blur') { handlers.setValue(valueToString); } handlers.setError(undefined); props.onChangeInput && props.onChangeInput(valueToString); (props === null || props === void 0 ? void 0 : props.onChangeDispatcher) && props.onChangeDispatcher(Number(valueToString)); } return; } if (props.onChangeMode !== 'blur') { handlers.setValue(''); } handlers.setError(undefined); props.onChangeInput && props.onChangeInput('0'); (props === null || props === void 0 ? void 0 : props.onChangeDispatcher) && props.onChangeDispatcher(0); } var handleBlur = function () { if (tempValue !== field.value) { handlers.setValue(tempValue); (props === null || props === void 0 ? void 0 : props.onChangeBlur) && props.onChangeBlur(tempValue); } }; function handleKeyDown(e) { if (props.decimalLimit === 0 && (e.key === ',' || e.key === '.')) { e.preventDefault(); } } var value = (0, react_1.useMemo)(function () { return (props.onChangeMode === 'blur' ? tempValue : typeof props.value !== 'undefined' ? props.value : field.value); }, [field.value, tempValue, props.value]); (0, react_1.useEffect)(function () { if (props.onChangeMode === 'blur') { setTempValue(field.value); } }, [field.value]); (0, react_1.useEffect)(function () { if (typeof props.value !== 'undefined') { handleChange(props === null || props === void 0 ? void 0 : props.value); } }, [props === null || props === void 0 ? void 0 : props.value]); (0, react_1.useEffect)(function () { var _a; if ((_a = props === null || props === void 0 ? void 0 : props.controlledInitialValue) === null || _a === void 0 ? void 0 : _a.value) { setTimeout(function () { var _a; handleChange((_a = props === null || props === void 0 ? void 0 : props.controlledInitialValue) === null || _a === void 0 ? void 0 : _a.value); }, 500); } }, []); return ((0, jsx_runtime_1.jsx)(input_control_1.InputControl, __assign({}, props, { i18n: { disabled: true }, children: (0, jsx_runtime_1.jsx)(components_1.CurrencyInput, __assign({}, props, { prefix: typeof (props === null || props === void 0 ? void 0 : props.prefix) !== 'string' ? 'R$ ' : props.prefix, decimalsLimit: (props === null || props === void 0 ? void 0 : props.decimalLimit) || 2, decimalSeparator: (props === null || props === void 0 ? void 0 : props.decimalLimit) === 0 ? '' : ',', disableGroupSeparators: (props === null || props === void 0 ? void 0 : props.decimalLimit) === 0, groupSeparator: '.', value: value, onValueChange: function (value) { if (props.onChangeMode === 'blur') { setTempValue(value); } else { handleChange(value); } }, onKeyDown: handleKeyDown, onBlur: props.onChangeMode === 'blur' ? handleBlur : undefined, readOnly: props.readOnly, max: props.max })) }))); } //# sourceMappingURL=index.js.map