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; }; 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 { 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 (
b.name)} optionSelected={this.branchSelected} disabled={!!disabled} preselectedOption={this.state.selectedBranch ? this.state.selectedBranch.name : ""} />
); } else { return null; } } branchSelected = (branchName: string) => { const { branches, selected } = this.props; if (!branchName) { this.setState({ selectedBranch: undefined }); selected(undefined); return; } const branch = branches.find(b => b.name === branchName); selected(branch); this.setState({ selectedBranch: branch }); }; }