Files
SCM-Manager/scm-ui/src/repos/containers/BranchSelector.js

96 lines
2.3 KiB
JavaScript
Raw Normal View History

// @flow
import React from "react";
import type { Branch } from "@scm-manager/ui-types";
import DropDown from "../components/DropDown";
import { translate } from "react-i18next";
import injectSheet from "react-jss";
import { compose } from "redux";
import classNames from "classnames";
const styles = {
zeroflex: {
flexGrow: 0
2018-12-12 09:26:21 +01:00
},
iefixwidth: {
minWidth: "4.5rem"
}
};
type Props = {
branches: Branch[], // TODO: Use generics?
2018-10-17 14:11:28 +02:00
selected: (branch?: Branch) => void,
selectedBranch: string,
// context props
classes: Object,
t: string => string
};
type State = { selectedBranch?: Branch };
class BranchSelector extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {};
}
componentDidMount() {
this.props.branches
.filter(branch => branch.name === this.props.selectedBranch)
.forEach(branch => this.setState({ selectedBranch: branch }));
}
render() {
const { branches, classes, t } = this.props;
if (branches) {
return (
<div className="box field is-horizontal">
<div
2018-12-12 09:26:21 +01:00
className={classNames(
"field-label",
"is-normal",
classes.zeroflex,
classes.iefixwidth
)}
>
<label className="label">{t("branch-selector.label")}</label>
</div>
<div className="field-body">
<div className="field is-narrow">
<div className="control">
<DropDown
className="is-fullwidth"
options={branches.map(b => b.name)}
optionSelected={this.branchSelected}
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;
const branch = branches.find(b => b.name === branchName);
selected(branch);
this.setState({ selectedBranch: branch });
};
}
export default compose(
injectSheet(styles),
translate("repos")
)(BranchSelector);