Skip to content
Closed
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
18 changes: 14 additions & 4 deletions frontend/taskdeck-web/src/components/common/InputAssistField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const activeDescendant = computed(() => {
return `${componentId}-option-${activeIndex.value}`
})

watch(filteredOptions, (options) => {
watch(filteredOptions, (options, previousOptions) => {
if (options.length === 0) {
activeIndex.value = 0
return
Expand All @@ -46,6 +46,16 @@ watch(filteredOptions, (options) => {
if (activeIndex.value >= options.length) {
activeIndex.value = 0
}

if (!panelOpen.value) {
return
}

const exactMatch = findExactMatch(props.modelValue)
const previousExactMatch = findExactMatch(props.modelValue, previousOptions)
if (exactMatch && !previousExactMatch) {
selectOption(exactMatch)
}
})

function openPanel() {
Expand All @@ -65,18 +75,18 @@ function setModelValue(value: string) {
emit('update:modelValue', value)
}

function findExactMatch(value: string): InputAssistOption | null {
function findExactMatch(value: string, options: InputAssistOption[] = props.options): InputAssistOption | null {
const normalizedInput = value.trim().toLowerCase()
if (!normalizedInput) {
return null
}

const byValue = props.options.find((option) => option.value.trim().toLowerCase() === normalizedInput)
const byValue = options.find((option) => option.value.trim().toLowerCase() === normalizedInput)
if (byValue) {
return byValue
}

return props.options.find((option) => {
return options.find((option) => {
return option.label.trim().toLowerCase() === normalizedInput
})
?? null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,26 @@ describe('InputAssistField', () => {
expect(wrapper.emitted('update:modelValue')?.at(-1)).toEqual(['health.check'])
expect(wrapper.emitted('select')?.at(-1)?.[0]).toMatchObject({ value: 'health.check', label: 'Health Check' })
})

it('selects an exact value when matching options arrive after input', async () => {
const wrapper = mount(InputAssistField, {
props: {
modelValue: '',
options: [],
},
})

const input = wrapper.get('input')
await input.trigger('focus')
await input.setValue('health.check')

expect(wrapper.find('[role="listbox"]').exists()).toBe(true)
expect(wrapper.findAll('[role="option"]')).toHaveLength(0)

await wrapper.setProps({ options })

expect(wrapper.find('[role="listbox"]').exists()).toBe(false)
expect(wrapper.emitted('update:modelValue')?.at(-1)).toEqual(['health.check'])
expect(wrapper.emitted('select')?.at(-1)?.[0]).toMatchObject({ value: 'health.check', label: 'Health Check' })
})
})
Loading