Skip to content

Instantly share code, notes, and snippets.

@JeffreyWay
Last active May 12, 2022 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffreyWay/3438d35d8d501c2c34475749087a03ac to your computer and use it in GitHub Desktop.
Save JeffreyWay/3438d35d8d501c2c34475749087a03ac to your computer and use it in GitHub Desktop.
Learn Vue 3: Step By Step, Episode 12 - A Deeper Look at V-Model - https://laracasts.com/series/learn-vue-3-step-by-step/episodes/12
import Assignment from "./Assignment.js";
import AssignmentTags from "./AssignmentTags.js";
export default {
components: { Assignment, AssignmentTags },
template: `
<section v-show="assignments.length">
<h2 class="font-bold mb-2">
{{ title }}
<span>({{ assignments.length }})</span>
</h2>
<assignment-tags
v-model:currentTag="currentTag"
:initial-tags="assignments.map(a => a.tag)"
></assignment-tags>
<ul class="border border-gray-600 divide-y divide-gray-600 mt-6">
<assignment
v-for="assignment in filteredAssignments"
:key="assignment.id"
:assignment="assignment"
></assignment>
</ul>
</section>
`,
props: {
assignments: Array,
title: String
},
data() {
return {
currentTag: 'all'
};
},
computed: {
filteredAssignments() {
if (this.currentTag === 'all') {
return this.assignments;
}
return this.assignments.filter(a => a.tag === this.currentTag);
}
}
}
export default {
template: `
<div class="flex gap-2">
<button
@click="$emit('update:currentTag', tag)"
v-for="tag in tags"
class="border rounded px-1 py-px text-xs"
:class="{
'border-blue-500 text-blue-500': tag === currentTag
}"
>{{ tag }}</button>
</div>
`,
props: {
initialTags: Array,
currentTag: String
},
computed: {
tags() {
return ['all', ...new Set(this.initialTags)];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment