Files
ox_lib/web/src/features/dialog/InputDialog.tsx

127 lines
3.1 KiB
TypeScript
Raw Normal View History

2022-03-19 12:19:01 +01:00
import {
Modal,
ModalOverlay,
ModalContent,
ModalFooter,
ModalHeader,
ModalBody,
Box,
Input,
Text,
Button,
2022-05-02 13:51:01 +02:00
Checkbox,
2022-03-19 12:19:01 +01:00
} from "@chakra-ui/react";
import React from "react";
import { useNuiEvent } from "../../hooks/useNuiEvent";
2022-05-02 13:51:01 +02:00
import { debugData } from "../../utils/debugData";
import { fetchNui } from "../../utils/fetchNui";
2022-03-19 12:19:01 +01:00
interface Props {
heading: string;
2022-05-02 13:51:01 +02:00
rows: {
type: "input" | "checkbox";
label: string;
}[];
2022-03-19 12:19:01 +01:00
}
2022-05-02 13:51:01 +02:00
debugData<Props>([
{
action: "openDialog",
data: {
heading: "Police locker",
rows: [
{ type: "input", label: "Locker number" },
{ type: "checkbox", label: "Some checkbox" },
{ type: "input", label: "Locker PIN" },
{ type: "checkbox", label: "Some other checkbox" },
],
},
},
]);
2022-03-19 12:19:01 +01:00
const InputDialog: React.FC = () => {
2022-05-02 13:51:01 +02:00
const [fields, setFields] = React.useState<Props>({
2022-03-19 12:19:01 +01:00
heading: "",
2022-05-02 13:51:01 +02:00
rows: [{ type: "input", label: "" }],
2022-03-19 12:19:01 +01:00
});
2022-05-02 13:51:01 +02:00
const [inputData, setInputData] = React.useState<Array<string | boolean>>([]);
2022-03-19 12:19:01 +01:00
const [visible, setVisible] = React.useState(false);
useNuiEvent<Props>("openDialog", (data) => {
2022-05-02 13:51:01 +02:00
setFields(data);
setInputData([]);
2022-03-19 12:19:01 +01:00
setVisible(true);
});
const handleClose = () => {
setVisible(false);
fetchNui("inputData");
};
const handleChange = (
e: React.ChangeEvent<HTMLInputElement>,
2022-05-02 13:51:01 +02:00
index: number,
type: "input" | "checkbox"
2022-03-19 12:19:01 +01:00
) => {
setInputData((previousData) => {
2022-05-02 13:51:01 +02:00
previousData[index] =
type === "input" ? e.target.value : e.target.checked;
2022-03-19 12:19:01 +01:00
return previousData;
});
};
const handleConfirm = () => {
setVisible(false);
fetchNui("inputData", inputData);
};
return (
<>
<Modal
isOpen={visible}
onClose={handleClose}
isCentered
closeOnEsc
closeOnOverlayClick={false}
size="xs"
>
<ModalOverlay />
<ModalContent>
2022-05-02 13:51:01 +02:00
<ModalHeader textAlign="center">{fields.heading}</ModalHeader>
<ModalBody fontFamily="Poppins" textAlign="left">
{fields.rows.map((row, index) => (
<React.Fragment key={`row-${index}`}>
{row.type === "input" && (
<Box mb={3} key={`input-${index}`} textAlign="left">
<Text>{row.label}</Text>
<Input onChange={(e) => handleChange(e, index, "input")} />
</Box>
)}
{row.type === "checkbox" && (
<Box mb={3} key={`checkbox-${index}`}>
<Checkbox
onChange={(e) => handleChange(e, index, "checkbox")}
>
{row.label}
</Checkbox>
</Box>
)}
</React.Fragment>
2022-03-19 12:19:01 +01:00
))}
</ModalBody>
<ModalFooter>
<Button mr={3} onClick={handleClose}>
Close
</Button>
<Button colorScheme="blue" onClick={handleConfirm}>
Confirm
</Button>
</ModalFooter>
</ModalContent>
</Modal>
</>
);
};
export default InputDialog;