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.

108 lines
2.3 KiB

  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 v-slot: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. left
  26. color="white"
  27. class="black--text font-weight-bold"
  28. >
  29. {{ item.suffixKey }}
  30. </v-avatar>
  31. {{ item.text }}
  32. </v-chip>
  33. </template>
  34. <template v-slot:item="{ item }">
  35. <v-chip
  36. :color="item.backgroundColor"
  37. :text-color="$contrastColor(item.backgroundColor)"
  38. >
  39. <v-avatar
  40. left
  41. color="white"
  42. class="black--text font-weight-bold"
  43. >
  44. {{ item.suffixKey }}
  45. </v-avatar>
  46. {{ item.text }}
  47. </v-chip>
  48. </template>
  49. </v-combobox>
  50. </template>
  51. <script>
  52. export default {
  53. props: {
  54. labels: {
  55. type: Array,
  56. default: () => [],
  57. required: true
  58. },
  59. annotations: {
  60. type: Array,
  61. default: () => ([]),
  62. required: true
  63. }
  64. },
  65. data() {
  66. return {
  67. search: ''
  68. }
  69. },
  70. computed: {
  71. annotatedLabels: {
  72. get() {
  73. const labelIds = this.annotations.map(item => item.label)
  74. return this.labels.filter(item => labelIds.includes(item.id))
  75. },
  76. set(newValue) {
  77. if (newValue.length > this.annotations.length) {
  78. const label = newValue[newValue.length - 1]
  79. if (typeof label === 'object') {
  80. this.add(label)
  81. } else {
  82. newValue.pop()
  83. }
  84. } else {
  85. const label = this.annotatedLabels.find(x => !newValue.some(y => y.id === x.id))
  86. if (typeof label === 'object') {
  87. this.remove(label)
  88. }
  89. }
  90. }
  91. }
  92. },
  93. methods: {
  94. add(label) {
  95. this.$emit('add', label.id)
  96. },
  97. remove(label) {
  98. const annotation = this.annotations.find(item => item.label === label.id)
  99. this.$emit('remove', annotation.id)
  100. }
  101. }
  102. }
  103. </script>