-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathListRender.js
97 lines (76 loc) · 1.96 KB
/
ListRender.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
import React, {useState} from 'react';
import { StyleSheet, Text, View, FlatList } from 'react-native';
import TodoItem from './todoItem';
import AddTodo from './addTodos';
import { useNavigation } from '@react-navigation/native';
/** Adapted from CS455 University of Regina; Dr. Trevor M. Tomesh
* TList()
* Purpose: contains the list of items/notes, which upon clik open a new screen. Includes the functions definitions:
pressHandler() and submitHandler()
* Parameter(s):
* <1>N/A
*
* Precondition(s):
* <1> N/A
*
* Returns: Item button
*
* Side effect:
* <1> Renders the list
* <2> onPress item adds a new screen (push)
* <3> onLongPress item removes the item with a vibration feedback
*
*/
export default function TList(){
const [todos, setTodos] = useState([
{ text: 'Task1', key: '1'},
{ text: 'Task2', key: '2'},
{ text: 'Task3', key: '3'}
]);
const navigation = useNavigation();
/** pressHandler
* Returns: list with the selected item removed
*/
const pressHandler = (key) => {
setTodos( prevTodos => {
if (todos.length-1 == 0)
navigation.goBack();
return prevTodos.filter(filterTodo => filterTodo.key != key);
});
};
/** submitHandler
* Returns: list with the item(of given input) added, with a random key
*/
const submitHandler = (text) =>{
setTodos((prevTodos) => {
return [
{text: text, key: Math.random().toString()}, ...prevTodos];
}
)
}
return (
<View style = {styles.container}>
<View style={styles.content}>
<AddTodo submitHandler = {submitHandler}/>
<View style = {styles.list}>
<FlatList
data ={todos}
renderItem={({item})=>(
<TodoItem item={item} pressHandler={pressHandler}/>
)}
/>
</View>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
},
content: {
padding: 40,
},
list: {
marginTop: 20,
},
});