add select type to table
This commit is contained in:
parent
71cf3d656e
commit
739ee87879
4 changed files with 104 additions and 12 deletions
47
src/components/Table/Inputs/Select.tsx
Normal file
47
src/components/Table/Inputs/Select.tsx
Normal 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>
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue