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-02-15 13:00:09 +01:00
|
|
|
import { OptionValue } from '../../typings';
|
2022-10-27 14:22:15 +02:00
|
|
|
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';
|
2023-01-03 18:55:02 +01:00
|
|
|
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';
|
2023-01-15 11:44:14 +01:00
|
|
|
import TextareaField from './components/fields/textarea';
|
2023-02-14 10:54:53 +01:00
|
|
|
import TimeField from './components/fields/time';
|
2023-02-15 13:00:09 +01:00
|
|
|
import type { InputProps } from '../../typings';
|
2022-03-19 12:19:01 +01:00
|
|
|
|
2023-01-03 18:55:02 +01:00
|
|
|
export type FormValues = {
|
|
|
|
|
test: {
|
|
|
|
|
value: any;
|
|
|
|
|
}[];
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-19 12:19:01 +01:00
|
|
|
const InputDialog: React.FC = () => {
|
2022-08-27 15:40:41 +02:00
|
|
|
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);
|
2022-05-02 14:45:06 +02:00
|
|
|
const { locale } = useLocales();
|
2022-03-19 12:19:01 +01:00
|
|
|
|
2023-01-03 18:55:02 +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);
|
2023-01-03 18:55:02 +01:00
|
|
|
data.rows.forEach((row, index) => {
|
2023-01-13 19:09:07 +01:00
|
|
|
fieldForm.insert(
|
|
|
|
|
index,
|
|
|
|
|
{
|
|
|
|
|
value:
|
|
|
|
|
row.type !== 'checkbox'
|
2023-01-31 12:41:42 -05:00
|
|
|
? row.type === 'date' || row.type === 'date-range' || row.type === 'time'
|
2023-01-14 13:44:03 +01:00
|
|
|
? // Set date to current one if default is set to true
|
|
|
|
|
row.default === true
|
2023-01-14 19:02:25 +01:00
|
|
|
? new Date().getTime()
|
2023-01-14 14:15:38 +01:00
|
|
|
: Array.isArray(row.default)
|
2023-01-14 19:02:25 +01:00
|
|
|
? row.default.map((date) => new Date(date).getTime())
|
|
|
|
|
: row.default && new Date(row.default).getTime()
|
2023-01-13 19:09:07 +01:00
|
|
|
: row.default
|
|
|
|
|
: row.checked,
|
|
|
|
|
} || { value: null }
|
|
|
|
|
);
|
2023-01-08 11:50:44 +01:00
|
|
|
// Backwards compat with new Select data type
|
2023-01-15 11:54:15 +01:00
|
|
|
if (row.type === 'select' || row.type === 'multi-select') {
|
2023-01-08 11:50:44 +01:00
|
|
|
row.options = row.options.map((option) =>
|
|
|
|
|
!option.label ? { ...option, label: option.value } : option
|
|
|
|
|
) as Array<OptionValue>;
|
|
|
|
|
}
|
2023-01-03 18:55:02 +01:00
|
|
|
});
|
2022-03-19 12:19:01 +01:00
|
|
|
});
|
|
|
|
|
|
2022-09-29 11:39:30 +02:00
|
|
|
useNuiEvent('closeInputDialog', () => {
|
|
|
|
|
setVisible(false);
|
|
|
|
|
});
|
|
|
|
|
|
2023-01-04 13:52:39 +01:00
|
|
|
const handleClose = async () => {
|
2022-03-19 12:19:01 +01:00
|
|
|
setVisible(false);
|
2023-01-04 13:52:39 +01:00
|
|
|
await new Promise((resolve) => setTimeout(resolve, 200));
|
|
|
|
|
form.reset();
|
|
|
|
|
fieldForm.remove();
|
2023-02-26 12:31:08 +11:00
|
|
|
fetchNui('inputData');
|
2022-03-19 12:19:01 +01:00
|
|
|
};
|
|
|
|
|
|
2023-01-03 18:55:02 +01:00
|
|
|
const onSubmit = form.handleSubmit(async (data) => {
|
2023-01-04 13:28:40 +01:00
|
|
|
setVisible(false);
|
2023-01-03 18:55:02 +01:00
|
|
|
const values: any[] = [];
|
|
|
|
|
Object.values(data.test).forEach((obj: { value: any }) => values.push(obj.value));
|
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 200));
|
|
|
|
|
form.reset();
|
|
|
|
|
fieldForm.remove();
|
2023-02-26 12:31:08 +11:00
|
|
|
fetchNui('inputData', values);
|
2023-01-03 18:55:02 +01:00
|
|
|
});
|
|
|
|
|
|
2022-03-19 12:19:01 +01:00
|
|
|
return (
|
|
|
|
|
<>
|
2022-12-16 15:00:01 +01:00
|
|
|
<Modal
|
|
|
|
|
opened={visible}
|
|
|
|
|
onClose={handleClose}
|
|
|
|
|
centered
|
2023-01-11 20:54:29 +01:00
|
|
|
closeOnEscape={fields.options?.allowCancel !== false}
|
2022-12-16 15:00:01 +01:00
|
|
|
closeOnClickOutside={false}
|
|
|
|
|
size="xs"
|
|
|
|
|
styles={{ title: { textAlign: 'center', width: '100%', fontSize: 18 } }}
|
|
|
|
|
title={fields.heading}
|
|
|
|
|
withCloseButton={false}
|
|
|
|
|
overlayOpacity={0.5}
|
2022-12-31 15:05:01 +01:00
|
|
|
transition="fade"
|
2022-12-16 15:00:01 +01:00
|
|
|
exitTransitionDuration={150}
|
|
|
|
|
>
|
2023-01-03 18:55:02 +01:00
|
|
|
<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}
|
|
|
|
|
/>
|
2023-01-03 18:55:02 +01:00
|
|
|
)}
|
|
|
|
|
{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}
|
|
|
|
|
/>
|
2023-01-03 18:55:02 +01:00
|
|
|
)}
|
2023-01-15 11:54:15 +01:00
|
|
|
{(row.type === 'select' || row.type === 'multi-select') && (
|
|
|
|
|
<SelectField row={row} index={index} control={form.control} />
|
|
|
|
|
)}
|
2023-01-03 18:55:02 +01:00
|
|
|
{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-02-14 10:54:53 +01:00
|
|
|
{row.type === 'time' && <TimeField control={form.control} row={row} index={index} />}
|
|
|
|
|
{row.type === 'date' || row.type === 'date-range' ? (
|
2023-01-14 19:02:25 +01:00
|
|
|
<DateField control={form.control} row={row} index={index} />
|
|
|
|
|
) : null}
|
2023-01-15 11:44:14 +01:00
|
|
|
{row.type === 'textarea' && (
|
|
|
|
|
<TextareaField
|
|
|
|
|
register={form.register(`test.${index}.value`, { required: row.required })}
|
|
|
|
|
row={row}
|
|
|
|
|
index={index}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2023-01-03 18:55:02 +01:00
|
|
|
</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}
|
|
|
|
|
>
|
2023-01-03 18:55:02 +01:00
|
|
|
{locale.ui.cancel}
|
|
|
|
|
</Button>
|
2023-01-08 11:35:30 +01:00
|
|
|
<Button uppercase variant="light" type="submit">
|
2023-01-03 18:55:02 +01:00
|
|
|
{locale.ui.confirm}
|
|
|
|
|
</Button>
|
|
|
|
|
</Group>
|
|
|
|
|
</Stack>
|
|
|
|
|
</form>
|
2022-03-19 12:19:01 +01:00
|
|
|
</Modal>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default InputDialog;
|