web-base/react/shared/elements/dialog.jsx

93 lines
2.7 KiB
React
Raw Normal View History

import React, {useEffect, useState} from "react";
import {
Box,
Button,
Dialog as MuiDialog,
DialogActions,
DialogContent,
DialogContentText,
DialogTitle,
2023-01-22 12:32:18 +01:00
Input, List, ListItem, TextField
} from "@mui/material";
2023-01-14 09:51:46 +01:00
export default function Dialog(props) {
const show = props.show;
const onClose = props.onClose || function() { };
const onOption = props.onOption || function() { };
const options = props.options || ["Close"];
const inputs = props.inputs || [];
2023-01-18 14:37:34 +01:00
const [inputData, setInputData] = useState({});
2023-01-14 09:51:46 +01:00
useEffect(() => {
if (props.inputs) {
let initialData = {};
for (const input of props.inputs) {
initialData[input.name] = input.value || "";
}
setInputData(initialData);
}
}, [props.inputs]);
2023-01-14 09:51:46 +01:00
let buttons = [];
for (const [index, name] of options.entries()) {
2023-01-14 09:51:46 +01:00
buttons.push(
<Button variant={"outlined"} size={"small"} key={"button-" + name}
onClick={() => { onClose(); onOption(index, inputData); setInputData({}); }}>
2023-01-14 09:51:46 +01:00
{name}
2023-01-15 00:32:17 +01:00
</Button>
2023-01-14 09:51:46 +01:00
)
}
let inputElements = [];
for (const input of inputs) {
let inputProps = { ...input };
delete inputProps.name;
delete inputProps.type;
switch (input.type) {
case 'text':
inputElements.push(<TextField
{...inputProps}
sx={{marginTop: 1}}
size={"small"} fullWidth={true}
key={"input-" + input.name}
value={inputData[input.name] || ""}
onChange={e => setInputData({ ...inputData, [input.name]: e.target.value })}
/>)
break;
2023-01-22 12:32:18 +01:00
case 'list':
delete inputProps.items;
let listItems = input.items.map((item, index) => <ListItem key={"item-" + index}>{item}</ListItem>);
inputElements.push(<Box
{...inputProps}
sx={{marginTop: 1}}
key={"input-" + input.name}
>
<List>
{listItems}
</List>
</Box>);
}
}
2023-01-18 14:37:34 +01:00
return <MuiDialog
2023-01-15 00:32:17 +01:00
open={show}
onClose={onClose}>
<DialogTitle>
{ props.title }
</DialogTitle>
2023-01-18 14:37:34 +01:00
<DialogContent>
<DialogContentText>
{ props.message }
</DialogContentText>
<Box mt={2}>
{ inputElements }
</Box>
2023-01-18 14:37:34 +01:00
</DialogContent>
<DialogActions>
{ buttons }
2023-01-18 14:37:34 +01:00
</DialogActions>
</MuiDialog>
2023-01-14 09:51:46 +01:00
}