-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
97 lines (87 loc) · 3.47 KB
/
App.tsx
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
import React, { useState } from 'react';
import { View, Text, TextInput, Button, TouchableOpacity, FlatList, Dimensions, Alert } from 'react-native';
import Icon from 'react-native-vector-icons/MaterialIcons';
const { width, height} = Dimensions.get('screen');
const App = () => {
const [todos, setTodos] = useState([
{id: 1, name: 'reading', completed: true},
{id: 2, name: 'coding', completed: false}
]);
const [ inputText, setInputText] = useState('');
const addTodo = () => {
if(inputText == '') {
Alert.alert('Error', 'Please Input Todo');
} else {
const NewTodo = {
id: Math.random(),
name: inputText,
completed: false
}
setTodos([...todos, NewTodo]);
setInputText('');
}
}
const markTodo = id => {
const NewTodo = todos.map(item => {
if(item.id == id){
return{...item, completed: true}
}
return item;
});
setTodos(NewTodo);
}
const deleteTodo = id => {
const NewTodo = todos.filter(item => item.id !== id);
setTodos(NewTodo);
}
const clearAll = () => {
setTodos([])
}
return (
<View style={{ flex: 1, margin: 0, padding: 0,}}>
<View style={{ flexDirection: 'row', justifyContent: 'space-between', padding: 20, backgroundColor: 'white', elevation: 10}}>
<Text style={{ fontSize: 20, fontWeight: 'bold', color: 'black'}}>TODO LIST</Text>
<TouchableOpacity onPress={clearAll}>
<Icon name='delete' size={30} color='red' />
</TouchableOpacity>
</View>
<FlatList
data={todos}
renderItem={({item, index}) => (
<View style={{ padding: 15, elevation: 5, borderRadius: 5, backgroundColor: 'white', marginBottom: 10, flexDirection: 'row', justifyContent: 'space-between'}}>
<Text style={{ fontSize: 16, fontWeight: 'bold', marginHorizontal: 5, textDecorationLine: item?.completed ? 'line-through' : 'none' }}
>
{item.name}
</Text>
<View style={{ flexDirection: 'row'}}>
{
!item?.completed && (
<TouchableOpacity style={{ backgroundColor: 'green', marginHorizontal: 5 }} onPress={() => markTodo(item?.id)}>
<Icon name='done' size={24} color='white' />
</TouchableOpacity>
)}
<TouchableOpacity style={{ backgroundColor: 'red', marginHorizontal: 5}} onPress={() => deleteTodo(item?.id)}>
<Icon name='delete' size={24} color='white'/>
</TouchableOpacity>
</View>
</View>
)}
showsVerticalScrollIndicator={false}
style={{ marginTop: 15, marginHorizontal: 10, marginBottom: 80 }}
/>
<View style={{ position: 'absolute', bottom: 0, flexDirection: 'row', marginHorizontal: 5, marginVertical: 20, justifyContent:'space-between'}}>
<View style={{ width: width/1.4, borderRadius: 100, elevation: 5, backgroundColor: 'white', paddingLeft: 20,}}>
<TextInput
placeholder='Input'
value={inputText}
onChangeText={text => setInputText(text)}
/>
</View>
<TouchableOpacity style={{ borderRadius: 55, width: 50, marginLeft: 15, height: 50, backgroundColor: 'red', padding: 10 }} onPress={addTodo}>
<Icon name='add' size={30} color='white'/>
</TouchableOpacity>
</View>
</View>
)
}
export default App