2018-10-16 17:04:28 +02:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
|
|
import React from "react";
|
2018-12-17 11:29:20 +01:00
|
|
|
import type {Branch} from "@scm-manager/ui-types";
|
2019-01-29 15:17:51 +01:00
|
|
|
import TableHeader from "./TableHeader";
|
2018-10-17 11:33:40 +02:00
|
|
|
import injectSheet from "react-jss";
|
|
|
|
|
import classNames from "classnames";
|
2018-12-14 16:01:57 +01:00
|
|
|
import DropDown from "./forms/DropDown";
|
2018-10-17 11:33:40 +02:00
|
|
|
|
|
|
|
|
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"
|
2018-10-17 11:33:40 +02:00
|
|
|
}
|
|
|
|
|
};
|
2018-10-16 17:04:28 +02:00
|
|
|
|
|
|
|
|
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,
|
2018-10-17 11:33:40 +02:00
|
|
|
|
|
|
|
|
// context props
|
2018-12-14 16:01:57 +01:00
|
|
|
classes: Object
|
2018-10-16 17:04:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type State = { selectedBranch?: Branch };
|
2018-10-17 10:06:26 +02:00
|
|
|
|
2018-10-16 17:04:28 +02:00
|
|
|
class BranchSelector extends React.Component<Props, State> {
|
|
|
|
|
constructor(props: Props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {};
|
|
|
|
|
}
|
2018-10-17 10:06:26 +02:00
|
|
|
|
2018-10-23 11:24:23 +02:00
|
|
|
componentDidMount() {
|
2018-12-14 16:01:57 +01:00
|
|
|
const selectedBranch = this.props.branches.find(branch => branch.name === this.props.selectedBranch);
|
2018-12-17 11:29:20 +01:00
|
|
|
this.setState({ selectedBranch });
|
2018-10-23 11:24:23 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 17:04:28 +02:00
|
|
|
render() {
|
2018-12-14 16:01:57 +01:00
|
|
|
const { branches, classes, label } = this.props;
|
2018-10-17 10:06:26 +02:00
|
|
|
|
2018-10-16 17:04:28 +02:00
|
|
|
if (branches) {
|
|
|
|
|
return (
|
2019-01-29 15:17:51 +01:00
|
|
|
<TableHeader>
|
2018-10-17 11:33:40 +02:00
|
|
|
<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-10-17 11:33:40 +02:00
|
|
|
>
|
2018-12-14 16:01:57 +01:00
|
|
|
<label className="label">{label}</label>
|
2018-10-17 11:33:40 +02:00
|
|
|
</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>
|
2019-01-29 15:17:51 +01:00
|
|
|
</TableHeader>
|
2018-10-16 17:04:28 +02:00
|
|
|
);
|
2018-10-19 09:17:26 +02:00
|
|
|
} else {
|
|
|
|
|
return null;
|
2018-10-16 17:04:28 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2018-10-16 17:04:28 +02:00
|
|
|
const branch = branches.find(b => b.name === branchName);
|
|
|
|
|
|
2018-10-17 11:33:40 +02:00
|
|
|
selected(branch);
|
|
|
|
|
this.setState({ selectedBranch: branch });
|
2018-10-16 17:04:28 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-14 16:01:57 +01:00
|
|
|
export default injectSheet(styles)(BranchSelector);
|