forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathco-author-form.js
More file actions
112 lines (98 loc) · 3.25 KB
/
Copy pathco-author-form.js
File metadata and controls
112 lines (98 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import React from 'react';
import PropTypes from 'prop-types';
import Author from '../models/author';
import Commands, {Command} from '../atom/commands';
import {autobind} from '../helpers';
export default class CoAuthorForm extends React.Component {
static propTypes = {
commands: PropTypes.object.isRequired,
onSubmit: PropTypes.func,
onCancel: PropTypes.func,
name: PropTypes.string,
}
static defaultProps = {
onSubmit: () => {},
onCancel: () => {},
}
constructor(props, context) {
super(props, context);
autobind(this, 'confirm', 'cancel', 'onNameChange', 'onEmailChange', 'validate', 'focusFirstInput');
this.state = {
name: this.props.name,
email: '',
submitDisabled: true,
};
}
componentDidMount() {
setTimeout(this.focusFirstInput);
}
render() {
return (
<div className="github-CoAuthorForm native-key-bindings">
<Commands registry={this.props.commands} target=".github-CoAuthorForm">
<Command command="core:cancel" callback={this.cancel} />
<Command command="core:confirm" callback={this.confirm} />
</Commands>
<label className="github-CoAuthorForm-row">
<span className="github-CoAuthorForm-label">Name:</span>
<input
type="text"
placeholder="Co-author name"
ref={e => (this.nameInput = e)}
className="input-text github-CoAuthorForm-name"
value={this.state.name}
onChange={this.onNameChange}
tabIndex="1"
/>
</label>
<label className="github-CoAuthorForm-row">
<span className="github-CoAuthorForm-label">Email:</span>
<input
type="email"
placeholder="foo@bar.com"
ref={e => (this.emailInput = e)}
className="input-text github-CoAuthorForm-email"
value={this.state.email}
onChange={this.onEmailChange}
tabIndex="2"
/>
</label>
<footer className="github-CoAuthorForm-row has-buttons">
<button className="btn github-CancelButton" tabIndex="3" onClick={this.cancel}>Cancel</button>
<button className="btn btn-primary" disabled={this.state.submitDisabled} tabIndex="4" onClick={this.confirm}>
Add Co-Author
</button>
</footer>
</div>
);
}
confirm() {
if (this.isInputValid()) {
this.props.onSubmit(new Author(this.state.email, this.state.name));
}
}
cancel() {
this.props.onCancel();
}
onNameChange(e) {
this.setState({name: e.target.value}, this.validate);
}
onEmailChange(e) {
this.setState({email: e.target.value}, this.validate);
}
validate() {
if (this.isInputValid()) {
this.setState({submitDisabled: false});
}
}
isInputValid() {
// email validation with regex has a LOT of corner cases, dawg.
// https://stackoverflow.com/questions/48055431/can-it-cause-harm-to-validate-email-addresses-with-a-regex
// to avoid bugs for users with nonstandard email addresses,
// just check to make sure email address contains `@` and move on with our lives.
return this.state.name && this.state.email.includes('@');
}
focusFirstInput() {
this.nameInput.focus();
}
}