UNPKG

drf-react-by-schema

Version:

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

40 lines (39 loc) 2.39 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 = BooleanFieldBySchema; const react_1 = __importDefault(require("react")); const react_hook_form_1 = require("react-hook-form"); const Checkbox_1 = __importDefault(require("@mui/material/Checkbox")); const FormControlLabel_1 = __importDefault(require("@mui/material/FormControlLabel")); const utils_1 = require("../../../utils"); function BooleanFieldBySchema(_a) { var { name, schema, control, fieldKey, index, sx = { mr: 2 }, onValueChange, autoFocus, disabled } = _a, other = __rest(_a, ["name", "schema", "control", "fieldKey", "index", "sx", "onValueChange", "autoFocus", "disabled"]); if (!(name in schema)) { console.error(`BooleanFieldBySchema: could not find schema for ${name}! Not rendering`); return; } const fieldSchema = schema[name]; 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(FormControlLabel_1.default, { control: react_1.default.createElement(Checkbox_1.default, Object.assign({}, field, other, { value: field.value || false, checked: field.value || false, disabled: fieldSchema.disabled === true || disabled === true, inputProps: { 'aria-label': 'controlled' }, onChange: (e) => { if (onValueChange) { onValueChange(e); } field.onChange(e); }, autoFocus: autoFocus })), label: fieldSchema.label, required: (0, utils_1.isFieldRequired)(fieldSchema), sx: Object.assign({ width: '100%' }, sx) })) })); }