You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.5 KiB

import React, { FC, useEffect, useState } from 'react'
import { FieldValues, Controller, Control } from 'react-hook-form';
import { useFetchWithAuth } from '../hooks/useFetchWithAuth';
interface Props {
entity: string,
control: Control<any, any>
}
export const Manytoone: FC<Props> = ({entity, control }) => {
const [data, setData] = useState([{id:0,nombre:'Select one'}]);
useEffect(() => {
async function fetchData() {
try {
const { data, error } = await useFetchWithAuth(entity.toLowerCase());
setData(data);
} catch (trace) {
console.error(trace);
}
}
fetchData();
}, [entity]);
return (
<>
<label htmlFor={entity}>{entity}</label>
<Controller key={entity}
render={({ field }) => (
<select key={entity+"select"} {...field} className={' p-2 border-2 border-sky-300 outline-sky-300 bg-white rounded-xl w-full'}>
<option key={entity+0} value={0}>
Seleccione un valor
</option>
{data.map((item) => (
<option key={entity+item.id} value={item.id}>
{item['nombre']}
</option>
))}
</select>
)}
name={entity}
control={control}
/>
</>
)
}