2021-06-30 00:56:01 +02:00
|
|
|
import React from "react";
|
|
|
|
|
|
|
|
export interface IItemProps {
|
|
|
|
label: string;
|
|
|
|
value: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ISelectProps {
|
|
|
|
items: Array<IItemProps>;
|
|
|
|
onChange: (item: any) => void;
|
2021-07-11 15:58:42 +02:00
|
|
|
current?: string;
|
2021-06-30 00:56:01 +02:00
|
|
|
className?: string;
|
2021-07-11 15:58:42 +02:00
|
|
|
testId?: string;
|
2021-06-30 00:56:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const update = (
|
|
|
|
items: Array<IItemProps>,
|
|
|
|
onChange: (item: any) => void,
|
|
|
|
e: React.ChangeEvent<HTMLSelectElement>,
|
|
|
|
) => {
|
|
|
|
onChange(items.find((item) => item.value.toString() === e.target.value));
|
|
|
|
};
|
|
|
|
|
2021-07-11 15:58:42 +02:00
|
|
|
const Select = ({
|
|
|
|
items,
|
|
|
|
onChange,
|
|
|
|
current,
|
|
|
|
className,
|
|
|
|
testId,
|
|
|
|
}: ISelectProps) => (
|
2021-06-30 00:56:01 +02:00
|
|
|
<select
|
2021-07-11 15:58:42 +02:00
|
|
|
data-testid={"select" + (testId ? "-" + testId : "")}
|
2021-06-30 00:56:01 +02:00
|
|
|
onChange={(e) => update(items, onChange, e)}
|
|
|
|
className={className}
|
2021-07-11 15:58:42 +02:00
|
|
|
defaultValue={current}
|
2021-06-30 00:56:01 +02:00
|
|
|
>
|
|
|
|
{items.map(({ label, value }, index) => (
|
|
|
|
<option
|
2021-07-11 15:58:42 +02:00
|
|
|
data-testid={"option-" + (testId ? testId + "-" : "") + index}
|
2021-06-30 00:56:01 +02:00
|
|
|
key={[label, index].join("")}
|
|
|
|
value={value.toString()}
|
|
|
|
>
|
|
|
|
{label}
|
|
|
|
</option>
|
|
|
|
))}
|
|
|
|
</select>
|
|
|
|
);
|
|
|
|
|
|
|
|
export default Select;
|