-
Notifications
You must be signed in to change notification settings - Fork 1
/
TodoList.js
116 lines (110 loc) · 2.43 KB
/
TodoList.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
import React, { Component } from 'react';
import {
FlatList,
Text,
View,
StyleSheet,
TextInput,
Button,
ListView,
} from 'react-native';
// import { Constants } from 'expo';
export default class App extends Component {
state = {
inputValue: '',
todoList: [],
};
_handleTextChange = value => {
const inputValue = value;
this.setState(() => ({
inputValue,
}));
};
_handleSendButtonPress = () => {
if (!this.state.inputValue) {
return;
}
this.setState(prevState => ({
todoList: [...prevState.todoList, this.state.inputValue],
inputValue: '',
}));
};
_handleDeleteButtonPress = id => {
this.setState(prevState => {
const todoList = prevState.todoList.filter(
(item, i) => parseInt(id) !== i
);
return {
todoList,
};
});
};
render() {
return (
<View style={styles.container}>
<View style={styles.formView}>
<TextInput
style={styles.inputForm}
value={this.state.inputValue}
onChangeText={this._handleTextChange}
placeholder="Input todo"
/>
<Button title="Add" onPress={this._handleSendButtonPress} />
</View>
<FlatList
data={this.state.todoList}
style={styles.listView}
renderItem={({ item, index }) => {
return (
<View style={styles.todoItem}>
<Text style={styles.todoText}>{item}</Text>
<Button
title="Delete"
onPress={() => {
this._handleDeleteButtonPress(index);
}}
style={styles.deleteButton}
/>
</View>
);
}}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
paddingTop: 40,
backgroundColor: '#eee',
},
formView: {
borderBottomWidth: 1,
borderColor: '#ccc',
paddingBottom: 8,
},
inputForm: {
backgroundColor: '#fff',
width: 320,
height: 40,
padding: 8,
marginBottom: 8,
},
todoItem: {
alignItems: 'center',
padding: 8,
width: 320,
borderBottomWidth: 1.5,
borderColor: '#e0e0e0',
backgroundColor: '#fff',
// border: '1 solid #333',
flex: 1,
flexDirection: 'row',
},
todoText: {
flex: 1,
},
});