2021-11-26 21:06:15 +01:00
|
|
|
import React, { useState } from "react";
|
2021-11-23 17:50:30 +01:00
|
|
|
import styled from "styled-components";
|
2021-06-30 00:56:01 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-11-23 17:50:30 +01:00
|
|
|
const List = styled.select`
|
|
|
|
border-radius: 0;
|
|
|
|
padding: 0.5rem;
|
|
|
|
`;
|
|
|
|
|
2021-07-11 15:58:42 +02:00
|
|
|
const Select = ({
|
|
|
|
items,
|
|
|
|
onChange,
|
|
|
|
current,
|
|
|
|
className,
|
|
|
|
testId,
|
2021-11-26 21:06:15 +01:00
|
|
|
}: ISelectProps) => {
|
|
|
|
const [selected, setSelected] = useState<string | undefined>(current);
|
|
|
|
|
|
|
|
const update = (
|
|
|
|
items: Array<IItemProps>,
|
|
|
|
onChange: (item: any) => void,
|
|
|
|
event: React.ChangeEvent<HTMLSelectElement>,
|
|
|
|
) => {
|
|
|
|
setSelected(event.target.value);
|
|
|
|
onChange(
|
|
|
|
items.find((item) => item.value.toString() === event.target.value),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<List
|
|
|
|
data-testid={"select" + (testId ? `-${testId}` : "")}
|
|
|
|
onChange={(e) => update(items, onChange, e)}
|
|
|
|
className={className}
|
|
|
|
value={selected}
|
|
|
|
>
|
2022-02-13 20:57:26 +01:00
|
|
|
{items.map(({ label, value }, index) => (
|
|
|
|
<option
|
|
|
|
data-testid={"option-" + (testId ? `${testId}-` : "") + index}
|
|
|
|
key={[label, index].join("")}
|
|
|
|
value={value.toString()}
|
|
|
|
>
|
|
|
|
{label}
|
|
|
|
</option>
|
|
|
|
))}
|
2021-11-26 21:06:15 +01:00
|
|
|
</List>
|
|
|
|
);
|
|
|
|
};
|
2021-06-30 00:56:01 +02:00
|
|
|
|
|
|
|
export default Select;
|