|
1 | | -<script setup lang="ts"> |
2 | | -import HelloWorld from './components/HelloWorld.vue' |
3 | | -import TheWelcome from './components/TheWelcome.vue' |
4 | | -</script> |
5 | | - |
6 | 1 | <template> |
7 | | - <header> |
8 | | - <img alt="Vue logo" class="logo" src="./assets/logo.svg" width="125" height="125" /> |
9 | | - |
10 | | - <div class="wrapper"> |
11 | | - <HelloWorld msg="You did it!" /> |
12 | | - </div> |
13 | | - </header> |
14 | | - |
15 | | - <main> |
16 | | - <TheWelcome /> |
17 | | - </main> |
| 2 | + <h1>My To-Do List</h1> |
| 3 | + <div> |
| 4 | + <!-- Input for adding new tasks --> |
| 5 | + <input v-model="newTask" @keyup.enter="addTask" placeholder="Add a new task" /> |
| 6 | + <button @click="addTask">Add</button> |
| 7 | + </div> |
| 8 | + <ul> |
| 9 | + <!-- Loop through tasks and display them --> |
| 10 | + <li v-for="(task, index) in tasks" :key="task.id"> |
| 11 | + <span :class="{ completed: task.completed }"> |
| 12 | + {{ task.text }} |
| 13 | + </span> |
| 14 | + <button @click="toggleTask(index)"> |
| 15 | + {{ task.completed ? 'Undo' : 'Complete' }} |
| 16 | + </button> |
| 17 | + <button @click="deleteTask(index)">Delete</button> |
| 18 | + </li> |
| 19 | + </ul> |
18 | 20 | </template> |
19 | 21 |
|
20 | | -<style scoped> |
21 | | -header { |
22 | | - line-height: 1.5; |
| 22 | +<script setup lang="ts"> |
| 23 | +import { ref } from 'vue' |
| 24 | +
|
| 25 | +// Define the structure of a task |
| 26 | +interface Task { |
| 27 | + id: number |
| 28 | + text: string |
| 29 | + completed: boolean |
23 | 30 | } |
24 | 31 |
|
25 | | -.logo { |
26 | | - display: block; |
27 | | - margin: 0 auto 2rem; |
| 32 | +// Reactive state |
| 33 | +const newTask = ref('') |
| 34 | +const tasks = ref<Task[]>([]) |
| 35 | +
|
| 36 | +// Add a new task |
| 37 | +function addTask() { |
| 38 | + if (newTask.value.trim() === '') return // Prevent empty tasks |
| 39 | + tasks.value.push({ |
| 40 | + id: new Date().getTime(), |
| 41 | + text: newTask.value, |
| 42 | + completed: false, |
| 43 | + }) |
| 44 | + newTask.value = '' // Clear the input |
28 | 45 | } |
29 | 46 |
|
30 | | -@media (min-width: 1024px) { |
31 | | - header { |
32 | | - display: flex; |
33 | | - place-items: center; |
34 | | - padding-right: calc(var(--section-gap) / 2); |
35 | | - } |
| 47 | +// Toggle task completion |
| 48 | +function toggleTask(index: number) { |
| 49 | + tasks.value[index].completed = !tasks.value[index].completed |
| 50 | +} |
36 | 51 |
|
37 | | - .logo { |
38 | | - margin: 0 2rem 0 0; |
39 | | - } |
| 52 | +// Delete a task |
| 53 | +function deleteTask(index: number) { |
| 54 | + tasks.value.splice(index, 1) |
| 55 | +} |
| 56 | +</script> |
40 | 57 |
|
41 | | - header .wrapper { |
42 | | - display: flex; |
43 | | - place-items: flex-start; |
44 | | - flex-wrap: wrap; |
45 | | - } |
| 58 | +<style scoped> |
| 59 | +.completed { |
| 60 | + text-decoration: line-through; |
| 61 | + color: gray; |
| 62 | +} |
| 63 | +ul { |
| 64 | + list-style-type: none; |
| 65 | + padding: 0; |
| 66 | +} |
| 67 | +li { |
| 68 | + margin: 10px 0; |
| 69 | +} |
| 70 | +button { |
| 71 | + margin-left: 10px; |
46 | 72 | } |
47 | 73 | </style> |
0 commit comments