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

96 lines
2.4 KiB
JavaScript
Raw Normal View History

//@flow
import React from "react";
import classNames from "classnames";
import styled from "styled-components";
import type { Branch } from "@scm-manager/ui-types";
2018-12-14 16:01:57 +01:00
import DropDown from "./forms/DropDown";
type Props = {
branches: Branch[],
2018-10-17 14:11:28 +02:00
selected: (branch?: Branch) => void,
2018-12-14 16:01:57 +01:00
selectedBranch?: string,
label: string,
disabled?: boolean
};
type State = { selectedBranch?: Branch };
2019-10-09 16:54:23 +02:00
const ZeroflexFieldLabel = styled.div`
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> {
constructor(props: Props) {
super(props);
this.state = {};
}
componentDidMount() {
const { branches } = this.props;
if (branches) {
const selectedBranch = branches.find(
branch => branch.name === this.props.selectedBranch
);
this.setState({ selectedBranch });
}
}
render() {
const { branches, label, disabled } = this.props;
if (branches) {
return (
<div className={classNames("field", "is-horizontal")}>
<ZeroflexFieldLabel
className={classNames("field-label", "is-normal")}
>
2019-10-09 16:54:23 +02:00
<label className={classNames("label", "is-size-6")}>{label}</label>
</ZeroflexFieldLabel>
<div className="field-body">
<NoBottomMarginField className={classNames("field", "is-narrow")}>
2019-10-10 13:47:46 +02:00
<MinWidthControl className="control">
<DropDown
className="is-fullwidth"
options={branches.map(b => b.name)}
optionSelected={this.branchSelected}
disabled={!!disabled}
preselectedOption={
this.state.selectedBranch
? this.state.selectedBranch.name
: ""
}
/>
</MinWidthControl>
</NoBottomMarginField>
</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 });
};
}