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

114 lines
3.7 KiB
TypeScript
Raw Normal View History

import { Group, Modal, Button, Stack } from '@mantine/core';
2022-08-27 15:58:36 +02:00
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 (
<>
<Modal
opened={visible}
onClose={handleClose}
centered
closeOnClickOutside={false}
size="xs"
onKeyDown={(e) => {
if (e.key === 'Enter' && visible) return handleConfirm();
}}
styles={{ title: { textAlign: 'center', width: '100%', fontSize: 18 } }}
title={fields.heading}
withCloseButton={false}
overlayOpacity={0.5}
transition="fade"
exitTransitionDuration={150}
>
<Stack>
{fields.rows.map((row: IInput | ICheckbox | ISelect | INumber | ISlider, index) => (
<React.Fragment key={`row-${index}-${row.type}-${row.label}`}>
{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>
))}
<Group position="right" spacing={10}>
<Button uppercase variant="default" onClick={handleClose} mr={3}>
{locale.ui.cancel}
2022-03-19 12:19:01 +01:00
</Button>
<Button uppercase variant="light" onClick={handleClose}>
{locale.ui.confirm}
2022-03-19 12:19:01 +01:00
</Button>
</Group>
</Stack>
2022-03-19 12:19:01 +01:00
</Modal>
</>
);
};
export default InputDialog;