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