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

132 lines
4.5 KiB
TypeScript
Raw Normal View History

import { Modal, ModalOverlay, ModalContent, ModalFooter, ModalHeader, ModalBody, Button } from '@chakra-ui/react';
import React from 'react';
import { useNuiEvent } from '../../hooks/useNuiEvent';
import { useKeyPress } from '../../hooks/useKeyPress';
import { useLocales } from '../../providers/LocaleProvider';
import { debugData } from '../../utils/debugData';
import { fetchNui } from '../../utils/fetchNui';
import { IInput, ICheckbox, ISelect, INumber, ISlider } from '../../interfaces/dialog';
import InputField from './components/input';
import CheckboxField from './components/checkbox';
import SelectField from './components/select';
import NumberField from './components/number';
import SliderField from './components/slider';
2022-03-19 12:19:01 +01:00
interface Props {
heading: string;
rows: Array<IInput | ICheckbox | ISelect | INumber | ISlider>;
2022-03-19 12:19:01 +01:00
}
2022-06-11 11:33:15 +02:00
// debugData<Props>([
// {
// action: "openDialog",
// data: {
// heading: "Police locker",
// rows: [
// { type: "input", label: "Locker number", placeholder: "420" },
2022-06-11 11:33:15 +02:00
// { type: "checkbox", label: "Some checkbox" },
// { type: "input", label: "Locker PIN", password: true, icon: "lock" },
// { type: "checkbox", label: "Some other checkbox", checked: true },
2022-06-11 11:33:15 +02:00
// {
// type: "select",
// label: "Locker type",
// options: [
// { value: "option1" },
2022-06-11 11:33:15 +02:00
// { value: "option2", label: "Option 2" },
// { value: "option3", label: "Option 3" },
// ],
// },
// { type: "number", label: "Number counter", default: 12 },
// { type: "slider", label: "Slide bar", min: 10, max: 50, step: 2 },
2022-06-11 11:33:15 +02:00
// ],
// },
// },
// ]);
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>({
heading: '',
rows: [{ type: 'input', label: '' }],
2022-03-19 12:19:01 +01:00
});
const [inputData, setInputData] = React.useState<Array<string | number | boolean>>([]);
const [passwordStates, setPasswordStates] = React.useState<boolean[]>([]);
2022-03-19 12:19:01 +01:00
const [visible, setVisible] = React.useState(false);
const { locale } = useLocales();
2022-03-19 12:19:01 +01:00
const handlePasswordStates = (index: number) => {
setPasswordStates({
...passwordStates,
[index]: !passwordStates[index],
});
};
useNuiEvent<Props>('openDialog', (data) => {
setPasswordStates([]);
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');
2022-03-19 12:19:01 +01:00
};
2022-06-19 14:57:03 +02:00
const handleChange = (value: string | number | boolean, index: number) => {
2022-03-19 12:19:01 +01:00
setInputData((previousData) => {
previousData[index] = value;
2022-03-19 12:19:01 +01:00
return previousData;
});
};
const handleConfirm = () => {
setVisible(false);
fetchNui('inputData', inputData);
2022-03-19 12:19:01 +01:00
};
return (
<>
<Modal isOpen={visible} onClose={handleClose} isCentered closeOnEsc closeOnOverlayClick={false} size="xs">
2022-03-19 12:19:01 +01:00
<ModalOverlay />
<ModalContent
onKeyDown={(e) => {
if (e.key === 'Enter' && visible) return handleConfirm();
}}
>
2022-05-02 13:51:01 +02:00
<ModalHeader textAlign="center">{fields.heading}</ModalHeader>
<ModalBody fontFamily="Poppins" textAlign="left">
{fields.rows.map((row: IInput | ICheckbox | ISelect | INumber | ISlider, index) => (
2022-05-02 13:51:01 +02:00
<React.Fragment key={`row-${index}`}>
{row.type === 'input' && (
<InputField
key={`input-${index}`}
row={row}
index={index}
handleChange={handleChange}
passwordStates={passwordStates}
handlePasswordStates={handlePasswordStates}
/>
2022-05-02 13:51:01 +02:00
)}
{row.type === 'checkbox' && <CheckboxField row={row} index={index} handleChange={handleChange} />}
{row.type === 'select' && <SelectField row={row} index={index} handleChange={handleChange} />}
{row.type === 'number' && <NumberField row={row} index={index} handleChange={handleChange} />}
{row.type === 'slider' && <SliderField row={row} index={index} handleChange={handleChange} />}
2022-05-02 13:51:01 +02:00
</React.Fragment>
2022-03-19 12:19:01 +01:00
))}
</ModalBody>
<ModalFooter>
<Button mr={3} onClick={handleClose}>
{locale.ui.close}
2022-03-19 12:19:01 +01:00
</Button>
<Button colorScheme="blue" onClick={handleConfirm}>
{locale.ui.confirm}
2022-03-19 12:19:01 +01:00
</Button>
</ModalFooter>
</ModalContent>
</Modal>
</>
);
};
export default InputDialog;