fix(36): polish ScopePicker search collapse, unique panel IDs, and lazy catalog expand

This commit is contained in:
Jakub Zych
2026-11-25 01:29:28 +01:00
parent 51b60b94b8
commit bfb0bee809
7 changed files with 158 additions and 25 deletions

View File

@@ -1,5 +1,5 @@
<script setup lang="ts">
import { computed, nextTick, onMounted, ref } from 'vue'
import { computed, nextTick, ref, useId, watch } from 'vue'
import { ChevronDown, ChevronRight, Lock, Search, X } from '@lucide/vue'
import type { AdminAPITokenScope } from '../../api/types'
import {
@@ -46,9 +46,12 @@ interface VisibleScopeGroup {
canClear: boolean
}
const pickerId = useId()
const query = ref('')
const filter = ref<ScopeFilter>('all')
const expanded = ref<Set<string>>(new Set())
const searchCollapsed = ref<Set<string>>(new Set())
let initializedExpanded = false
const searchInput = ref<HTMLInputElement | null>(null)
const partitioned = computed(() => partitionCatalog(props.catalog))
@@ -123,7 +126,7 @@ const visibleGroups = computed<VisibleScopeGroup[]>(() => {
result.push({
key: group.key,
panelId: `scope-group-panel-${index}`,
panelId: `${pickerId}-group-${index}`,
label: group.label,
allScopes: group.scopes,
visibleScopes,
@@ -182,11 +185,21 @@ const emptyState = computed(() => {
})
function isGroupExpanded(key: string): boolean {
return isSearching.value || expanded.value.has(key)
if (isSearching.value) {
return !searchCollapsed.value.has(key)
}
return expanded.value.has(key)
}
function toggleGroup(key: string): void {
if (isSearching.value) {
const next = new Set(searchCollapsed.value)
if (next.has(key)) {
next.delete(key)
} else {
next.add(key)
}
searchCollapsed.value = next
return
}
const next = new Set(expanded.value)
@@ -262,15 +275,28 @@ function focusSearch(): void {
searchInput.value?.focus()
}
onMounted(() => {
expanded.value = computeDefaultExpanded(
allGroups.value,
pluginScopes.value.length,
draftSelection.value,
false,
)
watch(query, () => {
if (searchCollapsed.value.size > 0) {
searchCollapsed.value = new Set()
}
})
watch(
() => allGroups.value.length,
(len) => {
if (!initializedExpanded && len > 0) {
initializedExpanded = true
expanded.value = computeDefaultExpanded(
allGroups.value,
pluginScopes.value.length,
draftSelection.value,
false,
)
}
},
{ immediate: true },
)
defineExpose({ focusSearch })
</script>

View File

@@ -306,4 +306,111 @@ describe('ScopePicker.vue', () => {
expect(wrapper.find('[data-scope-filter="selected"]').text()).toBe('Selected (1)')
expect((wrapper.find('[data-action="scope-picker-apply"]').element as HTMLButtonElement).disabled).toBe(false)
})
it('allows collapsing matching groups during search without mutating pre-search expand state (IN-02)', async () => {
const wrapper = mountInteractivePicker({ catalog: smallCatalog })
await flushPromises()
// Collapse SEO before searching (Journal stays expanded, SEO collapsed).
const togglesBefore = wrapper.findAll('[data-scope-group-toggle]')
await togglesBefore[1]!.trigger('click')
await flushPromises()
expect(wrapper.findAll('[data-scope-group-toggle]').map((b) => b.attributes('aria-expanded'))).toEqual([
'true',
'false',
])
// Search for "SEO" -> auto-expands SEO.
const search = wrapper.find('[data-scope-search]')
await search.setValue('seo')
await flushPromises()
expect(wrapper.find('[data-scope-group-toggle]').attributes('aria-expanded')).toBe('true')
// User can temporarily collapse the matching group during search.
await wrapper.find('[data-scope-group-toggle]').trigger('click')
await flushPromises()
expect(wrapper.find('[data-scope-group-toggle]').attributes('aria-expanded')).toBe('false')
// Changing the query re-expands matching groups; clearing restores exact pre-search state.
await search.setValue('seo:read')
await flushPromises()
expect(wrapper.find('[data-scope-group-toggle]').attributes('aria-expanded')).toBe('true')
await wrapper.find('[data-action="clear-scope-search"]').trigger('click')
await flushPromises()
expect(wrapper.findAll('[data-scope-group-toggle]').map((b) => b.attributes('aria-expanded'))).toEqual([
'true',
'false',
])
})
it('uses unique per-instance panel IDs and initializes smart expansion when catalog loads lazily (IN-03)', async () => {
const DualHarness = defineComponent({
components: { ScopePicker },
setup() {
const aRead = ref(false)
const aScopes = ref<Set<string>>(new Set())
const bRead = ref(false)
const bScopes = ref<Set<string>>(new Set())
return { catalog: smallCatalog, aRead, aScopes, bRead, bScopes }
},
template: `
<div>
<div data-first>
<ScopePicker
:catalog="catalog"
v-model:draft-admin-read="aRead"
v-model:draft-scopes="aScopes"
cancel-label="Cancel"
/>
</div>
<div data-second>
<ScopePicker
:catalog="catalog"
v-model:draft-admin-read="bRead"
v-model:draft-scopes="bScopes"
cancel-label="Cancel"
/>
</div>
</div>
`,
})
const dual = mount(DualHarness, { attachTo: document.body })
await flushPromises()
const firstPanelId = dual.find('[data-first] [data-scope-group-toggle]').attributes('aria-controls')
const secondPanelId = dual.find('[data-second] [data-scope-group-toggle]').attributes('aria-controls')
expect(firstPanelId).toBeTruthy()
expect(secondPanelId).toBeTruthy()
expect(firstPanelId).not.toBe(secondPanelId)
// Lazy catalog: starts empty, then populates with smallCatalog -> smart expand triggers automatically.
const lazyCatalog = ref<AdminAPITokenScope[]>([])
const LazyHarness = defineComponent({
components: { ScopePicker },
setup() {
const draftAdminRead = ref(false)
const draftScopes = ref<Set<string>>(new Set())
return { lazyCatalog, draftAdminRead, draftScopes }
},
template: `
<ScopePicker
:catalog="lazyCatalog"
v-model:draft-admin-read="draftAdminRead"
v-model:draft-scopes="draftScopes"
cancel-label="Cancel"
/>
`,
})
const lazy = mount(LazyHarness, { attachTo: document.body })
await flushPromises()
expect(lazy.findAll('[data-scope-group]')).toHaveLength(0)
lazyCatalog.value = smallCatalog
await flushPromises()
expect(lazy.findAll('[data-scope-group-toggle]').map((b) => b.attributes('aria-expanded'))).toEqual([
'true',
'true',
])
})
})