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

107 lines
3.7 KiB
TypeScript
Raw Normal View History

2022-08-27 15:58:36 +02:00
import { Modal, ModalOverlay, ModalContent, ModalFooter, ModalHeader, ModalBody, Button } from '@chakra-ui/react';
import React from 'react';
import { useNuiEvent } from '../../hooks/useNuiEvent';
import { useLocales } from '../../providers/LocaleProvider';
import { fetchNui } from '../../utils/fetchNui';
import { IInput, ICheckbox, ISelect, INumber, ISlider } from '../../interfaces/dialog';
import InputField from './components/fields/input';
import CheckboxField from './components/fields/checkbox';
import SelectField from './components/fields/select';
import NumberField from './components/fields/number';
import SliderField from './components/fields/slider';
export interface InputProps {
2022-03-19 12:19:01 +01:00
heading: string;
rows: Array<IInput | ICheckbox | ISelect | INumber | ISlider>;
2022-03-19 12:19:01 +01:00
}
const InputDialog: React.FC = () => {
const [fields, setFields] = React.useState<InputProps>({
2022-08-27 15:58:36 +02:00
heading: '',
rows: [{ type: 'input', label: '' }],
2022-03-19 12:19:01 +01:00
});
2022-08-27 15:58:36 +02: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],
});
};
2022-08-27 15:58:36 +02:00
useNuiEvent<InputProps>('openDialog', (data) => {
setPasswordStates([]);
2022-05-02 13:51:01 +02:00
setFields(data);
setInputData([]);
2022-03-19 12:19:01 +01:00
setVisible(true);
});
useNuiEvent('closeInputDialog', () => {
setVisible(false);
});
2022-03-19 12:19:01 +01:00
const handleClose = () => {
setVisible(false);
2022-08-27 15:58:36 +02:00
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);
2022-08-27 15:58:36 +02:00
fetchNui('inputData', inputData);
2022-03-19 12:19:01 +01:00
};
return (
<>
2022-08-27 15:58:36 +02:00
<Modal isOpen={visible} onClose={handleClose} isCentered closeOnEsc closeOnOverlayClick={false} size="xs">
2022-03-19 12:19:01 +01:00
<ModalOverlay />
<ModalContent
onKeyDown={(e) => {
2022-08-27 15:58:36 +02:00
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">
2022-08-27 15:58:36 +02:00
{fields.rows.map((row: IInput | ICheckbox | ISelect | INumber | ISlider, index) => (
<React.Fragment key={`row-${index}-${row.type}-${row.label}`}>
2022-08-27 15:58:36 +02:00
{row.type === 'input' && (
<InputField
row={row}
index={index}
handleChange={handleChange}
passwordStates={passwordStates}
handlePasswordStates={handlePasswordStates}
/>
)}
{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} />}
</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;