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-10 23:57:08 +02:00
|
|
|
current: string;
|
2021-06-30 00:56:01 +02:00
|
|
|
className?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-10 23:57:08 +02:00
|
|
|
const Select = ({ items, onChange, current, className }: ISelectProps) => (
|
2021-06-30 00:56:01 +02:00
|
|
|
<select
|
|
|
|
data-testid="select"
|
|
|
|
onChange={(e) => update(items, onChange, e)}
|
|
|
|
className={className}
|
|
|
|
>
|
|
|
|
{items.map(({ label, value }, index) => (
|
|
|
|
<option
|
|
|
|
data-testid={"option-" + index}
|
|
|
|
key={[label, index].join("")}
|
|
|
|
value={value.toString()}
|
2021-07-10 23:57:08 +02:00
|
|
|
selected={current === label}
|
2021-06-30 00:56:01 +02:00
|
|
|
>
|
|
|
|
{label}
|
|
|
|
</option>
|
|
|
|
))}
|
|
|
|
</select>
|
|
|
|
);
|
|
|
|
|
|
|
|
export default Select;
|