Files
ox_lib/web/src/features/dialog/components/select.tsx

48 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-08-27 15:58:36 +02:00
import { Box, Select } from '@chakra-ui/react';
import { useEffect } from 'react';
import { ISelect } from '../../../interfaces/dialog';
interface Props {
row: ISelect;
index: number;
handleChange: (value: string, index: number) => void;
}
const SelectField: React.FC<Props> = (props) => {
useEffect(() => {
2022-08-27 15:58:36 +02:00
if (props.row.default) {
props.row.options?.map((option) => {
2022-08-27 15:58:36 +02:00
if (props.row.default === option.value) {
props.handleChange(option.value, props.index);
}
});
}
}, []);
return (
<>
<Box mb={3} key={`select-${props.index}`}>
<Select
onChange={(e: React.ChangeEvent<HTMLSelectElement>) => props.handleChange(e.target.value, props.index)}
2022-08-27 15:58:36 +02:00
defaultValue={props.row.default || ''}
isDisabled={props.row.disabled}
>
{/* Hacky workaround for selectable placeholder issue */}
2022-08-27 15:58:36 +02:00
{!props.row.default && (
<option value="" hidden disabled>
{props.row.label}
</option>
)}
{props.row.options?.map((option, index) => (
<option key={`option-${index}`} value={option.value}>
{option.label || option.value}
</option>
))}
</Select>
</Box>
</>
);
};
export default SelectField;