drf-react-by-schema
Version:
Components and Tools for building a React App having Django Rest Framework (DRF) as server
125 lines (124 loc) • 6.19 kB
JavaScript
;
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 __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 = __importStar(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 fieldSchema = schema[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 : fieldSchema.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 || fieldSchema.label, required: (0, utils_1.isFieldRequired)(fieldSchema), margin: "normal", error: error, helperText: helperText, sx: sx })));
};
function AutocompleteFieldBySchema(_a) {
var { fieldKey, index, name, schema, control, errors, optionIdKey = 'id', optionLabelKey = 'label', options: optionsByProps, isSemaphoric = false, sx = { mr: 2 }, onValueChange, autoFocus, disabled, autoComplete: _ } = _a, other = __rest(_a, ["fieldKey", "index", "name", "schema", "control", "errors", "optionIdKey", "optionLabelKey", "options", "isSemaphoric", "sx", "onValueChange", "autoFocus", "disabled", "autoComplete"]);
const { theme } = (0, DRFReactBySchemaContext_1.useDRFReactBySchema)();
const fieldSchema = schema[name];
const options = (0, react_1.useMemo)(() => optionsByProps || fieldSchema.choices, [optionsByProps, fieldSchema]);
if (!(name in schema)) {
console.error(`AutocompleteFieldBySchema: could not find schema for ${name}! Not rendering`);
return;
}
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: fieldSchema.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))) }));
}