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

111 lines
2.7 KiB
JavaScript
Raw Normal View History

// @flow
import React from "react";
import type { Branch } from "@scm-manager/ui-types";
import injectSheet from "react-jss";
import classNames from "classnames";
2018-12-14 16:01:57 +01:00
import DropDown from "./forms/DropDown";
const styles = {
zeroflex: {
flexBasis: "inherit",
flexGrow: 0
2018-12-12 09:26:21 +01:00
},
minWidthOfControl: {
minWidth: "10rem"
},
labelSizing: {
fontSize: "1rem !important"
},
noBottomMargin: {
marginBottom: "0 !important"
}
};
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,
disabled?: boolean,
// context props
2018-12-14 16:01:57 +01:00
classes: Object
};
type State = { selectedBranch?: Branch };
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, classes, label, disabled } = this.props;
if (branches) {
return (
<div
className={classNames("field is-horizontal", classes.noBottomMargin)}
>
<div
className={classNames("field-label is-normal", classes.zeroflex)}
>
<label className={classNames("label", classes.labelSizing)}>
{label}
</label>
</div>
<div className="field-body">
<div
className={classNames("field is-narrow", classes.noBottomMargin)}
>
<div className={classNames("control", classes.minWidthOfControl)}>
<DropDown
className="is-fullwidth"
options={branches.map(b => b.name)}
optionSelected={this.branchSelected}
disabled={!!disabled}
preselectedOption={
this.state.selectedBranch
? this.state.selectedBranch.name
: ""
}
/>
</div>
</div>
</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 });
};
}
2018-12-14 16:01:57 +01:00
export default injectSheet(styles)(BranchSelector);