Skip to content

Commit

Permalink
feat(playground): todo mvc
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Jan 25, 2024
1 parent 1beed73 commit 2f9e25d
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 17 deletions.
1 change: 1 addition & 0 deletions playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"build": "node ./setup/vite.js build"
},
"dependencies": {
"@vueuse/core": "^10.7.2",
"vue": "workspace:*"
},
"devDependencies": {
Expand Down
84 changes: 67 additions & 17 deletions playground/src/todo-mvc.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
<script setup lang="ts">
import { ref } from 'vue/vapor'
import { computed } from 'vue/vapor'
import { useLocalStorage } from '@vueuse/core'
interface Task {
title: string
completed: boolean
}
const tasks = ref<Task[]>([])
const value = ref('hello')
const tasks = useLocalStorage<Task[]>('tasks', [])
const value = useLocalStorage('value', '')
const remaining = computed(() => {
return tasks.value.filter(task => !task.completed).length
})
function handleAdd() {
tasks.value.push({
Expand All @@ -16,31 +21,76 @@ function handleAdd() {
// TODO: clear input
value.value = ''
}
function handleComplete(index: number, evt: Event) {
tasks.value[index].completed = (evt.target as HTMLInputElement).checked
}
function handleClearComplete() {
tasks.value = tasks.value.filter(task => !task.completed)
}
function handleClearAll() {
tasks.value = []
}
</script>

<template>
<h1>todos</h1>
<ul>
<!-- TODO: v-for -->
<li>
<!-- TODO checked=false -->
<input type="checkbox" :checked="tasks[0]?.completed" />
<li v-show="tasks[0]" :class="{ del: tasks[0]?.completed }">
<input
type="checkbox"
:checked="tasks[0]?.completed"
@change="handleComplete(0, $event)"
/>
{{ tasks[0]?.title }}
</li>
<li>
<input type="checkbox" :checked="tasks[1]?.completed" />
<li v-show="tasks[1]" :class="{ del: tasks[1]?.completed }">
<input
type="checkbox"
:checked="tasks[1]?.completed"
@change="handleComplete(1, $event)"
/>
{{ tasks[1]?.title }}
</li>
<li>
<input type="checkbox" :checked="tasks[2]?.completed" />
<li v-show="tasks[2]" :class="{ del: tasks[2]?.completed }">
<input
type="checkbox"
:checked="tasks[2]?.completed"
@change="handleComplete(2, $event)"
/>
{{ tasks[2]?.title }}
</li>
<li>
<input type="checkbox" :checked="tasks[3]?.completed" />
<li v-show="tasks[3]" :class="{ del: tasks[3]?.completed }">
<input
type="checkbox"
:checked="tasks[3]?.completed"
@change="handleComplete(3, $event)"
/>
{{ tasks[3]?.title }}
</li>
<li>
<input type="text" v-model="value" />
<button @click="handleAdd">Add</button>
</li>
</ul>
<p>
{{ remaining }} item{{ remaining !== 1 ? 's' : '' }} left /
{{ tasks.length }} item{{ tasks.length !== 1 ? 's' : '' }} in total
</p>
<div style="display: flex; gap: 8px">
<input
type="text"
v-model="value"
@keydown.enter="handleAdd"
placeholder="What need to be done?"
/>
<button @click="handleAdd">Add</button>
<button @click="handleClearComplete">Clear completed</button>
<button @click="handleClearAll">Clear all</button>
</div>
</template>

<style>
.del {
text-decoration: line-through;
}
</style>
6 changes: 6 additions & 0 deletions playground/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import * as CompilerVapor from '@vue/compiler-vapor'
import * as CompilerSFC from '@vue/compiler-sfc'

export default defineConfig({
resolve: {
alias: [{ find: /^vue$/, replacement: 'vue/vapor' }],
},
build: {
target: 'esnext',
},
Expand All @@ -20,4 +23,7 @@ export default defineConfig({
DevPlugin(),
Inspect(),
],
optimizeDeps: {
exclude: ['@vueuse/core'],
},
})
47 changes: 47 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2f9e25d

Please sign in to comment.