drf-react-by-schema
Version:
Components and Tools for building a React App having Django Rest Framework (DRF) as server
60 lines (59 loc) • 3.21 kB
JavaScript
;
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = FloatFieldBySchema;
const react_1 = __importDefault(require("react"));
const react_hook_form_1 = require("react-hook-form");
const TextField_1 = __importDefault(require("@mui/material/TextField"));
const react_number_format_1 = require("react-number-format");
const utils_1 = require("../../../utils");
function FloatFieldBySchema(_a) {
var { name, schema, control, errors, fieldKey, index, onValueChange, decimalScale, label, autoFocus, disabled } = _a, other = __rest(_a, ["name", "schema", "control", "errors", "fieldKey", "index", "onValueChange", "decimalScale", "label", "autoFocus", "disabled"]);
const model = name;
if (!label) {
label = schema[model].label;
}
if (fieldKey && index && index >= 0) {
name = `${fieldKey}.${index}.${name}`;
}
const { error, helperText } = fieldKey && index && index >= 0
? (0, utils_1.errorProps)({
fieldKey,
index,
fieldKeyProp: name,
errors,
})
: {
error: errors && Boolean(errors[name]),
helperText: errors && errors[name] ? errors[name].message : schema[name].help_text || '',
};
const prefix = schema[model].is_currency
? 'R$ '
: schema[model].prefix !== ''
? `${schema[model].prefix} `
: '';
const suffix = schema[model].suffix !== '' ? ` ${schema[model].suffix}` : '';
const decimals = decimalScale || schema[model].decimal_places || 2;
return (react_1.default.createElement(react_hook_form_1.Controller, { name: name, control: control, render: ({ field }) => {
const moedaInputProps = Object.assign(Object.assign({}, field), { ref: undefined, onChange: undefined });
return (react_1.default.createElement(react_number_format_1.NumericFormat, Object.assign({}, moedaInputProps, other, { label: label, disabled: schema[model].disabled === true || disabled === true, required: schema[model].required, onValueChange: (values) => {
field.onChange(values.floatValue);
if (onValueChange) {
onValueChange(values.floatValue);
}
}, thousandSeparator: ".", decimalSeparator: ",", decimalScale: decimals, fixedDecimalScale: true, valueIsNumericString: true, prefix: prefix, suffix: suffix, error: error, helperText: helperText, margin: "normal", customInput: TextField_1.default, autoFocus: autoFocus })));
} }));
}