Files
SCM-Manager/scm-ui/src/repos/branches/components/BranchForm.js

119 lines
2.7 KiB
JavaScript
Raw Normal View History

2019-04-03 13:12:07 +02:00
// @flow
import React from "react";
2019-04-03 13:12:07 +02:00
import { translate } from "react-i18next";
import type { Repository, Branch } from "@scm-manager/ui-types";
import {
Select,
InputField,
SubmitButton,
validation as validator
} from "@scm-manager/ui-components";
import { orderBranches } from "../util/orderBranches";
2019-04-03 13:12:07 +02:00
type Props = {
submitForm: Branch => void,
repository: Repository,
branches: Branch[],
loading?: boolean,
transmittedName?: string,
2019-04-03 13:12:07 +02:00
t: string => string
};
type State = {
source?: string,
name?: string,
nameValidationError: boolean
};
class BranchForm extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
nameValidationError: false
};
}
isFalsy(value) {
return !value;
}
2019-04-03 13:12:07 +02:00
isValid = () => {
const { source, name } = this.state;
return !(
this.state.nameValidationError ||
this.isFalsy(source) ||
this.isFalsy(name)
);
2019-04-03 13:12:07 +02:00
};
submit = (event: Event) => {
event.preventDefault();
if (this.isValid()) {
this.props.submitForm(this.state.branch);
}
};
render() {
const { t, branches, loading } = this.props;
const { name } = this.state;
orderBranches(branches);
2019-04-03 13:12:07 +02:00
const options = branches.map(branch => ({
label: branch.name,
value: branch.name
}));
return (
<>
2019-04-03 13:12:07 +02:00
<form onSubmit={this.submit}>
<div className="columns">
<div className="column">
<Select
name="source"
label={t("branches.create.source")}
options={options}
onChange={this.handleSourceChange}
loading={loading}
/>
<InputField
name="name"
label={t("branches.create.name")}
onChange={this.handleNameChange}
value={name ? name : ""}
validationError={this.state.nameValidationError}
errorMessage={t("validation.branch.nameInvalid")}
2019-04-03 13:12:07 +02:00
/>
</div>
</div>
<div className="columns">
<div className="column">
<SubmitButton
disabled={!this.isValid()}
loading={loading}
label={t("branches.create.submit")}
/>
</div>
</div>
</form>
</>
);
}
2019-04-03 13:12:07 +02:00
handleSourceChange = (source: string) => {
this.setState({
...this.state,
source
});
};
handleNameChange = (name: string) => {
this.setState({
nameValidationError: !validator.isNameValid(name),
...this.state,
name
});
};
}
export default translate("repos")(BranchForm);