-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
118 lines (112 loc) · 2.87 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
import React, {useState} from 'react';
import { StyleSheet, Text, View, KeyboardAvoidingView, TextInput, TouchableOpacity, Platform, Keyboard, ScrollView } from 'react-native';
import Task from './components/Task'
export default function App() {
const [task, setTask] = useState()
const [error, setError] = useState()
const [tasks, setTasks] = useState([])
const addTaskHandler = () => {
if(!task) {
setError('your task is empty!')
return
}
Keyboard.dismiss()
if(error) {
setError(null)
}
setTasks(currentTasks => [...currentTasks, task])
setTask(null)
}
const removeTaskHandler = (index) => {
let updatedTasks = [...tasks]
updatedTasks.splice(index, 1)
setTasks(updatedTasks)
}
return (
<View style={styles.container}>
<View style={styles.tasksWrapper}>
<Text style={styles.titleSection}>Today's Tasks</Text>
{error ?
<View style={styles.errorContainer}>
<Text style={styles.errorText}>{error}</Text>
</View>: null
}
<View style={styles.items}>
<ScrollView>
{tasks.map((item, index) => <Task text={item} key={index} onDelete={() =>removeTaskHandler(index)} />)}
</ScrollView>
</View>
</View>
<KeyboardAvoidingView
behavior={Platform.OS == "ios" ? 'padding': 'height'}
style={styles.writeTaskWrapper}>
<TextInput style={styles.input} placeholder="Add New Task" onChangeText={text => setTask(text)} value={task}/>
<TouchableOpacity onPress={addTaskHandler}>
<View style={styles.addWrapper}>
<Text style={styles.addText}>+</Text>
</View>
</TouchableOpacity>
</KeyboardAvoidingView>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#E8EAED',
},
tasksWrapper: {
paddingTop: 80,
paddingHorizontal: 20
},
titleSection: {
fontSize: 24,
fontWeight: 'bold',
},
items: {
marginTop: 30
},
writeTaskWrapper: {
position: 'absolute',
bottom: 60,
width: '100%',
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-around'
},
input: {
width: 250,
padding: 15,
backgroundColor: '#fff',
borderRadius: 60,
borderColor: '#C0C0C0',
borderWidth: 1
},
addWrapper: {
width: 60,
height: 60,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#fff',
borderRadius: 60,
borderColor: '#C0C0C0',
borderWidth: 1,
},
addText: {
fontSize: 50,
opacity: 0.6
},
errorContainer: {
padding: 5,
backgroundColor: 'red',
opacity: 0.35,
borderColor: '#C0C0C0',
borderWidth: 1,
borderRadius:5,
marginTop: 5,
},
errorText: {
fontWeight: 'bold',
alignSelf: 'center'
}
});