feat(web/input): required fields

This commit is contained in:
Luke
2023-01-08 11:35:30 +01:00
parent 6c4485b570
commit d5a9a3fb90
7 changed files with 32 additions and 4 deletions

View File

@@ -61,6 +61,7 @@ const InputDialog: React.FC = () => {
setVisible(false); setVisible(false);
const values: any[] = []; const values: any[] = [];
Object.values(data.test).forEach((obj: { value: any }) => values.push(obj.value)); Object.values(data.test).forEach((obj: { value: any }) => values.push(obj.value));
console.log(values);
fetchNui('inputData', values); fetchNui('inputData', values);
await new Promise((resolve) => setTimeout(resolve, 200)); await new Promise((resolve) => setTimeout(resolve, 200));
form.reset(); form.reset();
@@ -89,10 +90,18 @@ const InputDialog: React.FC = () => {
return ( return (
<React.Fragment key={item.id}> <React.Fragment key={item.id}>
{row.type === 'input' && ( {row.type === 'input' && (
<InputField register={form.register(`test.${index}.value`)} row={row} index={index} /> <InputField
register={form.register(`test.${index}.value`, { required: row.required })}
row={row}
index={index}
/>
)} )}
{row.type === 'checkbox' && ( {row.type === 'checkbox' && (
<CheckboxField register={form.register(`test.${index}.value`)} row={row} index={index} /> <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 === 'select' && <SelectField row={row} index={index} control={form.control} />}
{row.type === 'number' && <NumberField control={form.control} row={row} index={index} />} {row.type === 'number' && <NumberField control={form.control} row={row} index={index} />}
@@ -105,7 +114,7 @@ const InputDialog: React.FC = () => {
<Button uppercase variant="default" onClick={handleClose} mr={3}> <Button uppercase variant="default" onClick={handleClose} mr={3}>
{locale.ui.cancel} {locale.ui.cancel}
</Button> </Button>
<Button uppercase variant="light" onClick={handleClose} type="submit"> <Button uppercase variant="light" type="submit">
{locale.ui.confirm} {locale.ui.confirm}
</Button> </Button>
</Group> </Group>

View File

@@ -11,7 +11,13 @@ interface Props {
const CheckboxField: React.FC<Props> = (props) => { const CheckboxField: React.FC<Props> = (props) => {
return ( return (
<Checkbox {...props.register} sx={{ display: 'flex' }} label={props.row.label} defaultChecked={props.row.checked} /> <Checkbox
{...props.register}
sx={{ display: 'flex' }}
required={props.row.required}
label={props.row.label}
defaultChecked={props.row.checked}
/>
); );
}; };

View File

@@ -14,6 +14,7 @@ const ColorField: React.FC<Props> = (props) => {
name: `test.${props.index}.value`, name: `test.${props.index}.value`,
control: props.control, control: props.control,
defaultValue: props.row.default, defaultValue: props.row.default,
rules: { required: props.row.required },
}); });
return ( return (
@@ -29,6 +30,7 @@ const ColorField: React.FC<Props> = (props) => {
disabled={props.row.disabled} disabled={props.row.disabled}
defaultValue={props.row.default} defaultValue={props.row.default}
format={props.row.format} format={props.row.format}
withAsterisk={props.row.required}
/> />
); );
}; };

View File

@@ -30,6 +30,7 @@ const InputField: React.FC<Props> = (props) => {
icon={props.row.icon && <FontAwesomeIcon icon={props.row.icon} fixedWidth />} icon={props.row.icon && <FontAwesomeIcon icon={props.row.icon} fixedWidth />}
placeholder={props.row.placeholder} placeholder={props.row.placeholder}
disabled={props.row.disabled} disabled={props.row.disabled}
withAsterisk={props.row.required}
/> />
) : ( ) : (
<PasswordInput <PasswordInput
@@ -40,6 +41,7 @@ const InputField: React.FC<Props> = (props) => {
icon={props.row.icon && <FontAwesomeIcon icon={props.row.icon} fixedWidth />} icon={props.row.icon && <FontAwesomeIcon icon={props.row.icon} fixedWidth />}
placeholder={props.row.placeholder} placeholder={props.row.placeholder}
disabled={props.row.disabled} disabled={props.row.disabled}
withAsterisk={props.row.required}
visibilityToggleIcon={({ reveal, size }) => ( visibilityToggleIcon={({ reveal, size }) => (
<FontAwesomeIcon <FontAwesomeIcon
icon={reveal ? 'eye-slash' : 'eye'} icon={reveal ? 'eye-slash' : 'eye'}

View File

@@ -16,6 +16,7 @@ const NumberField: React.FC<Props> = (props) => {
name: `test.${props.index}.value`, name: `test.${props.index}.value`,
control: props.control, control: props.control,
defaultValue: props.row.default, defaultValue: props.row.default,
rules: { required: props.row.required },
}); });
return ( return (
@@ -32,6 +33,7 @@ const NumberField: React.FC<Props> = (props) => {
max={props.row.max} max={props.row.max}
disabled={props.row.disabled} disabled={props.row.disabled}
icon={props.row.icon && <FontAwesomeIcon icon={props.row.icon} fixedWidth />} icon={props.row.icon && <FontAwesomeIcon icon={props.row.icon} fixedWidth />}
withAsterisk={props.row.required}
/> />
); );
}; };

View File

@@ -15,6 +15,7 @@ const SelectField: React.FC<Props> = (props) => {
name: `test.${props.index}.value`, name: `test.${props.index}.value`,
control: props.control, control: props.control,
defaultValue: props.row.default, defaultValue: props.row.default,
rules: { required: props.row.required },
}); });
return ( return (
@@ -28,6 +29,7 @@ const SelectField: React.FC<Props> = (props) => {
disabled={props.row.disabled} disabled={props.row.disabled}
label={props.row.label} label={props.row.label}
description={props.row.description} description={props.row.description}
withAsterisk={props.row.required}
/> />
); );
}; };

View File

@@ -10,6 +10,7 @@ export interface IInput {
icon?: IconProp; icon?: IconProp;
disabled?: boolean; disabled?: boolean;
description?: string; description?: string;
required?: boolean;
} }
export interface ICheckbox { export interface ICheckbox {
@@ -18,6 +19,7 @@ export interface ICheckbox {
checked?: boolean; checked?: boolean;
disabled?: boolean; disabled?: boolean;
description?: string; description?: string;
required?: boolean;
} }
export interface ISelect { export interface ISelect {
@@ -27,6 +29,7 @@ export interface ISelect {
options: Array<SelectItem>; options: Array<SelectItem>;
disabled?: boolean; disabled?: boolean;
description?: string; description?: string;
required?: boolean;
} }
export interface INumber { export interface INumber {
@@ -39,6 +42,7 @@ export interface INumber {
max?: number; max?: number;
disabled?: boolean; disabled?: boolean;
description?: string; description?: string;
required?: boolean;
} }
export interface ISlider { export interface ISlider {
@@ -59,4 +63,5 @@ export interface IColorInput {
disabled?: boolean; disabled?: boolean;
description?: string; description?: string;
format?: 'hex' | 'hexa' | 'rgb' | 'rgba' | 'hsl' | 'hsla'; format?: 'hex' | 'hexa' | 'rgb' | 'rgba' | 'hsl' | 'hsla';
required?: boolean;
} }