-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathCommentForm.js
65 lines (59 loc) · 1.63 KB
/
CommentForm.js
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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import styles from '../PostComments.css';
export class CommentForm extends Component {
/* state = {
author: '',
text: '',
}*/
toaddComment = () => {
const author = this.refs.name;
const text = this.refs.comment;
if (author.value && text.value) {
this.props.addComment(author.value, text.value);
this.refs.name.value = '';
this.refs.comment.value = '';
} else {
alert('No, pls fill all inputs ');
}
}
handleChange = (e) => {
this.setState({
[e.target.name]: [e.target.value],
});
}
render() {
return (
<div className={`${styles['comment-form']}`}>
<h3>New comment</h3>
<div>
<input
type="text"
placeholder="Author name"
required="required"
className={`${styles['author-input']}`}
ref="name"
name="author"
onChange={this.handleChange}
/>
</div>
<div className={`${styles['form-item']}`}>
<textarea type="text" name="text" className={`${styles['comment-input']}`} ref="comment" onChange={this.handleChange} />
</div>
<div className={`${styles['form-item']}`}>
<input
type="button"
className={`${styles['sent-comment-button']}`}
value="Add comment"
onClick={this.toaddComment}
/>
</div>
</div>
);
}
}
CommentForm.propTypes = {
addComment: PropTypes.func.isRequired,
};
export default connect()(CommentForm);