-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
117 lines (110 loc) · 2.74 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
import { useState } from "react";
import {
FlatList,
StyleSheet,
Text,
TextInput,
TouchableOpacity,
View,
} from "react-native";
import { Icon } from "react-native-elements";
export default function App() {
const [taskText, setTaskText] = useState("");
const [tasks, setTasks] = useState([]);
const [isEditing, setIsEditing] = useState(null)
const handleSaveTask = () => {
if (!taskText.trim()) return;
if (isEditing){
setTasks(
tasks.map((task) => (task.id === isEditing ? {...task, text: taskText}: task))
)
setIsEditing(null);
} else {
const newTask = { id: Date.now().toString(), text: taskText };
setTasks([...tasks, newTask]);
}
setTaskText("")
};
const handleEdit = (item) => {
setTaskText(item.text);
setIsEditing(item.id);
}
const handleDelete = (id) => {
setTasks(tasks.filter((task) => task.id !== id));
};
const renderTask = ({ item }) => (
<View style={styles.task}>
<Text style={styles.taskText}>{item.text}</Text>
<View style={styles.buttonContainer}>
<TouchableOpacity style={styles.editButton}>
<Icon name="edit" color="#4caf50" onPress={() => handleEdit(item)}>
編集
</Icon>
</TouchableOpacity>
<TouchableOpacity style={styles.deleteButton} onPress={() => handleDelete(item.id)}>
<Icon name="delete" color="#f44336">
削除
</Icon>
</TouchableOpacity>
</View>
</View>
);
return (
<View style={styles.container}>
<Text style={styles.title}>TODOアプリ</Text>
<TextInput
placeholder="タスクを入力"
style={styles.input}
onChangeText={setTaskText}
value={taskText}
/>
<TouchableOpacity style={styles.saveBotton} onPress={handleSaveTask}>
<Text style={styles.saveBottonText}>{isEditing ? "編集" : "追加"}</Text>
</TouchableOpacity>
<FlatList data={tasks} renderItem={renderTask} />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 40,
},
title: {
fontSize: 24,
fontWeight: "bold",
marginBottom: 20,
},
input: {
borderColor: "#ccceee",
padding: 10,
marginBottom: 10,
borderRadius: 6,
borderWidth: 2,
},
saveBotton: {
backgroundColor: "green",
padding: 10,
borderRadius: 5,
marginBottom: 20,
},
saveBottonText: {
color: "#fff",
textAlign: "center",
},
task: {
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
marginBottom: 20,
padding: 10,
borderRadius: 5,
backgroundColor: "#eeee",
},
buttonContainer: {
flexDirection: "row",
},
taskText: {
maxWidth: "80%",
},
});