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

172 lines
5.4 KiB
TypeScript
Raw Normal View History

2023-01-13 19:09:07 +01:00
import { Group, Modal, Button, Stack } from '@mantine/core';
import React from 'react';
2022-08-27 15:58:36 +02:00
import { useNuiEvent } from '../../hooks/useNuiEvent';
import { useLocales } from '../../providers/LocaleProvider';
import { fetchNui } from '../../utils/fetchNui';
2023-01-13 19:09:07 +01:00
import {
IInput,
ICheckbox,
ISelect,
INumber,
ISlider,
IColorInput,
OptionValue,
IDateInput,
} 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';
import { useFieldArray, useForm } from 'react-hook-form';
2023-01-08 11:23:50 +01:00
import ColorField from './components/fields/color';
2023-01-13 19:09:07 +01:00
import DateField from './components/fields/date';
export interface InputProps {
2022-03-19 12:19:01 +01:00
heading: string;
2023-01-13 19:09:07 +01:00
rows: Array<IInput | ICheckbox | ISelect | INumber | ISlider | IColorInput | IDateInput>;
2023-01-11 20:54:29 +01:00
options?: {
allowCancel?: boolean;
};
2022-03-19 12:19:01 +01:00
}
export type FormValues = {
test: {
value: any;
}[];
};
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
});
const [visible, setVisible] = React.useState(false);
const { locale } = useLocales();
2022-03-19 12:19:01 +01:00
const form = useForm<{ test: { value: any }[] }>({});
const fieldForm = useFieldArray({
control: form.control,
name: 'test',
});
2022-08-27 15:58:36 +02:00
useNuiEvent<InputProps>('openDialog', (data) => {
2022-05-02 13:51:01 +02:00
setFields(data);
2022-03-19 12:19:01 +01:00
setVisible(true);
data.rows.forEach((row, index) => {
2023-01-13 19:09:07 +01:00
fieldForm.insert(
index,
{
value:
row.type !== 'checkbox'
2023-01-14 14:15:38 +01:00
? row.type === 'date' || row.type === 'date-range'
? // Set date to current one if default is set to true
row.default === true
? new Date()
2023-01-14 14:15:38 +01:00
: Array.isArray(row.default)
? row.default.map((date) => new Date(date))
: row.default && new Date(row.default)
2023-01-13 19:09:07 +01:00
: row.default
: row.checked,
} || { value: null }
);
// Backwards compat with new Select data type
if (row.type === 'select') {
row.options = row.options.map((option) =>
!option.label ? { ...option, label: option.value } : option
) as Array<OptionValue>;
}
});
2022-03-19 12:19:01 +01:00
});
useNuiEvent('closeInputDialog', () => {
setVisible(false);
});
const handleClose = async () => {
2022-03-19 12:19:01 +01:00
setVisible(false);
2022-08-27 15:58:36 +02:00
fetchNui('inputData');
await new Promise((resolve) => setTimeout(resolve, 200));
form.reset();
fieldForm.remove();
2022-03-19 12:19:01 +01:00
};
const onSubmit = form.handleSubmit(async (data) => {
setVisible(false);
const values: any[] = [];
Object.values(data.test).forEach((obj: { value: any }) => values.push(obj.value));
2023-01-08 11:35:30 +01:00
console.log(values);
fetchNui('inputData', values);
await new Promise((resolve) => setTimeout(resolve, 200));
form.reset();
fieldForm.remove();
});
2022-03-19 12:19:01 +01:00
return (
<>
<Modal
opened={visible}
onClose={handleClose}
centered
2023-01-11 20:54:29 +01:00
closeOnEscape={fields.options?.allowCancel !== false}
closeOnClickOutside={false}
size="xs"
styles={{ title: { textAlign: 'center', width: '100%', fontSize: 18 } }}
title={fields.heading}
withCloseButton={false}
overlayOpacity={0.5}
transition="fade"
exitTransitionDuration={150}
>
<form onSubmit={onSubmit}>
<Stack>
{fieldForm.fields.map((item, index) => {
const row = fields.rows[index];
return (
<React.Fragment key={item.id}>
{row.type === 'input' && (
2023-01-08 11:35:30 +01:00
<InputField
register={form.register(`test.${index}.value`, { required: row.required })}
row={row}
index={index}
/>
)}
{row.type === 'checkbox' && (
2023-01-08 11:35:30 +01:00
<CheckboxField
register={form.register(`test.${index}.value`, { required: row.required })}
row={row}
index={index}
/>
)}
{row.type === 'select' && <SelectField row={row} index={index} control={form.control} />}
{row.type === 'number' && <NumberField control={form.control} row={row} index={index} />}
{row.type === 'slider' && <SliderField control={form.control} row={row} index={index} />}
2023-01-08 11:23:50 +01:00
{row.type === 'color' && <ColorField control={form.control} row={row} index={index} />}
2023-01-14 14:15:38 +01:00
{row.type === 'date' ||
(row.type === 'date-range' && <DateField control={form.control} row={row} index={index} />)}
</React.Fragment>
);
})}
<Group position="right" spacing={10}>
2023-01-11 20:54:29 +01:00
<Button
uppercase
variant="default"
onClick={handleClose}
mr={3}
disabled={fields.options?.allowCancel === false}
>
{locale.ui.cancel}
</Button>
2023-01-08 11:35:30 +01:00
<Button uppercase variant="light" type="submit">
{locale.ui.confirm}
</Button>
</Group>
</Stack>
</form>
2022-03-19 12:19:01 +01:00
</Modal>
</>
);
};
export default InputDialog;