Files
SCM-Manager/scm-ui/src/groups/components/AddUserField.js

66 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-08-03 10:37:56 +02:00
//@flow
import React from "react";
import { translate } from "react-i18next";
import AddButton from "../../components/buttons/AddButton";
import InputField from "../../components/forms/InputField";
2018-08-07 14:30:40 +02:00
import { isMemberNameValid } from "./groupValidation"
2018-08-03 10:37:56 +02:00
type Props = {
t: string => string,
addUser: string => void
};
type State = {
2018-08-07 14:30:40 +02:00
userToAdd: string,
validationError: boolean
2018-08-03 10:37:56 +02:00
};
class AddUserField extends React.Component<Props, State> {
constructor(props) {
super(props);
this.state = {
2018-08-07 14:30:40 +02:00
userToAdd: "",
validationError: false
2018-08-03 10:37:56 +02:00
};
}
2018-08-07 14:30:40 +02:00
2018-08-03 10:37:56 +02:00
render() {
const { t } = this.props;
return (
<div className="field">
<InputField
label={t("add-user-textfield.label")}
errorMessage={t("add-user-textfield.error")}
onChange={this.handleAddUserChange}
2018-08-07 14:30:40 +02:00
validationError={this.state.validationError}
2018-08-03 10:37:56 +02:00
value={this.state.userToAdd}
2018-08-06 16:57:46 +02:00
onReturnPressed={this.appendMember}
2018-08-03 10:37:56 +02:00
/>
<AddButton
2018-08-07 14:30:40 +02:00
2018-08-03 10:37:56 +02:00
label={t("add-user-button.label")}
action={this.addButtonClicked}
/>
</div>
);
}
2018-08-07 14:30:40 +02:00
2018-08-03 10:37:56 +02:00
addButtonClicked = (event: Event) => {
event.preventDefault();
2018-08-06 16:57:46 +02:00
this.appendMember();
};
appendMember = () => {
2018-08-03 10:37:56 +02:00
this.props.addUser(this.state.userToAdd);
this.setState({ ...this.state, userToAdd: "" });
2018-08-06 16:57:46 +02:00
}
2018-08-03 10:37:56 +02:00
handleAddUserChange = (username: string) => {
2018-08-07 14:30:40 +02:00
this.setState({ ...this.state, userToAdd: username, validationError: !isMemberNameValid(username)});
2018-08-03 10:37:56 +02:00
};
}
export default translate("groups")(AddUserField);