@etsoo/materialui
Version:
TypeScript Material-UI Implementation
99 lines (98 loc) • 4.87 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OptionGroupFlag = OptionGroupFlag;
const jsx_runtime_1 = require("react/jsx-runtime");
const shared_1 = require("@etsoo/shared");
const Box_1 = __importDefault(require("@mui/material/Box"));
const Checkbox_1 = __importDefault(require("@mui/material/Checkbox"));
const FormControl_1 = __importDefault(require("@mui/material/FormControl"));
const FormControlLabel_1 = __importDefault(require("@mui/material/FormControlLabel"));
const FormGroup_1 = __importDefault(require("@mui/material/FormGroup"));
const FormHelperText_1 = __importDefault(require("@mui/material/FormHelperText"));
const InputLabel_1 = __importDefault(require("@mui/material/InputLabel"));
const OutlinedInput_1 = __importDefault(require("@mui/material/OutlinedInput"));
const react_1 = __importDefault(require("react"));
/**
* OptionGroupFlag
* @param props Props
* @returns Component
*/
function OptionGroupFlag(props) {
// Destruct
const { getOptionLabel, defaultValue, idField = "id", label, labelField = "label", mRef, name, onValueChange, options, readOnly, row, itemSize, helperText, variant, required, fullWidth, sx = {}, ...rest } = props;
// Outlined
const outlined = variant === "outlined";
// Get option value
// D type should be the source id type
const getOptionValue = (option) => {
const value = shared_1.DataTypes.getValue(option, idField);
if (value == null)
return null;
return value;
};
// Value
const [value, setValue] = react_1.default.useState(defaultValue);
react_1.default.useEffect(() => {
setValue(defaultValue);
}, [defaultValue]);
// Disabled ids
const [disabledIds, setDisabledIds] = react_1.default.useState();
// Item checked
const itemChecked = (option) => {
// Value
const itemValue = getOptionValue(option);
if (itemValue == null || value == null)
return false;
return (value & itemValue) > 0;
};
react_1.default.useImperativeHandle(mRef, () => ({
disable(ids) {
setDisabledIds(ids);
}
}));
// First item value
const firstOptionValue = getOptionValue(options[0]);
// Items
const list = options.map((option) => {
// Value
const ov = getOptionValue(option);
if (ov == null)
return;
// Control
const control = ((0, jsx_runtime_1.jsx)(Checkbox_1.default, { name: name, readOnly: readOnly, size: itemSize, checked: itemChecked(option), disabled: disabledIds?.includes(ov), onChange: (event) => {
if (firstOptionValue == null)
return;
const typeValue = shared_1.Utils.parseString(event.target.value, firstOptionValue);
const newValue = (value == null
? event.target.checked
? typeValue
: undefined
: event.target.checked
? value | typeValue
: value ^ typeValue);
if (onValueChange)
onValueChange(newValue);
setValue(newValue);
} }));
// Label
const label = getOptionLabel == null ? `${option[labelField]}` : getOptionLabel(option);
return ((0, jsx_runtime_1.jsx)(FormControlLabel_1.default, { control: control, value: ov, label: label }, ov));
});
// Group
const group = (0, jsx_runtime_1.jsx)(FormGroup_1.default, { row: row, children: list });
// Layout
return ((0, jsx_runtime_1.jsxs)(react_1.default.Fragment, { children: [(0, jsx_runtime_1.jsxs)(FormControl_1.default, { fullWidth: fullWidth, sx: sx, ...rest, children: [label && ((0, jsx_runtime_1.jsx)(InputLabel_1.default, { required: required, variant: variant, shrink: true, children: label })), outlined ? ((0, jsx_runtime_1.jsx)(OutlinedInput_1.default, { label: label && required ? label + " *" : label, notched: true, endAdornment: group, sx: {
cursor: "default",
display: "flex",
gap: 1,
paddingX: 2,
paddingY: "7px",
width: fullWidth ? "100%" : "auto",
"& input": {
display: "none"
}
} })) : ((0, jsx_runtime_1.jsx)(Box_1.default, { paddingLeft: 2, paddingY: "7px", children: group }))] }), helperText && ((0, jsx_runtime_1.jsx)(FormHelperText_1.default, { sx: { marginLeft: 2, marginRight: 2 }, children: helperText }))] }));
}