2019-10-20 16:59:02 +02:00
|
|
|
import React from "react";
|
|
|
|
|
import classNames from "classnames";
|
|
|
|
|
import styled from "styled-components";
|
|
|
|
|
import { Branch } from "@scm-manager/ui-types";
|
|
|
|
|
import DropDown from "./forms/DropDown";
|
2018-10-17 11:33:40 +02:00
|
|
|
|
2018-10-16 17:04:28 +02:00
|
|
|
type Props = {
|
2019-10-19 16:38:07 +02:00
|
|
|
branches: Branch[];
|
|
|
|
|
selected: (branch?: Branch) => void;
|
|
|
|
|
selectedBranch?: string;
|
|
|
|
|
label: string;
|
|
|
|
|
disabled?: boolean;
|
2018-10-16 17:04:28 +02:00
|
|
|
};
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
type State = {
|
|
|
|
|
selectedBranch?: Branch;
|
|
|
|
|
};
|
2018-10-17 10:06:26 +02:00
|
|
|
|
2019-10-09 16:54:23 +02:00
|
|
|
const ZeroflexFieldLabel = styled.div`
|
2019-10-08 16:42:08 +02:00
|
|
|
flex-basis: inherit;
|
|
|
|
|
flex-grow: 0;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const MinWidthControl = styled.div`
|
|
|
|
|
min-width: 10rem;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const NoBottomMarginField = styled.div`
|
|
|
|
|
margin-bottom: 0 !important;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
export default class BranchSelector extends React.Component<Props, State> {
|
2018-10-16 17:04:28 +02:00
|
|
|
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(
|
2019-10-20 16:59:02 +02:00
|
|
|
branch => branch.name === this.props.selectedBranch
|
2019-04-24 16:55:24 +02:00
|
|
|
);
|
2019-10-19 16:38:07 +02:00
|
|
|
this.setState({
|
2019-10-20 16:59:02 +02:00
|
|
|
selectedBranch
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
2019-04-24 16:55:24 +02:00
|
|
|
}
|
2018-10-23 11:24:23 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 17:04:28 +02:00
|
|
|
render() {
|
2019-10-08 16:42:08 +02:00
|
|
|
const { branches, label, disabled } = this.props;
|
2018-10-17 10:06:26 +02:00
|
|
|
|
2018-10-16 17:04:28 +02:00
|
|
|
if (branches) {
|
|
|
|
|
return (
|
2019-10-20 16:59:02 +02:00
|
|
|
<div className={classNames("field", "is-horizontal")}>
|
2019-10-08 16:42:08 +02:00
|
|
|
<ZeroflexFieldLabel
|
2019-10-20 16:59:02 +02:00
|
|
|
className={classNames("field-label", "is-normal")}
|
2018-10-17 11:33:40 +02:00
|
|
|
>
|
2019-10-20 16:59:02 +02:00
|
|
|
<label className={classNames("label", "is-size-6")}>{label}</label>
|
2019-10-08 16:42:08 +02:00
|
|
|
</ZeroflexFieldLabel>
|
2018-10-17 11:33:40 +02:00
|
|
|
<div className="field-body">
|
2019-10-20 16:59:02 +02:00
|
|
|
<NoBottomMarginField className={classNames("field", "is-narrow")}>
|
2019-10-10 13:47:46 +02:00
|
|
|
<MinWidthControl className="control">
|
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
|
2019-10-20 16:59:02 +02:00
|
|
|
: ""
|
2018-10-17 11:33:40 +02:00
|
|
|
}
|
|
|
|
|
/>
|
2019-10-08 16:42:08 +02:00
|
|
|
</MinWidthControl>
|
|
|
|
|
</NoBottomMarginField>
|
2018-10-17 11:33:40 +02:00
|
|
|
</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) {
|
2019-10-19 16:38:07 +02:00
|
|
|
this.setState({
|
2019-10-20 16:59:02 +02:00
|
|
|
selectedBranch: undefined
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
2018-12-14 20:20:00 +01:00
|
|
|
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);
|
2019-10-19 16:38:07 +02:00
|
|
|
this.setState({
|
2019-10-20 16:59:02 +02:00
|
|
|
selectedBranch: branch
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
2018-10-16 17:04:28 +02:00
|
|
|
};
|
|
|
|
|
}
|