add select type to table

This commit is contained in:
Alexander Navarro 2023-09-24 11:58:29 -03:00
parent 71cf3d656e
commit 739ee87879
4 changed files with 104 additions and 12 deletions

View file

@ -0,0 +1,47 @@
import React, { useMemo, type ChangeEventHandler } from 'react';
export type DataItem = Record<string, any>;
interface Props {
onChange: (value: string | null) => void;
data: DataItem[];
keyData: string;
}
export default function Select({
data,
keyData,
onChange,
}: Props): JSX.Element {
const options = useMemo(() => {
let options = data.map((item) => item[keyData]);
options = [...new Set(options)];
options = options.map((item, idx) => (
<option key={idx} value={item}>
{item}
</option>
));
options.unshift(
<option key={-1} value="">
Select...
</option>,
);
return options;
}, [data, keyData]);
return (
<select
name="fooe"
id="foo"
onChange={({ target }) =>
{ onChange(target.value !== '' ? target.value : null); }
}
>
{options}
</select>
);
}