@etsoo/materialui
Version:
TypeScript Material-UI Implementation
58 lines (57 loc) • 2.05 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FieldJson = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = __importDefault(require("react"));
const InputField_1 = require("../InputField");
const Typography_1 = __importDefault(require("@mui/material/Typography"));
function parseJson(value) {
if (value) {
try {
return JSON.parse(value);
}
catch (error) {
return undefined;
}
}
return undefined;
}
/**
* Textarea field creator
* type: json
* @returns Component
*/
const FieldJson = ({ field, mref, onChange, defaultValue }) => {
// Ref
const inputRef = react_1.default.useRef();
const getValue = () => parseJson(inputRef.current?.value);
const setValue = (value) => {
if (inputRef.current) {
const obj = typeof value === "object"
? value
: typeof value === "string"
? parseJson(value)
: undefined;
inputRef.current.value = obj ? JSON.stringify(obj, null, 4) : "";
}
};
react_1.default.useImperativeHandle(mref, () => ({
getValue,
setValue
}));
react_1.default.useEffect(() => {
if (defaultValue == null)
return;
setValue(defaultValue);
}, [defaultValue]);
// Name
const name = field.name;
if (!name) {
return ((0, jsx_runtime_1.jsxs)(Typography_1.default, { children: ["No name for FieldJson ", JSON.stringify(field)] }));
}
return ((0, jsx_runtime_1.jsx)(InputField_1.InputField, { label: field.label ?? "", helperText: field.helperText, name: name, fullWidth: true, multiline: true, rows: 4, inputProps: { maxLength: 1280 }, inputRef: inputRef, onChange: () => onChange(name, getValue()), ...field.mainSlotProps }));
};
exports.FieldJson = FieldJson;