2023-01-20 12:16:18 +01:00
|
|
|
import React, {useEffect, useState} from "react";
|
2023-01-19 18:12:16 +01:00
|
|
|
import {
|
|
|
|
Box,
|
|
|
|
Button,
|
|
|
|
Dialog as MuiDialog,
|
|
|
|
DialogActions,
|
|
|
|
DialogContent,
|
|
|
|
DialogContentText,
|
|
|
|
DialogTitle,
|
2024-03-27 16:27:26 +01:00
|
|
|
List, ListItem, TextField
|
2023-01-19 18:12:16 +01:00
|
|
|
} 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"];
|
2023-01-19 18:12:16 +01:00
|
|
|
const inputs = props.inputs || [];
|
2023-01-18 14:37:34 +01:00
|
|
|
|
2023-01-19 18:12:16 +01:00
|
|
|
const [inputData, setInputData] = useState({});
|
2023-01-14 09:51:46 +01:00
|
|
|
|
2023-01-20 12:16:18 +01:00
|
|
|
useEffect(() => {
|
|
|
|
if (props.inputs) {
|
|
|
|
let initialData = {};
|
|
|
|
for (const input of props.inputs) {
|
2024-04-02 15:33:00 +02:00
|
|
|
if (input.type !== "label" && input.hasOwnProperty("name")) {
|
2024-03-27 16:27:26 +01:00
|
|
|
initialData[input.name] = input.value || "";
|
|
|
|
}
|
2023-01-20 12:16:18 +01:00
|
|
|
}
|
|
|
|
setInputData(initialData);
|
|
|
|
}
|
|
|
|
}, [props.inputs]);
|
|
|
|
|
2023-01-14 09:51:46 +01:00
|
|
|
let buttons = [];
|
2023-01-19 18:12:16 +01:00
|
|
|
for (const [index, name] of options.entries()) {
|
2023-01-14 09:51:46 +01:00
|
|
|
buttons.push(
|
2023-01-19 18:12:16 +01:00
|
|
|
<Button variant={"outlined"} size={"small"} key={"button-" + name}
|
2023-01-20 12:16:18 +01:00
|
|
|
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
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-01-19 18:12:16 +01:00
|
|
|
let inputElements = [];
|
|
|
|
for (const input of inputs) {
|
|
|
|
let inputProps = { ...input };
|
|
|
|
delete inputProps.name;
|
|
|
|
delete inputProps.type;
|
|
|
|
|
|
|
|
switch (input.type) {
|
2024-03-27 16:27:26 +01:00
|
|
|
case 'label':
|
2024-03-27 20:50:57 +01:00
|
|
|
delete inputProps.value;
|
2024-03-27 16:27:26 +01:00
|
|
|
inputElements.push(<span {...inputProps}>{input.value}</span>);
|
|
|
|
break;
|
2023-01-19 18:12:16 +01:00
|
|
|
case 'text':
|
2023-03-05 15:30:06 +01:00
|
|
|
case 'password':
|
2023-01-19 18:12:16 +01:00
|
|
|
inputElements.push(<TextField
|
|
|
|
{...inputProps}
|
2023-03-05 15:30:06 +01:00
|
|
|
type={input.type}
|
2023-01-19 18:12:16 +01:00
|
|
|
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>);
|
2024-04-02 15:33:00 +02:00
|
|
|
break;
|
|
|
|
case 'custom':
|
|
|
|
let element = inputProps.element;
|
|
|
|
delete inputProps.element;
|
|
|
|
inputElements.push(React.createElement(element, inputProps));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2023-01-19 18:12:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-18 14:37:34 +01:00
|
|
|
return <MuiDialog
|
2023-01-15 00:32:17 +01:00
|
|
|
open={show}
|
2023-01-19 18:12:16 +01:00
|
|
|
onClose={onClose}>
|
|
|
|
<DialogTitle>
|
|
|
|
{ props.title }
|
|
|
|
</DialogTitle>
|
2023-01-18 14:37:34 +01:00
|
|
|
<DialogContent>
|
|
|
|
<DialogContentText>
|
|
|
|
{ props.message }
|
|
|
|
</DialogContentText>
|
2023-01-19 18:12:16 +01:00
|
|
|
<Box mt={2}>
|
|
|
|
{ inputElements }
|
|
|
|
</Box>
|
2023-01-18 14:37:34 +01:00
|
|
|
</DialogContent>
|
|
|
|
<DialogActions>
|
2023-01-19 18:12:16 +01:00
|
|
|
{ buttons }
|
2023-01-18 14:37:34 +01:00
|
|
|
</DialogActions>
|
|
|
|
</MuiDialog>
|
2023-01-14 09:51:46 +01:00
|
|
|
}
|