|
1 | 1 | import React, { useState } from 'react' |
2 | | -import { StyleSheet, View, Text, Button } from 'react-native' |
| 2 | +import { |
| 3 | + StyleSheet, |
| 4 | + View, |
| 5 | + Text, |
| 6 | + TextInput, |
| 7 | + Button, |
| 8 | + FlatList, |
| 9 | +} from 'react-native' |
3 | 10 |
|
4 | 11 | export default function App() { |
5 | | - const [counter, setCounter] = useState(0) |
| 12 | + const [todo, setTodo] = useState([]) |
| 13 | + const [todoInputValue, setTodoInputValueChange] = useState('') |
| 14 | + |
| 15 | + const addTodo = () => { |
| 16 | + if (todoInputValue.length > 0) { |
| 17 | + const singleTodo = { |
| 18 | + id: todo.length + 1, |
| 19 | + task: todoInputValue, |
| 20 | + } |
| 21 | + setTodo([...todo, singleTodo]) |
| 22 | + setTodoInputValueChange('') |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + const deleteTodo = (deleteId) => { |
| 27 | + const newTodo = todo.filter((item) => item.id !== deleteId) |
| 28 | + setTodo(newTodo) |
| 29 | + } |
| 30 | + |
6 | 31 | return ( |
7 | 32 | <View style={styles.container}> |
8 | | - <Text style={styles.heading}>{counter}</Text> |
9 | | - <Button |
10 | | - title='Increase +' |
11 | | - color='#014294' |
12 | | - onPress={() => setCounter(counter + 1)} |
13 | | - /> |
14 | | - <Button |
15 | | - title='Decrease - ' |
16 | | - color='#ff6160' |
17 | | - onPress={() => setCounter(counter - 1)} |
18 | | - disabled={counter < 1 ? true : false} |
19 | | - /> |
20 | | - <Button |
21 | | - title='Reset to Zero' |
22 | | - color='#fdc5c8' |
23 | | - onPress={() => setCounter(0)} |
24 | | - disabled={counter < 1 ? true : false} |
25 | | - /> |
| 33 | + <View style={styles.heading}> |
| 34 | + <Text style={styles.title}> Simple Todo App</Text> |
| 35 | + </View> |
| 36 | + <View style={styles.todoContainer}> |
| 37 | + <TextInput |
| 38 | + style={styles.addTodoInput} |
| 39 | + onChangeText={(text) => setTodoInputValueChange(text)} |
| 40 | + value={todoInputValue} |
| 41 | + placeholder='Add task' |
| 42 | + /> |
| 43 | + <Button title='Add' color='#4374c1' onPress={() => addTodo()} /> |
| 44 | + <FlatList |
| 45 | + data={todo} |
| 46 | + renderItem={({ item }) => ( |
| 47 | + <View style={styles.todoListing}> |
| 48 | + <Text style={styles.todoListItem}>{item.task}</Text> |
| 49 | + <Button |
| 50 | + title='Delete' |
| 51 | + color='#fc6064' |
| 52 | + onPress={() => deleteTodo(item.id)} |
| 53 | + /> |
| 54 | + </View> |
| 55 | + )} |
| 56 | + keyExtractor={(item) => item.id} |
| 57 | + /> |
| 58 | + </View> |
26 | 59 | </View> |
27 | 60 | ) |
28 | 61 | } |
29 | 62 |
|
30 | 63 | const styles = StyleSheet.create({ |
31 | 64 | container: { |
32 | 65 | flex: 1, |
33 | | - backgroundColor: '#2a2b3d', |
34 | | - padding: 30, |
35 | | - justifyContent: 'center', |
| 66 | + backgroundColor: '#f6fbfe', |
36 | 67 | }, |
37 | 68 | heading: { |
38 | | - color: '#ff6160', |
39 | | - fontSize: 100, |
| 69 | + marginTop: 50, |
| 70 | + padding: 20, |
| 71 | + backgroundColor: '#010535', |
40 | 72 | textAlign: 'center', |
41 | 73 | }, |
| 74 | + title: { |
| 75 | + fontWeight: '700', |
| 76 | + textTransform: 'uppercase', |
| 77 | + color: '#fff', |
| 78 | + fontSize: 25, |
| 79 | + }, |
| 80 | + todoContainer: { |
| 81 | + marginTop: 30, |
| 82 | + padding: 30, |
| 83 | + }, |
| 84 | + addTodoInput: { |
| 85 | + height: 40, |
| 86 | + padding: 5, |
| 87 | + color: '#000', |
| 88 | + marginBottom: 10, |
| 89 | + borderBottomWidth: 2, |
| 90 | + borderBottomColor: 'grey', |
| 91 | + }, |
| 92 | + todoListing: { |
| 93 | + backgroundColor: '#010535', |
| 94 | + marginTop: 30, |
| 95 | + flex: 1, |
| 96 | + flexDirection: 'row', |
| 97 | + justifyContent: 'space-between', |
| 98 | + padding: 20, |
| 99 | + borderRadius: 5, |
| 100 | + }, |
| 101 | + todoListItem: { |
| 102 | + color: '#fff', |
| 103 | + fontSize: 25, |
| 104 | + }, |
42 | 105 | }) |
0 commit comments