import { Group, Modal, Button, Stack } from '@mantine/core'; 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, 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'; import ColorField from './components/fields/color'; import DateField from './components/fields/date'; export interface InputProps { heading: string; rows: Array; options?: { allowCancel?: boolean; }; } export type FormValues = { test: { value: any; }[]; }; const InputDialog: React.FC = () => { const [fields, setFields] = React.useState({ heading: '', rows: [{ type: 'input', label: '' }], }); const [visible, setVisible] = React.useState(false); const { locale } = useLocales(); const form = useForm<{ test: { value: any }[] }>({}); const fieldForm = useFieldArray({ control: form.control, name: 'test', }); useNuiEvent('openDialog', (data) => { setFields(data); setVisible(true); data.rows.forEach((row, index) => { fieldForm.insert( index, { value: row.type !== 'checkbox' ? row.type === 'date' && row.default ? new Date(row.default) : 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; } }); }); useNuiEvent('closeInputDialog', () => { setVisible(false); }); const handleClose = async () => { setVisible(false); fetchNui('inputData'); await new Promise((resolve) => setTimeout(resolve, 200)); form.reset(); fieldForm.remove(); }; const onSubmit = form.handleSubmit(async (data) => { setVisible(false); const values: any[] = []; Object.values(data.test).forEach((obj: { value: any }) => values.push(obj.value)); console.log(values); fetchNui('inputData', values); await new Promise((resolve) => setTimeout(resolve, 200)); form.reset(); fieldForm.remove(); }); return ( <>
{fieldForm.fields.map((item, index) => { const row = fields.rows[index]; return ( {row.type === 'input' && ( )} {row.type === 'checkbox' && ( )} {row.type === 'select' && } {row.type === 'number' && } {row.type === 'slider' && } {row.type === 'color' && } {row.type === 'date' && } ); })}
); }; export default InputDialog;