2018-10-16 17:04:28 +02:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
|
|
import React from "react";
|
2019-01-29 17:45:47 +01:00
|
|
|
import type { Branch } from "@scm-manager/ui-types";
|
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: {
|
2019-04-25 17:50:59 +02:00
|
|
|
flexBasis: "inherit",
|
2018-10-17 11:33:40 +02:00
|
|
|
flexGrow: 0
|
2018-12-12 09:26:21 +01:00
|
|
|
},
|
2019-04-25 17:50:59 +02:00
|
|
|
minWidthOfControl: {
|
|
|
|
|
minWidth: "10rem"
|
2019-01-29 17:45:47 +01:00
|
|
|
},
|
|
|
|
|
labelSizing: {
|
|
|
|
|
fontSize: "1rem !important"
|
|
|
|
|
},
|
|
|
|
|
noBottomMargin: {
|
|
|
|
|
marginBottom: "0 !important"
|
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,
|
2019-03-11 14:56:59 +01:00
|
|
|
disabled?: boolean,
|
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() {
|
2019-04-24 16:55:24 +02:00
|
|
|
const { branches } = this.props;
|
|
|
|
|
if (branches) {
|
|
|
|
|
const selectedBranch = branches.find(
|
|
|
|
|
branch => branch.name === this.props.selectedBranch
|
|
|
|
|
);
|
|
|
|
|
this.setState({ selectedBranch });
|
|
|
|
|
}
|
2018-10-23 11:24:23 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 17:04:28 +02:00
|
|
|
render() {
|
2019-03-11 14:56:59 +01:00
|
|
|
const { branches, classes, label, disabled } = this.props;
|
2018-10-17 10:06:26 +02:00
|
|
|
|
2018-10-16 17:04:28 +02:00
|
|
|
if (branches) {
|
|
|
|
|
return (
|
2019-01-29 17:45:47 +01:00
|
|
|
<div
|
2019-04-25 18:00:36 +02:00
|
|
|
className={classNames(
|
|
|
|
|
"field",
|
2019-04-25 18:07:18 +02:00
|
|
|
"is-horizontal"
|
2019-04-25 18:00:36 +02:00
|
|
|
)}
|
2019-01-29 17:45:47 +01:00
|
|
|
>
|
2018-10-17 11:33:40 +02:00
|
|
|
<div
|
2019-04-25 18:00:36 +02:00
|
|
|
className={classNames("field-label", "is-normal", classes.zeroflex)}
|
2018-10-17 11:33:40 +02:00
|
|
|
>
|
2019-01-29 17:45:47 +01:00
|
|
|
<label className={classNames("label", classes.labelSizing)}>
|
|
|
|
|
{label}
|
|
|
|
|
</label>
|
2018-10-17 11:33:40 +02:00
|
|
|
</div>
|
|
|
|
|
<div className="field-body">
|
2019-01-29 17:45:47 +01:00
|
|
|
<div
|
2019-04-25 18:00:36 +02:00
|
|
|
className={classNames(
|
|
|
|
|
"field",
|
|
|
|
|
"is-narrow",
|
|
|
|
|
classes.noBottomMargin
|
|
|
|
|
)}
|
2019-01-29 17:45:47 +01:00
|
|
|
>
|
2019-04-25 17:50:59 +02:00
|
|
|
<div className={classNames("control", classes.minWidthOfControl)}>
|
2018-10-17 11:33:40 +02:00
|
|
|
<DropDown
|
|
|
|
|
className="is-fullwidth"
|
|
|
|
|
options={branches.map(b => b.name)}
|
|
|
|
|
optionSelected={this.branchSelected}
|
2019-03-11 14:56:59 +01:00
|
|
|
disabled={!!disabled}
|
2018-10-17 11:33:40 +02:00
|
|
|
preselectedOption={
|
|
|
|
|
this.state.selectedBranch
|
|
|
|
|
? this.state.selectedBranch.name
|
|
|
|
|
: ""
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2019-01-29 17:45:47 +01:00
|
|
|
</div>
|
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);
|