Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable3.3] Fix category selection #5322

Merged
merged 2 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 61 additions & 8 deletions src/components/Editor/Properties/PropertySelectMultiple.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@
<div class="property-select-multiple__input"
:class="{ 'property-select-multiple__input--readonly': isReadOnly }">
<Multiselect v-if="!isReadOnly"
v-model="selectionData"
:options="options"
:searchable="true"
:placeholder="placeholder"
:tag-placeholder="tagPlaceholder"
:allow-empty="true"
:title="readableName"
:value="value"
:multiple="true"
:taggable="true"
track-by="value"
track-by="label"
label="label"
@select="selectValue"
@tag="selectValue"
@tag="tag"
@remove="unselectValue">
<template v-if="coloredOptions" #tag="scope">
<PropertySelectMultipleColoredTag v-bind="scope" />
Expand Down Expand Up @@ -100,24 +100,43 @@ export default {
default: false,
},
},
data() {
return {
selectionData: [],
}
},
computed: {
display() {
return !(this.isReadOnly && this.value.length === 0)
return !(this.isReadOnly && this.selectionData.length === 0)
},
options() {
const options = this.propModel.options.slice()
for (const value of (this.value ?? [])) {
if (options.find(option => option.value === value)) {
for (const category of (this.selectionData ?? [])) {
if (options.find(option => option.value === category.value)) {
continue
}

// Add pseudo options for unknown values
options.push({
value,
label: value,
value: category.value,
label: category.label,
})
}

for (const category of this.value) {
if (!options.find(option => option.value === category)) {
options.push({ value: category, label: category })
}
}

if (this.customLabelBuffer) {
for (const category of this.customLabelBuffer) {
if (!options.find(option => option.value === category.value)) {
options.push(category)
}
}
}

return options
.sort((a, b) => {
return a.label.localeCompare(
Expand All @@ -128,6 +147,23 @@ export default {
})
},
},
async mounted() {
// Dirty hack but this forces a recaculation of collapsed categories. The resize event has
// to be dispatched multiple times for tag widths to be calculated correctly.
window.dispatchEvent(new Event('resize'))
await this.$nextTick()
window.dispatchEvent(new Event('resize'))
await this.$nextTick()
window.dispatchEvent(new Event('resize'))
},
created() {
for (const category of this.value) {
const option = this.options.find(option => option.value === category)
if (option) {
this.selectionData.push(option)
}
}
},
methods: {
selectValue(value) {
if (!value) {
Expand All @@ -142,6 +178,23 @@ export default {
}

this.$emit('remove-single-value', value.value)

// store removed custom options to keep it in the option list
const options = this.propModel.options.slice()
if (!options.find(option => option.value === value.value)) {
if (!this.customLabelBuffer) {
this.customLabelBuffer = []
}
this.customLabelBuffer.push(value)
}
},
tag(value) {
if (!value) {
return
}

this.selectionData.push({ value, label: value })
this.$emit('add-single-value', value)
},
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<template>
<span class="multiselect__tag"
:style="{ 'background-color': color, 'border-color': borderColor, color: textColor }">
<span>{{ option }}</span>
<span>{{ label }}</span>
</span>
</template>

Expand All @@ -36,7 +36,7 @@ export default {
name: 'PropertySelectMultipleColoredTag',
props: {
option: {
type: String,
type: [String, Object],
required: true,
},
search: {
Expand All @@ -49,8 +49,17 @@ export default {
},
},
computed: {
label() {
if (typeof this.option === 'string') {
return this.option
}
return this.option.label
},
colorObject() {
return uidToColor(this.option)
if (typeof this.option === 'string') {
return uidToColor(this.option)
}
return uidToColor(this.option.label)
},
borderColor() {
const color = this.colorObject
Expand Down