UNPKG

drf-react-by-schema

Version:

Components and Tools for building a React App having Django Rest Framework (DRF) as server

90 lines (89 loc) 4.54 kB
"use strict"; 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 = AutocompleteFieldBySchema; const react_1 = __importDefault(require("react")); const react_hook_form_1 = require("react-hook-form"); const Autocomplete_1 = __importDefault(require("@mui/material/Autocomplete")); const TextField_1 = __importDefault(require("@mui/material/TextField")); const utils_1 = require("../../../utils"); const DRFReactBySchemaContext_1 = require("../../../context/DRFReactBySchemaContext"); const renderInput = (_a) => { var { control: _, options, optionLabelKey, schema, name, errors, fieldKey, index, isSemaphoric, label, theme } = _a, params = __rest(_a, ["control", "options", "optionLabelKey", "schema", "name", "errors", "fieldKey", "index", "isSemaphoric", "label", "theme"]); const model = 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 || '', }; let sx = undefined; if (isSemaphoric && options && optionLabelKey) { let tipo; switch (params.inputProps.value) { case options[0][optionLabelKey]: tipo = 'success'; break; case options[1][optionLabelKey]: tipo = 'warning'; break; case options[2][optionLabelKey]: tipo = 'error'; break; } if (tipo) { sx = { backgroundColor: theme.palette[tipo].semaphoric }; } } return (react_1.default.createElement(TextField_1.default, Object.assign({}, params, { label: label || schema[model].label, required: schema[model].required, margin: "normal", error: error, helperText: helperText, sx: sx }))); }; function AutocompleteFieldBySchema(_a) { var { fieldKey, index, name, schema, control, errors, optionIdKey = 'id', optionLabelKey = 'label', options, isSemaphoric = false, sx = { mr: 2 }, onValueChange, autoFocus, disabled } = _a, other = __rest(_a, ["fieldKey", "index", "name", "schema", "control", "errors", "optionIdKey", "optionLabelKey", "options", "isSemaphoric", "sx", "onValueChange", "autoFocus", "disabled"]); const model = name; const { theme } = (0, DRFReactBySchemaContext_1.useDRFReactBySchema)(); if (!options) { options = schema[model].choices; } if (fieldKey && index && index >= 0) { name = `${fieldKey}.${index}.${name}`; } return (react_1.default.createElement(react_hook_form_1.Controller, { name: name, control: control, render: ({ field }) => (react_1.default.createElement(Autocomplete_1.default, Object.assign({}, field, { key: name, id: name, options: options || [], disabled: schema[model].disabled === true || disabled === true, selectOnFocus: true, autoHighlight: true, autoFocus: autoFocus, isOptionEqualToValue: (option, value) => { return option[optionIdKey] === value[optionIdKey]; }, getOptionLabel: (option) => { return option[optionLabelKey]; }, onChange: (e, value) => { field.onChange(value); if (onValueChange) { onValueChange(e); } }, fullWidth: true, renderInput: (params) => { return renderInput(Object.assign(Object.assign({}, params), { control, isSemaphoric, options, optionLabelKey, schema, name, errors, fieldKey, index, theme })); }, sx: sx }, other))) })); }