2019-10-20 16:59:02 +02:00
|
|
|
import React, { ChangeEvent } from "react";
|
|
|
|
|
import classNames from "classnames";
|
2019-10-19 16:38:07 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
options: string[];
|
|
|
|
|
optionValues?: string[];
|
|
|
|
|
optionSelected: (p: string) => void;
|
|
|
|
|
preselectedOption?: string;
|
2019-10-29 16:44:35 +01:00
|
|
|
className: string;
|
2019-10-19 16:38:07 +02:00
|
|
|
disabled?: boolean;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class DropDown extends React.Component<Props> {
|
|
|
|
|
render() {
|
2019-10-21 10:57:56 +02:00
|
|
|
const { options, optionValues, preselectedOption, className, disabled } = this.props;
|
2020-01-08 12:38:21 +01:00
|
|
|
|
|
|
|
|
if (preselectedOption && !options.includes(preselectedOption)) {
|
|
|
|
|
options.unshift(preselectedOption)
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
return (
|
2019-10-21 10:57:56 +02:00
|
|
|
<div className={classNames(className, "select", disabled ? "disabled" : "")}>
|
|
|
|
|
<select value={preselectedOption ? preselectedOption : ""} onChange={this.change} disabled={disabled}>
|
2020-01-08 12:38:21 +01:00
|
|
|
<option key={preselectedOption} />
|
2019-10-19 16:38:07 +02:00
|
|
|
{options.map((option, index) => {
|
|
|
|
|
return (
|
2019-10-21 10:57:56 +02:00
|
|
|
<option key={option} value={optionValues && optionValues[index] ? optionValues[index] : option}>
|
2019-10-19 16:38:07 +02:00
|
|
|
{option}
|
|
|
|
|
</option>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</select>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
change = (event: ChangeEvent<HTMLSelectElement>) => {
|
2019-10-19 16:38:07 +02:00
|
|
|
this.props.optionSelected(event.target.value);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default DropDown;
|