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

74 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";
2018-08-02 16:09:58 +02:00
export type SelectItem = {
value: string,
label: string
};
type Props = {
label?: string,
options: SelectItem[],
value?: SelectItem,
2018-08-28 14:29:45 +02:00
onChange: string => void,
loading?: boolean
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);
};
renderLabel = () => {
const label = this.props.label;
if (label) {
return <label className="label">{label}</label>;
}
return "";
};
render() {
2018-08-28 14:29:45 +02:00
const { options, value, loading } = this.props;
const loadingClass = loading ? "is-loading" : "";
2018-08-02 16:09:58 +02:00
return (
<div className="field">
{this.renderLabel()}
2018-08-28 14:29:45 +02:00
<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 (
<option value={opt.value} key={opt.value}>
{opt.label}
</option>
);
})}
</select>
</div>
</div>
);
}
}
export default Select;