@etsoo/materialui
Version:
TypeScript Material-UI Implementation
38 lines (37 loc) • 1.23 kB
JavaScript
import React from "react";
import { CustomFieldUtils } from "./CustomFieldUtils";
import { useAppContext } from "../app/ReactApp";
/**
* CustomFieldUI component
* @param props Props
* @returns component
*/
export function CustomFieldUI(props) {
// Destruct
const { fields, initialValue, mref, onChange } = props;
// App
const app = useAppContext();
// Field component collections
const collections = {};
// Value reference
const valueRef = React.useRef({ ...initialValue });
// Methods
React.useImperativeHandle(mref, () => ({
getValue: () => valueRef.current,
setValue: (value) => {
if (!!value && typeof value === "object") {
valueRef.current = { ...value };
CustomFieldUtils.updateValues(collections, valueRef.current);
}
}
}), []);
// Layout
return CustomFieldUtils.create(fields, collections, (field) => {
if (!field.name)
return undefined;
return valueRef.current[field.name];
}, (name, fieldValue) => {
valueRef.current[name] = fieldValue;
onChange?.(valueRef.current, name, fieldValue);
}, (input) => app?.get(input) ?? input);
}