2023-01-15 11:54:15 +01:00
|
|
|
import { MultiSelect, Select } from '@mantine/core';
|
2022-10-27 14:22:15 +02:00
|
|
|
import { ISelect } from '../../../../interfaces/dialog';
|
2023-01-03 18:55:02 +01:00
|
|
|
import { Control, FieldValues, useController, UseFormRegisterReturn, UseFormSetValue } from 'react-hook-form';
|
|
|
|
|
import { FormValues } from '../../InputDialog';
|
2023-01-09 14:39:49 +01:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
2022-05-03 15:08:28 +02:00
|
|
|
|
|
|
|
|
interface Props {
|
2022-08-09 23:50:14 +02:00
|
|
|
row: ISelect;
|
2022-05-03 15:08:28 +02:00
|
|
|
index: number;
|
2023-01-03 18:55:02 +01:00
|
|
|
control: Control<FormValues>;
|
2022-05-03 15:08:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const SelectField: React.FC<Props> = (props) => {
|
2023-01-03 18:55:02 +01:00
|
|
|
const controller = useController({
|
|
|
|
|
name: `test.${props.index}.value`,
|
|
|
|
|
control: props.control,
|
2023-01-15 11:54:15 +01:00
|
|
|
defaultValue: props.row.default || props.row.type !== 'multi-select' ? props.row.options[0].value : undefined,
|
2023-01-08 11:35:30 +01:00
|
|
|
rules: { required: props.row.required },
|
2023-01-03 18:55:02 +01:00
|
|
|
});
|
2022-08-05 15:10:37 +02:00
|
|
|
|
2022-05-03 15:08:28 +02:00
|
|
|
return (
|
2023-01-15 11:54:15 +01:00
|
|
|
<>
|
|
|
|
|
{props.row.type === 'select' ? (
|
|
|
|
|
<Select
|
|
|
|
|
data={props.row.options}
|
|
|
|
|
value={controller.field.value}
|
|
|
|
|
name={controller.field.name}
|
|
|
|
|
ref={controller.field.ref}
|
|
|
|
|
onBlur={controller.field.onBlur}
|
|
|
|
|
onChange={controller.field.onChange}
|
|
|
|
|
disabled={props.row.disabled}
|
|
|
|
|
label={props.row.label}
|
|
|
|
|
description={props.row.description}
|
|
|
|
|
withAsterisk={props.row.required}
|
|
|
|
|
clearable={props.row.clearable}
|
|
|
|
|
icon={props.row.icon && <FontAwesomeIcon icon={props.row.icon} fixedWidth />}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
{props.row.type === 'multi-select' && (
|
|
|
|
|
<MultiSelect
|
|
|
|
|
data={props.row.options}
|
|
|
|
|
value={controller.field.value}
|
|
|
|
|
name={controller.field.name}
|
|
|
|
|
ref={controller.field.ref}
|
|
|
|
|
onBlur={controller.field.onBlur}
|
|
|
|
|
onChange={controller.field.onChange}
|
|
|
|
|
disabled={props.row.disabled}
|
|
|
|
|
label={props.row.label}
|
|
|
|
|
description={props.row.description}
|
|
|
|
|
withAsterisk={props.row.required}
|
|
|
|
|
clearable={props.row.clearable}
|
|
|
|
|
icon={props.row.icon && <FontAwesomeIcon icon={props.row.icon} fixedWidth />}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
2022-05-03 15:08:28 +02:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default SelectField;
|