@etsoo/materialui
Version:
TypeScript Material-UI Implementation
49 lines (48 loc) • 1.63 kB
JavaScript
import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
import React from "react";
import { SelectEx } from "../SelectEx";
import Typography from "@mui/material/Typography";
/**
* Select (single value) field creator
* type select
* @returns Component
*/
export const FieldSelect = ({ field, mref, onChange, defaultValue }) => {
// State
const [value, setLocalValue] = React.useState();
const getValue = () => value;
const setValue = (value) => {
if (Array.isArray(value)) {
setLocalValue(value[0]);
}
else if (typeof value === "string" || typeof value === "number") {
setLocalValue(value);
}
else {
setLocalValue(undefined);
}
};
// Ref
React.useImperativeHandle(mref, () => ({
getValue,
setValue
}));
React.useEffect(() => {
if (defaultValue == null)
return;
setValue(defaultValue);
}, [defaultValue]);
// Name
const name = field.name;
if (!name) {
return (_jsxs(Typography, { children: ["No name for FieldSelect ", JSON.stringify(field)] }));
}
return (_jsx(SelectEx, { label: field.label ?? "", helperText: field.helperText, name: name, options: field.options, fullWidth: true, value: value, onChange: (event) => {
const value = event.target.value;
const newValue = typeof value === "string" || typeof value === "number"
? value
: undefined;
setLocalValue(newValue);
onChange(name, newValue);
}, ...field.mainSlotProps }));
};