-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
121 lines (102 loc) · 2.99 KB
/
Copy pathapp.js
File metadata and controls
121 lines (102 loc) · 2.99 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
115
116
117
118
119
120
121
class Note extends React.Component {
constructor(props) {
super(props);
this.state = {
editing: false
}
}
componentWillMount = () => {
this.style = {
right: this.randomBetween(0, window.innerWidth - 150, 'px'),
top: this.randomBetween(0, window.innerHeight - 150, 'px')
}
}
randomBetween = (x, y, s) => {
return (x + Math.ceil(Math.random() * (y - x))) + s
}
edit = () => {
this.setState({ editing: true })
}
save = () => {
this.props.onChange(this.refs.newText.value, this.props.id)
this.setState({ editing: false })
}
delete = () => {
this.props.onRemove(this.props.id)
}
renderForm = () => {
return (
<div className="note" style={this.style}>
<textarea ref="newText" rows="7"></textarea>
<br />
<button className="btn-custom" onClick={this.save}>Save</button>
</div>
)
}
renderDisplay = () => {
return (
<div className="note" style={this.style}>
<p>{this.props.children}</p>
<span>
<button className="btn-custom" onClick={this.edit}>Edit</button>
<button className="btn-custom" onClick={this.delete}>Delete</button>
</span>
</div>
)
}
render() {
return (<ReactDraggable>{
(this.state.editing) ? this.renderForm() : this.renderDisplay()
}</ReactDraggable>)
}
}
ReactDOM.render(<Note>Hello world</Note>, document.getElementById('react-container'))
class Board extends React.Component {
constructor(props) {
super(props);
this.state = {
notes: []
}
}
nextId = () => {
this.uniqueId = this.uniqueId || 0
return this.uniqueId++
}
add = (text) => {
var notes = [
...this.state.notes,
{
id: this.nextID,
note: text
}
]
this.setState({ notes })
}
update = (newText, id) => {
var notes = this.state.notes.map(
note => (note.id !== id) ?
note :
{
...note,
note: newText
}
)
this.setState({ notes })
}
remove = (id) => {
var notes = this.state.notes.filter(note => note.id !== id)
this.setState({ notes })
}
eachNote = (note) => {
return (<Note key={note.id} id={note.id} onChange={this.update} onRemove={this.remove}>{note.note}</Note>)
}
render() {
return (
<div className="board">
<button className="btn-custom" onClick={() => this.add()}>+ Add Note</button>
{this.state.notes.map(this.eachNote)}
</div>
)
}
}
ReactDOM.render(<Board count="10" />, document.getElementById('react-container'))