-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathComp.js
More file actions
114 lines (101 loc) · 2.98 KB
/
Comp.js
File metadata and controls
114 lines (101 loc) · 2.98 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
113
114
import React from 'react';
import styles from './Comp.css';
export class Comp extends React.Component {
constructor(props) {
super(props);
this.state = {
arr: [{}],
click: false,
};
this.addComment = this.addComment.bind(this);
}
// Initialize main array to push state comments
arr = [];
addComment(index) {
let userName = this.refs.userName.value;
let userTitle = this.refs.title.value;
let userPost = this.refs.post.value;
let form = document.getElementById('inputForm');
let { arr } = this;
if (userName.length > 0 && userTitle.length > 0 && userPost.length > 0) {
arr.push({
name: userName,
title: userTitle,
post: userPost,
});
this.setState({
arr,
});
}
form.reset();
}
removeComment(index) {
const arr = this.arr;
arr.splice(index, 1);
this.addState();
}
addState() {
const { arr } = this;
this.setState({
arr,
});
}
render() {
const { arr } = this;
const { click } = this.state;
return (
<div className={`${styles['parent']}`}>
<form id="inputForm">
<input
type="text"
ref="userName"
placeholder="Enter your name"
required
/>
<input
type="text"
ref="title"
placeholder="Enter post title"
required
/>
<textarea
type="text"
ref="post"
required
/>
<input
type="button"
value="Add comment"
onClick={this.addComment}
className={`${styles['button']}`}
/>
</form>
{arr.map((elem, index) => (
<div
className={`${styles['list']}`}
onClick={() => this.editPost(index)}
>
<ul>
<li>
<span>Name : </span>{elem.name} <br />
</li>
<li>
<span>Title :</span> {elem.title} <br />
</li>
<li>
<span>Comment :</span> {elem.post} <br />
</li>
</ul>
<button
className={`${styles['list-button']}`}
onClick={() => { this.removeComment(index); }}
>
Delete comment
</button>
</div>
))}
</div>
);
}
}
export default Comp;