forked from TylerPottsDev/react-native-todo-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
96 lines (78 loc) · 2.04 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
import React from 'react';
import { Platform, StyleSheet, Text, View, FlatList } from 'react-native';
import Header from './components/Header';
import InputBar from './components/InputBar';
import TodoItem from './components/TodoItem';
export default class App extends React.Component {
constructor () {
super();
this.state = {
todoInput: '',
todos: [
{ id: 0, title: 'Take out the trash', done: false },
{ id: 1, title: 'Cook dinner', done: false }
]
}
}
addNewTodo () {
let todos = this.state.todos;
todos.unshift({
id: todos.length + 1,
title: this.state.todoInput,
done: false
});
this.setState({
todos: todos,
todoInput: ''
});
}
toggleDone (item) {
let todos = this.state.todos;
todos = todos.map((todo) => {
if (todo.id == item.id) {
todo.done = !todo.done;
}
return todo;
})
this.setState({todos});
}
removeTodo (item) {
let todos = this.state.todos;
todos = todos.filter((todo) => todo.id !== item.id);
this.setState({todos});
}
render() {
const statusbar = (Platform.OS == 'ios') ? <View style={styles.statusbar}></View> : <View></View>;
return (
<View style={styles.container}>
{statusbar}
<Header title="Todoapp" />
<InputBar
addNewTodo={() => this.addNewTodo()}
textChange={todoInput => this.setState({ todoInput })}
todoInput={this.state.todoInput}
/>
<FlatList
data={this.state.todos}
extraData={this.state}
keyExtractor={(item, index) => index.toString()}
renderItem={({item, index}) => {
return (
<TodoItem todoItem={item} toggleDone={() => this.toggleDone(item)} removeTodo={() => this.removeTodo(item)} />
)
}}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
},
statusbar: {
backgroundColor: '#FFCE00',
height: 20
}
});