TagBar.vue 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <template>
  2. <!-- 单选标签选择按钮条,可显示一行标签,然后高亮选中项 -->
  3. <ScrollRect scroll="horizontal">
  4. <div class="tags">
  5. <div
  6. :class="[
  7. 'tag-button',
  8. { 'active': tag.id === selectedTag },
  9. ]"
  10. v-for="tag in tags"
  11. :key="tag.id"
  12. @click="emit('update:selectedTag', tag.id ?? tag)"
  13. >
  14. {{ tag.name?? tag }}
  15. </div>
  16. </div>
  17. </ScrollRect>
  18. </template>
  19. <script setup lang="ts">
  20. import type { PropType } from 'vue';
  21. import { ScrollRect } from '@imengyu/vue-scroll-rect';
  22. defineProps({
  23. /**
  24. * 标签列表
  25. */
  26. tags: {
  27. type: Object as PropType<Array<{
  28. id: number|string,
  29. name: string,
  30. }>>,
  31. required: true,
  32. },
  33. /**
  34. * 选中的标签,可双向绑定
  35. */
  36. selectedTag: {
  37. type: [Number,String],
  38. default: null,
  39. }
  40. })
  41. const emit = defineEmits([
  42. "update:selectedTag"
  43. ])
  44. </script>
  45. <style scoped lang="scss">
  46. .tags {
  47. display: flex;
  48. flex-direction: row;
  49. align-items: center;
  50. flex-wrap: nowrap;
  51. overflow-x: auto;
  52. margin-bottom: 10px;
  53. min-width: 1000px;
  54. }
  55. .tag-button {
  56. display: flex;
  57. flex-direction: row;
  58. align-items: center;
  59. background-color: var(--color-box-inset);
  60. color: var(--color-text);
  61. padding: 10px;
  62. margin-right: 4px;
  63. cursor: pointer;
  64. user-select: none;
  65. &:hover {
  66. background-color: var(--color-box-hover);
  67. }
  68. &:active, &.active {
  69. color: var(--color-text-light);
  70. background-color: var(--color-primary);
  71. }
  72. }
  73. </style>