-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
156 lines (141 loc) · 4.23 KB
/
app.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import React from 'react'
import ReactDOM from 'react-dom'
import 'bulma/bulma'
class List extends React.Component{
constructor(props){
super(props)
this.state = {
filteredList: []
}
this.searchItem = this.searchItem.bind(this)
}
componentDidMount(){
this.setState({
filteredList: this.props.items
})
}
componentWillReceiveProps(nextProps){
this.setState({
filteredList: nextProps.items
})
}
searchItem(e){
e.preventDefault()
let currentList = []
let newList = []
if(e.target.value !== ""){
currentList = this.props.items
newList = currentList.filter((item) => {
const lc = item.toLowerCase()
const filter = e.target.value.toLowerCase()
return lc.includes(filter)
})
}else{ // if search bar is empty show all the items.
newList = this.props.items
}
this.setState({
filteredList: newList
})
}
render(){
const { deleteItem } = this.props
const { filteredList } =this.state
return(
<section className="section">
<div>
<input
type="text"
className="input"
placeholder="search"
onChange={this.searchItem}
/>
</div>
<ul>
{filteredList.map((item) => (
<li key={item}>
{item}
<span
className="delete"
onClick={() => deleteItem(item)}>
X
</span>
</li>
))}
</ul>
</section>
)
}
}
class App extends React.Component{
constructor(props){
super(props)
this.state = {
list : [
"Finish the front end for Spazzo",
"Start with 'Would you rather'"
]
}
this.addItem = this.addItem.bind(this)
this.removeItem = this.removeItem.bind(this)
}
addItem (e) {
e.preventDefault() //prevent button from submitting the form like a retard.
let list = this.state.list
const newItem = document.getElementById('addInput')
if(newItem.value !== ""){
list.push(newItem.value)
this.setState({
list
})
//reset the form to normal..
newItem.classList.remove("is-danger")
newItem.value = ""
}else{
newItem.classList.add("is-danger")
}
}
removeItem(item){
// Put our list into an array
const list = this.state.list.slice()
// Check to see if item passed in matches item in array
list.some((el,i) => {
if(el === item){
list.splice(i, 1) // If item matches, remove it from array
return true
}
})
this.setState({
list
})
}
render(){
const { list } = this.state
return(
<div className="content">
<div className="container">
<List
items={list}
deleteItem={this.removeItem}/>
<hr/>
<section className="section">
<form className="form" id="addItemForm">
<input
type="text"
className="input"
id="addInput"
placeholder="Say something I'm giving up on you :("
/>
<button className="button is-info" onClick={this.addItem}>
Add Item
</button>
</form>
</section>
</div>
</div>
)
}
}
ReactDOM.render(
<App />,
document.getElementById('app')
)