@etsoo/materialui
Version:
TypeScript Material-UI Implementation
148 lines (147 loc) • 5.49 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import React from "react";
import { FieldCheckbox } from "./FieldCheckbox";
import { FieldAmountLabel } from "./FieldAmountLabel";
import { FieldDateInput } from "./FieldDateInput";
import { FieldDivider } from "./FieldDivider";
import { FieldCombobox } from "./FieldCombobox";
import { FieldInput } from "./FieldInput";
import { FieldLabel } from "./FieldLabel";
import { FieldNumberInput } from "./FieldNumberInput";
import { FieldTexarea } from "./FieldTexarea";
import { FieldJson } from "./FieldJson";
import { FieldRadio } from "./FieldRadio";
import { FieldSelect } from "./FieldSelect";
import { FieldSwitch } from "./FieldSwitch";
import Grid from "@mui/material/Grid";
import Typography from "@mui/material/Typography";
/**
* Custom field utilities
*/
export var CustomFieldUtils;
(function (CustomFieldUtils) {
/**
* Custom field creators
*/
CustomFieldUtils.customFieldCreators = {
amountlabel: FieldAmountLabel,
checkbox: FieldCheckbox,
combobox: FieldCombobox,
date: FieldDateInput,
divider: FieldDivider,
input: FieldInput,
json: FieldJson,
label: FieldLabel,
number: FieldNumberInput,
radio: FieldRadio,
select: FieldSelect,
switch: FieldSwitch,
textarea: FieldTexarea
};
/**
* Create layout
* @param fields Fields
* @param collections Ref collections
* @param getValue Get default value callback
* @param onChange Callback for value change
* @param globalCallback Global label callback, can be repeated
* @param fieldCalback Field callback
* @returns
*/
function create(fields, collections, getValue, onChange, globalCallback, fieldCalback) {
return fields.map((field, index) => {
// Global callback for labels
if (globalCallback) {
if (field.label)
field.label = globalCallback(field.label);
if (field.helperText)
field.helperText = globalCallback(field.helperText);
if (field.mainSlotProps)
updateProperties(field.mainSlotProps, globalCallback);
}
// Field callback for each field
if (fieldCalback)
fieldCalback(field);
const creator = CustomFieldUtils.customFieldCreators[field.type];
if (creator == null) {
return (_jsx(Grid, { size: transformSpace(field.space), ...field.gridItemProps, children: `Type ${field.type} is not supported currently` }, index));
}
const Creator = creator;
const mref = React.createRef();
let ui = (_jsx(Creator, { field: field, mref: mref, onChange: onChange, defaultValue: getValue(field) }));
const name = field.name;
if (name) {
if (collections[name] == null) {
collections[name] = [mref, field];
}
else {
ui = `Duplicate custom field ${name}`;
}
}
return (_jsx(Grid, { size: transformSpace(field.space), ...field.gridItemProps, children: ui }, name ?? index));
});
}
CustomFieldUtils.create = create;
/**
* Create multiline label
* @param label Original label
* @param props Properties
* @returns Result
*/
function createMultilineLabel(label, props) {
return label?.split("\n").map((line, index) => (_jsx(Typography, { component: "div", ...props, children: line }, index)));
}
CustomFieldUtils.createMultilineLabel = createMultilineLabel;
/**
* Transform custom field space
* @param space Space
* @returns Result
*/
function transformSpace(space) {
const size = space === "full"
? { xs: 12 }
: space === "quater"
? { sm: 12, md: 6, lg: 3 }
: space === "five"
? { sm: 12, md: 5 }
: space === "seven"
? { sm: 12, md: 7 }
: space === "half1"
? { xs: 12, sm: 6 }
: { sm: 12, md: 6 };
return size;
}
CustomFieldUtils.transformSpace = transformSpace;
/**
* Update ref options
* @param fields Fields
* @param callback Callback
*/
function updateOptions(fields, callback) {
fields.forEach((field) => {
if (field.refs == null || field.refs.length < 2)
return;
const key = field.refs[0];
const ids = field.refs.slice(1);
field.options = callback(key, ids);
});
}
CustomFieldUtils.updateOptions = updateOptions;
/**
* Update properties
* @param input Input object
* @param globalCallback Global callback
*/
function updateProperties(input, globalCallback) {
for (const key in input) {
const value = Reflect.get(input, key);
if (typeof value === "string") {
Reflect.set(input, key, globalCallback(value));
}
else if (typeof value === "object" && value != null) {
updateProperties(value, globalCallback);
}
}
}
CustomFieldUtils.updateProperties = updateProperties;
})(CustomFieldUtils || (CustomFieldUtils = {}));