commit 6eb321f4f5e9607c60000e60d6e528efaff69a19
parent 271d3a279f8f570847365c4bd22f4be6c7a35fd4
Author: Michael Camilleri <[email protected]>
Date: Thu, 22 Oct 2026 14:53:15 +0900
Hide selected answers from multi-select token lists
When a question takes more than one answer, a picked answer showed both
as a chip above the list and, with a checkmark, in the list itself. This
meant selected items were in the way of other items.
This commit leaves selected entries out of the list for questions
that allow multiple answers. Removing an answer's chip returns it to
the list in its ranked position. Questions with a single answer still
list the selected entry with a checkmark.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat
| M | Reportage/Views/Survey/SurveyModel.swift | | | 12 | ++++++++---- |
| M | Tests/Unit/SurveyModelTests.swift | | | 14 | +++++++++++++- |
2 files changed, 21 insertions(+), 5 deletions(-)
diff --git a/Reportage/Views/Survey/SurveyModel.swift b/Reportage/Views/Survey/SurveyModel.swift
@@ -344,13 +344,17 @@ final class TokenPicker {
didSet { updateVisibleEntries() }
}
- /// The entries matching `query`, in rank order.
+ /// The entries matching `query`, in rank order. With multiple selection,
+ /// selected entries are left out, as they show above the list.
private(set) var visibleEntries: [VocabularyEntry] = []
/// Whether `query` names no existing entry, so it can be added.
private(set) var canAddQuery = false
/// Selected token IDs, in the order they were chosen.
private(set) var selection: [String] = [] {
- didSet { updateSelectedEntries() }
+ didSet {
+ updateVisibleEntries()
+ updateSelectedEntries()
+ }
}
private(set) var selectedEntries: [VocabularyEntry] = []
@@ -436,8 +440,8 @@ final class TokenPicker {
private func updateVisibleEntries() {
let text = query.trimmingCharacters(in: .whitespacesAndNewlines)
- visibleEntries =
- text.isEmpty ? entries : entries.filter { $0.matches(text) }
+ let matching = text.isEmpty ? entries : entries.filter { $0.matches(text) }
+ visibleEntries = allowsMultiple ? matching.filter { !selection.contains($0.id) } : matching
canAddQuery = !text.isEmpty && entry(named: text) == nil
}
diff --git a/Tests/Unit/SurveyModelTests.swift b/Tests/Unit/SurveyModelTests.swift
@@ -339,13 +339,25 @@ struct TokenPickerTests {
@Test("A new token is listed first and selected")
func addNew() {
- let picker = makePicker()
+ let picker = makePicker(allowsMultiple: false)
picker.query = "Walking"
picker.addQuery()
#expect(picker.visibleEntries.first?.text == "Walking")
#expect(picker.selectedEntries.map(\.text) == ["Walking"])
}
+ @Test("Multiple selection leaves selected entries out of the list")
+ func multipleSelectionHidesSelected() {
+ let picker = makePicker()
+ picker.toggle("2")
+ #expect(picker.visibleEntries.map(\.id) == ["1", "3"])
+ picker.query = "Walking"
+ picker.addQuery()
+ #expect(picker.visibleEntries.map(\.id) == ["1", "3"])
+ picker.toggle("2")
+ #expect(picker.visibleEntries.map(\.id) == ["1", "2", "3"])
+ }
+
@Test("Single selection replaces the selected entry")
func singleSelection() {
let picker = makePicker(allowsMultiple: false)