Skip to content
Open
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
32 changes: 32 additions & 0 deletions app/javascript/controllers/multi_selection_combobox_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default class extends Controller {

connect() {
this.refresh()
this.#reorderItems()
}

change(event) {
Expand Down Expand Up @@ -69,6 +70,7 @@ export default class extends Controller {
this.#renameHiddenFields(item.dataset.multiSelectionFieldName)
}
this.labelTarget.textContent = this.#selectedLabel
this.#reorderItems()
}

isAnExclusiveSelectionItemInvolved(item) {
Expand Down Expand Up @@ -130,4 +132,34 @@ export default class extends Controller {
const hasSelection = this.#selectedValues().length > 0
this.element.setAttribute("data-filter-show", hasSelection)
}

#reorderItems() {
const list = this.element.querySelector("[role='listbox']")
if (!list) return

const items = Array.from(this.itemTargets)
const exclusiveItems = items.filter(item => this.#isExclusiveSelection(item))
const regularItems = items.filter(item => !this.#isExclusiveSelection(item))

const selectedItems = regularItems.filter(item =>
item.getAttribute(this.selectPropertyNameValue) === "true"
)
const unselectedItems = regularItems.filter(item =>
item.getAttribute(this.selectPropertyNameValue) !== "true"
)

const sortByLabel = (a, b) => {
const labelA = (a.dataset.multiSelectionComboboxLabel || "").toLowerCase()
const labelB = (b.dataset.multiSelectionComboboxLabel || "").toLowerCase()
return labelA.localeCompare(labelB)
}

selectedItems.sort(sortByLabel)
unselectedItems.sort(sortByLabel)

// Reorder: exclusive items first, then selected, then unselected
;[...exclusiveItems, ...selectedItems, ...unselectedItems].forEach(item => {
list.appendChild(item)
})
}
}