You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
2.4 KiB

3 years ago
3 years ago
  1. <template>
  2. <v-combobox
  3. v-model="annotatedLabels"
  4. chips
  5. :items="labels"
  6. item-text="text"
  7. hide-details
  8. hide-selected
  9. multiple
  10. class="pt-0"
  11. :search-input.sync="search"
  12. @change="search=''"
  13. >
  14. <template #selection="{ attrs, item, select, selected }">
  15. <v-chip
  16. v-bind="attrs"
  17. :input-value="selected"
  18. :color="item.backgroundColor"
  19. :text-color="$contrastColor(item.backgroundColor)"
  20. close
  21. @click="select"
  22. @click:close="remove(item)"
  23. >
  24. <v-avatar
  25. v-if="item.suffixKey"
  26. left
  27. color="white"
  28. class="black--text font-weight-bold"
  29. >
  30. {{ item.suffixKey }}
  31. </v-avatar>
  32. {{ item.text }}
  33. </v-chip>
  34. </template>
  35. <template #item="{ item }">
  36. <v-chip
  37. :color="item.backgroundColor"
  38. :text-color="$contrastColor(item.backgroundColor)"
  39. >
  40. <v-avatar
  41. v-if="item.suffixKey"
  42. left
  43. color="white"
  44. class="black--text font-weight-bold"
  45. >
  46. {{ item.suffixKey }}
  47. </v-avatar>
  48. {{ item.text }}
  49. </v-chip>
  50. </template>
  51. </v-combobox>
  52. </template>
  53. <script>
  54. export default {
  55. props: {
  56. labels: {
  57. type: Array,
  58. default: () => [],
  59. required: true
  60. },
  61. annotations: {
  62. type: Array,
  63. default: () => ([]),
  64. required: true
  65. }
  66. },
  67. data() {
  68. return {
  69. search: ''
  70. }
  71. },
  72. computed: {
  73. annotatedLabels: {
  74. get() {
  75. const labelIds = this.annotations.map(item => item.label)
  76. return this.labels.filter(item => labelIds.includes(item.id))
  77. },
  78. set(newValue) {
  79. if (newValue.length > this.annotations.length) {
  80. const label = newValue[newValue.length - 1]
  81. if (typeof label === 'object') {
  82. this.add(label)
  83. } else {
  84. newValue.pop()
  85. }
  86. } else {
  87. const label = this.annotatedLabels.find(x => !newValue.some(y => y.id === x.id))
  88. if (typeof label === 'object') {
  89. this.remove(label)
  90. }
  91. }
  92. }
  93. }
  94. },
  95. methods: {
  96. add(label) {
  97. this.$emit('add', label.id)
  98. },
  99. remove(label) {
  100. const annotation = this.annotations.find(item => item.label === label.id)
  101. this.$emit('remove', annotation.id)
  102. }
  103. }
  104. }
  105. </script>