UNPKG

infinity-forge

Version:
166 lines 11 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); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.InputCep = InputCep; var jsx_runtime_1 = require("react/jsx-runtime"); var react_1 = require("react"); var formik_1 = require("formik"); var use_cep_1 = require("./use-cep/index.js"); var ui_1 = require("../../../../ui/index.js"); function InputCep(props) { var _this = this; var _a; var _b = (0, react_1.useState)(false), loading = _b[0], setLoading = _b[1]; var nameZipCode = props.nameZipCode || 'zipCode'; var cep = (0, use_cep_1.useCep)(props); var _c = (0, formik_1.useFormikContext)(), setFieldValue = _c.setFieldValue, values = _c.values; function findFieldByKey(fields, key) { for (var _i = 0, _a = fields !== null && fields !== void 0 ? fields : []; _i < _a.length; _i++) { var row = _a[_i]; if (row[key]) { return row[key]; } } return null; } (0, react_1.useEffect)(function () { var _a; var rawCep = (_a = values === null || values === void 0 ? void 0 : values.address) === null || _a === void 0 ? void 0 : _a[nameZipCode]; if (rawCep && typeof rawCep === 'string' && rawCep.includes('-')) { var cleanedCep = rawCep.replace('-', ''); setFieldValue("address.".concat(nameZipCode), cleanedCep); } }, [nameZipCode, values === null || values === void 0 ? void 0 : values.address]); (0, react_1.useEffect)(function () { var _a; var input = document.querySelector('input[name="address.numero"]'); if (input && input.value && ((_a = values === null || values === void 0 ? void 0 : values.address) === null || _a === void 0 ? void 0 : _a.numero) !== input.value) { setFieldValue('address.numero', input.value); } }, [values === null || values === void 0 ? void 0 : values.address]); return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(ui_1.InputMask, { mask: '________', type: 'text', name: "address.".concat(nameZipCode), placeholder: 'CEP', label: 'CEP*', readOnly: props.readOnly, onChangeInput: function (value_1, _1, _a) { return __awaiter(_this, [value_1, _1, _a], void 0, function (value, _, _b) { var response, data, hasCustomName, _c; var _d; var _e; var state = _b.state, utils = _b.utils; return __generator(this, function (_f) { switch (_f.label) { case 0: if (props === null || props === void 0 ? void 0 : props.disableSearchCep) { return [2 /*return*/]; } _f.label = 1; case 1: _f.trys.push([1, 3, 4, 5]); setLoading(true); if (value.length !== 8) { return [2 /*return*/]; } if (value === state.initialValue) { return [2 /*return*/]; } return [4 /*yield*/, cep.getCep(value)]; case 2: response = _f.sent(); if (response === null || response === void 0 ? void 0 : response.erro) { utils.setError('Cep inválido'); return [2 /*return*/]; } data = props.transformData ? props.transformData(response) : response; hasCustomName = (_e = props.fields) === null || _e === void 0 ? void 0 : _e.some(function (row) { return Object.values(row).some(function (field) { return !!(field === null || field === void 0 ? void 0 : field.name); }); }); if (hasCustomName) { Object.entries(data).forEach(function (_a) { var key = _a[0], val = _a[1]; var fieldConfig = findFieldByKey(props.fields, key); if ((fieldConfig === null || fieldConfig === void 0 ? void 0 : fieldConfig.name) && !fieldConfig.name.startsWith('address.')) { setFieldValue(fieldConfig.name, val); } }); if (!nameZipCode.startsWith('address.')) { setFieldValue(nameZipCode, value); } } else { setFieldValue('address', __assign(__assign({}, data), (_d = {}, _d[nameZipCode] = value, _d))); } return [3 /*break*/, 5]; case 3: _c = _f.sent(); utils.setError('Cep inválido'); return [3 /*break*/, 5]; case 4: setLoading(false); return [7 /*endfinally*/]; case 5: return [2 /*return*/]; } }); }); } }), (0, jsx_runtime_1.jsx)("div", { children: (_a = cep === null || cep === void 0 ? void 0 : cep.fields) === null || _a === void 0 ? void 0 : _a.map(function (fieldsRow) { var key = Object.keys(fieldsRow).reduce(function (reducer, item) { return item + reducer; }, ''); return ((0, jsx_runtime_1.jsx)(Field, { onChangeMode: props.onChangeMode, loading: loading, fieldsRow: fieldsRow, nameZipCode: nameZipCode, omitFields: props.omitFields, showAllFields: props.showAllFields, readOnly: props.readOnly }, key)); }) })] })); } function Field(_a) { var _b, _c, _d; var omitFields = _a.omitFields, fieldsRow = _a.fieldsRow, loading = _a.loading, showAllFields = _a.showAllFields, nameZipCode = _a.nameZipCode, onChangeMode = _a.onChangeMode, readOnly = _a.readOnly; var _e = (0, formik_1.useFormikContext)(), values = _e.values, errors = _e.errors; var isValidCep = showAllFields ? true : ((_c = (_b = values === null || values === void 0 ? void 0 : values.address) === null || _b === void 0 ? void 0 : _b[nameZipCode]) === null || _c === void 0 ? void 0 : _c.length) === 8 && !loading && !((_d = errors.address) === null || _d === void 0 ? void 0 : _d[nameZipCode]); return ((0, jsx_runtime_1.jsx)("div", { style: { display: isValidCep ? 'flex' : 'none', gap: 15 }, children: Object.keys(fieldsRow).map(function (field) { var configsField = fieldsRow[field]; if (omitFields === null || omitFields === void 0 ? void 0 : omitFields.includes(field)) { return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, {}); } if (configsField === null || configsField === void 0 ? void 0 : configsField.Component) { return ((0, jsx_runtime_1.jsx)(configsField.Component, __assign({ readOnly: readOnly, name: (configsField === null || configsField === void 0 ? void 0 : configsField.name) || 'address.' + field }, configsField), field)); } return ((0, jsx_runtime_1.jsx)(ui_1.Input, __assign({ readOnly: readOnly }, configsField, { name: (configsField === null || configsField === void 0 ? void 0 : configsField.name) || 'address.' + field, onChangeMode: onChangeMode, i18n: { disabled: true } }), field)); }) })); } //# sourceMappingURL=component.js.map