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

96 lines
2.3 KiB
JavaScript
Raw Normal View History

// @flow
import React from "react";
2018-12-14 16:01:57 +01:00
import type {Branch} from "packages/ui-types/src/index";
import injectSheet from "react-jss";
import classNames from "classnames";
2018-12-14 16:01:57 +01:00
import DropDown from "./forms/DropDown";
const styles = {
zeroflex: {
flexGrow: 0
2018-12-12 09:26:21 +01:00
},
2018-12-12 10:58:22 +01:00
minWidthOfLabel: {
2018-12-12 09:26:21 +01:00
minWidth: "4.5rem"
}
};
type Props = {
branches: Branch[], // TODO: Use generics?
2018-10-17 14:11:28 +02:00
selected: (branch?: Branch) => void,
2018-12-14 16:01:57 +01:00
selectedBranch?: string,
label: string,
// context props
2018-12-14 16:01:57 +01:00
classes: Object
};
type State = { selectedBranch?: Branch };
class BranchSelector extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {};
}
componentDidMount() {
2018-12-14 16:01:57 +01:00
const selectedBranch = this.props.branches.find(branch => branch.name === this.props.selectedBranch);
this.setState({ selectedBranch })
}
render() {
2018-12-14 16:01:57 +01:00
const { branches, classes, label } = this.props;
if (branches) {
return (
<div className="box field is-horizontal">
<div
2018-12-12 09:26:21 +01:00
className={classNames(
"field-label",
"is-normal",
classes.zeroflex,
2018-12-12 10:58:22 +01:00
classes.minWidthOfLabel
2018-12-12 09:26:21 +01:00
)}
>
2018-12-14 16:01:57 +01:00
<label className="label">{label}</label>
</div>
<div className="field-body">
<div className="field is-narrow">
<div className="control">
<DropDown
className="is-fullwidth"
options={branches.map(b => b.name)}
optionSelected={this.branchSelected}
preselectedOption={
this.state.selectedBranch
? this.state.selectedBranch.name
: ""
}
/>
</div>
</div>
</div>
</div>
);
2018-10-19 09:17:26 +02:00
} else {
return null;
}
}
branchSelected = (branchName: string) => {
const { branches, selected } = this.props;
2018-12-14 20:20:00 +01:00
if (!branchName) {
this.setState({ selectedBranch: undefined });
selected(undefined);
return;
}
const branch = branches.find(b => b.name === branchName);
selected(branch);
this.setState({ selectedBranch: branch });
};
}
2018-12-14 16:01:57 +01:00
export default injectSheet(styles)(BranchSelector);