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

95 lines
2.3 KiB
TypeScript
Raw Normal View History

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";
type Props = {
branches: Branch[];
selected: (branch?: Branch) => void;
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) {
2019-10-21 10:57:56 +02:00
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")}>
2019-10-21 10:57:56 +02:00
<ZeroflexFieldLabel className={classNames("field-label", "is-normal")}>
<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}
2019-10-21 10:57:56 +02:00
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
});
2018-12-14 20:20:00 +01:00
selected(undefined);
return;
}
const branch = branches.find(b => b.name === branchName);
selected(branch);
this.setState({
selectedBranch: branch
});
};
}