Files
SCM-Manager/scm-ui-components/packages/ui-components/src/forms/Select.js

70 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-08-02 16:09:58 +02:00
//@flow
import React from "react";
2018-08-28 14:29:45 +02:00
import classNames from "classnames";
import LabelWithHelpIcon from "./LabelWithHelpIcon";
2018-08-02 16:09:58 +02:00
export type SelectItem = {
value: string,
label: string
};
type Props = {
name?: string,
2018-08-02 16:09:58 +02:00
label?: string,
options: SelectItem[],
value?: string,
onChange: (value: string, name?: string) => void,
2018-10-05 10:21:11 +02:00
loading?: boolean,
2018-10-02 09:52:39 +02:00
helpText?: string
2018-08-02 16:09:58 +02:00
};
class Select extends React.Component<Props> {
field: ?HTMLSelectElement;
componentDidMount() {
// trigger change after render, if value is null to set it to the first value
// of the given options.
if (!this.props.value && this.field && this.field.value) {
this.props.onChange(this.field.value);
}
}
2018-08-02 16:09:58 +02:00
handleInput = (event: SyntheticInputEvent<HTMLSelectElement>) => {
this.props.onChange(event.target.value, this.props.name);
2018-08-02 16:09:58 +02:00
};
render() {
2018-10-05 10:21:11 +02:00
const { options, value, label, helpText, loading } = this.props;
2018-08-28 14:29:45 +02:00
const loadingClass = loading ? "is-loading" : "";
2018-08-02 16:09:58 +02:00
return (
<div className="field">
2018-10-04 10:16:20 +02:00
<LabelWithHelpIcon label={label} helpText={helpText} />
<div className={classNames(
"control select",
loadingClass
)}>
2018-08-02 16:09:58 +02:00
<select
ref={input => {
this.field = input;
}}
value={value}
onChange={this.handleInput}
>
{options.map(opt => {
return (
2019-01-25 12:48:04 +01:00
<option value={opt.value} key={"KEY_" + opt.value}>
2018-08-02 16:09:58 +02:00
{opt.label}
</option>
);
})}
</select>
</div>
</div>
);
}
}
export default Select;