@etsoo/materialui
Version:
TypeScript Material-UI Implementation
44 lines (43 loc) • 1.57 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomFieldUI = CustomFieldUI;
const react_1 = __importDefault(require("react"));
const CustomFieldUtils_1 = require("./CustomFieldUtils");
const ReactApp_1 = require("../app/ReactApp");
/**
* CustomFieldUI component
* @param props Props
* @returns component
*/
function CustomFieldUI(props) {
// Destruct
const { fields, initialValue, mref, onChange } = props;
// App
const app = (0, ReactApp_1.useAppContext)();
// Field component collections
const collections = {};
// Value reference
const valueRef = react_1.default.useRef({ ...initialValue });
// Methods
react_1.default.useImperativeHandle(mref, () => ({
getValue: () => valueRef.current,
setValue: (value) => {
if (!!value && typeof value === "object") {
valueRef.current = { ...value };
CustomFieldUtils_1.CustomFieldUtils.updateValues(collections, valueRef.current);
}
}
}), []);
// Layout
return CustomFieldUtils_1.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);
}