-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
141 lines (132 loc) · 3.51 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
import React from 'react'
import {
StyleSheet,
Text,
View,
TextInput,
TouchableOpacity,
ListView,
Keyboard,
Switch
} from 'react-native'
import { Constants } from 'expo'
import { Ionicons } from '@expo/vector-icons'
import { map } from 'ramda'
const Header = props => {
return (
<View style={styles.header}>
<TouchableOpacity onPress={props.onToggleAllComplete}>
<Ionicons name="ios-checkmark-circle" size={32} color="lightblue" />
</TouchableOpacity>
<TextInput
placeholder="Hey dude, what do you want to do?"
blurOnSubmit={false}
returnKeyType="done"
style={styles.input}
value={props.value}
onChangeText={props.onChange}
onSubmitEditing={props.onAddItem}
/>
</View>
)
}
const Footer = () => <View />
const Row = ({ text, complete }) => {
return (
<View style={rstyles.container}>
<Switch value={complete} />
<View style={rstyles.textWrap}>
<Text style={[rstyles.text, complete && rstyles.complete ]}>{text}</Text>
</View>
</View>
)
}
const rstyles = StyleSheet.create({
container: {
padding: 10,
flexDirection: 'row',
alignItems: 'flex-start',
justifyContent: 'space-between'
},
text: { fontSize: 24, color: '#4d4d4d' },
complete: { textDecorationLine: 'line-through' },
textWrap: { flex: 1, marginHorizontal: 10 }
})
export default class App extends React.Component {
constructor(props) {
super(props)
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2})
this.state = {
value: '',
items: [],
dataSource: ds.cloneWithRows([]),
allComplete: false
}
this.handleAddItem = this.handleAddItem.bind(this)
this.setSource = this.setSource.bind(this)
this.handleToggleAllComplete = this.handleToggleAllComplete.bind(this)
}
handleToggleAllComplete(){
console.log('toggle')
const complete = !this.state.allComplete
const newItems = map((item) => ({...item, complete}), this.state.items)
this.setSource(newItems, newItems, {allComplete: complete})
}
setSource(items, itemsDataSource, otherState={}){
this.setState({
items,
dataSource: this.state.dataSource.cloneWithRows(itemsDataSource),
...otherState
})
}
handleAddItem() {
const newItems = [
...this.state.items,
{
key: new Date(),
text: this.state.value,
complete: false
}
]
this.setSource(newItems, newItems, { value: '' })
}
render() {
console.log('state', this.state)
return (
<View style={styles.container}>
<Header
value={this.state.value}
onChange={value => this.setState({ value })}
onAddItem={this.handleAddItem}
onToggleAllComplete={this.handleToggleAllComplete}
/>
<View style={styles.content}>
<ListView
enableEmptySections
onScroll={() => Keyboard.dismiss()}
dataSource={this.state.dataSource}
renderRow={({key, ...value}) => {
return <Row key={key} {...value} />
}}
/>
</View>
<Footer />
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: Constants.statusBarHeight,
backgroundColor: '#f5f5f5f5'
},
content: { flex: 1 },
input: { height: 50, marginLeft: 16, flex: 1 },
header: {
flexDirection: 'row',
paddingHorizontal: 16,
alignItems: 'center',
justifyContent: 'space-around'
}
})