/
app.js
145 lines (135 loc) · 3.84 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
import React, { Component } from 'react';
import { View, Text, StyleSheet, Platform, ListView, Keyboard } from 'react-native';
import Header from './header';
import Footer from './footer';
import Row from './row';
const filterItems = (filter, items) => {
return items.filter((item) => {
if (filter === "ALL") return true;
if (filter === "COMPLETE") return item.complete;
if (filter === "ACTIVE") return !item.complete;
})
}
export default class App extends Component {
constructor(props) {
super(props);
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
allComplete: false,
filter: "ALL",
value: "",
items: [],
dataSource: ds.cloneWithRows([])
}
this.handleAddItem = this.handleAddItem.bind(this);
this.handleRemoveItem = this.handleRemoveItem.bind(this);
this.handleToggleAllComplete = this.handleToggleAllComplete.bind(this);
this.handleToggleComplete = this.handleToggleComplete.bind(this);
this.setSource = this.setSource.bind(this);
this.handleFilter = this.handleFilter.bind(this);
}
handleFilter(filter) {
this.setSource(this.state.items, filterItems(filter, this.state.items), {filter})
}
setSource(items, itemsDataSource, otherState = {}) {
this.setState({
items,
dataSource: this.state.dataSource.cloneWithRows(itemsDataSource),
...otherState
})
}
handleRemoveItem(key) {
const newItems = this.state.items.filter((item) => {
return item.key !== key
})
this.setSource(newItems, filterItems(this.state.filter, newItems));
}
handleToggleComplete(key, complete) {
const newItems = this.state.items.map((item) => {
if (item.key !== key ) return item;
return {
...item,
complete
}
})
this.setSource(newItems, filterItems(this.state.filter, newItems));
}
handleToggleAllComplete() {
const complete = !this.state.allComplete;
const newItems = this.state.items.map((item) => ({
...item,
complete
}))
this.setSource(newItems, filterItems(this.state.filter, newItems), { allComplete: complete })
}
handleAddItem() {
if(!this.state.value) return;
const newItems = [
...this.state.items,
{
key: Date.now(),
text: this.state.value,
complete: false
}
]
this.setSource(newItems, filterItems(this.state.filter, newItems), { value: "" })
}
render() {
return (
<View style={styles.container}>
<Header
value={this.state.value}
onAddItem={this.handleAddItem}
onChange={(value) => this.setState({value})}
onToggleAllComplete={this.handleToggleAllComplete}
/>
<View style={styles.content}>
<ListView
style={styles.list}
enableEmptySections
dataSource={this.state.dataSource}
onScroll={() => Keyboard.dismiss()}
renderRow={({key, ...value}) => {
return (
<Row
key={key}
onComplete={(complete) => this.handleToggleComplete(key, complete)}
onRemove={() => this.handleRemoveItem(key)}
{...value}
/>
)
}}
renderSeperator={(sectionId, rowId) => {
return <View key={rowId} style={styles.seperator} />
}}
/>
</View>
<Footer
filter={this.state.filter}
onFilter={this.handleFilter}
/>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F5F5F5',
...Platform.select({
ios: {
paddingTop: 30
}
})
},
content: {
flex: 1
},
list: {
backgroundColor: '#FFF'
},
seperator: {
borderWidth: 1,
borderColor: '#F5F5F5'
}
})