mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-07 14:05:44 +01:00
85 lines
1.8 KiB
JavaScript
85 lines
1.8 KiB
JavaScript
//@flow
|
|
import React from "react";
|
|
import {Help} from "../index";
|
|
|
|
export type SelectItem = {
|
|
value: string,
|
|
label: string
|
|
};
|
|
|
|
type Props = {
|
|
label?: string,
|
|
options: SelectItem[],
|
|
value?: SelectItem,
|
|
onChange: string => void,
|
|
helpText?: string
|
|
};
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
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 "";
|
|
};
|
|
|
|
renderHelp = () => {
|
|
const helpText = this.props.helpText;
|
|
if(helpText){
|
|
return (
|
|
<div className="control columns is-vcentered">
|
|
<Help message={helpText} />
|
|
</div>);
|
|
}
|
|
else
|
|
return null;
|
|
};
|
|
|
|
render() {
|
|
const { options, value } = this.props;
|
|
|
|
return (
|
|
<div className="field">
|
|
{this.renderLabel()}
|
|
<div className="field is-grouped">
|
|
<div className="control select">
|
|
<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>
|
|
{this.renderHelp()}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
}
|
|
|
|
export default Select;
|