Skip to content

Commit 4e40e14

Browse files
fix(IME): process Android soft-keyboard composition live for filters (#2806)
* fix: process Android soft-keyboard composition live for filters #2665 deferred all input until compositionend, which froze Autocomplete, Combobox, Listbox, and DropdownMenu filtering on Android where Latin autocorrect keeps the current word in composition until commit. * fix: keep IME composition verdict sticky per session Romaji-mode Japanese IMEs compose Latin before converting to kana within a single composition session, so classifying each `compositionupdate` independently let the verdict flip back to live mid-session — the popup opened on partial romaji and stayed on "no results" through conversion. Track whether IME script has been seen and never downgrade the session back to live once it has.
1 parent 8a78f11 commit 4e40e14

10 files changed

Lines changed: 447 additions & 15 deletions

File tree

‎packages/core/src/Autocomplete/Autocomplete.test.ts‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { DOMWrapper, VueWrapper } from '@vue/test-utils'
22
import { mount } from '@vue/test-utils'
3-
import { beforeEach, describe, expect, it, vi } from 'vitest'
3+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
44
import { axe } from 'vitest-axe'
55
import { nextTick } from 'vue'
66
import { handleSubmit, sleep } from '@/test'
@@ -174,6 +174,60 @@ describe('given default Autocomplete', () => {
174174
const lastEmit = emitted.at(-1)?.[0]
175175
expect(lastEmit).toBe('香')
176176
})
177+
178+
it('should not filter during plain-text composition off Android (desktop Pinyin preedit)', async () => {
179+
await input.trigger('compositionstart')
180+
input.element.dispatchEvent(new CompositionEvent('compositionupdate', { data: 'xiang', bubbles: true }))
181+
input.element.value = 'xiang'
182+
await input.trigger('input')
183+
await nextTick()
184+
const content = wrapper.find('[role=listbox]')
185+
expect(content.attributes('data-empty')).toBeUndefined()
186+
})
187+
188+
describe('on Android soft keyboard', () => {
189+
beforeEach(() => {
190+
Object.defineProperty(window.navigator, 'userAgent', {
191+
value: 'Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Mobile Safari/537.36',
192+
configurable: true,
193+
})
194+
})
195+
196+
afterEach(() => {
197+
delete (window.navigator as { userAgent?: string }).userAgent
198+
})
199+
200+
it('should filter and update modelValue live during plain-text (autocorrect) composition', async () => {
201+
await input.trigger('compositionstart')
202+
input.element.dispatchEvent(new CompositionEvent('compositionupdate', { data: 'zzzzz', bubbles: true }))
203+
input.element.value = 'zzzzz'
204+
await input.trigger('input')
205+
await nextTick()
206+
207+
const content = wrapper.find('[role=listbox]')
208+
expect(content.attributes('data-empty')).toBeDefined()
209+
expect(wrapper.emitted('update:modelValue')?.at(-1)?.[0]).toBe('zzzzz')
210+
})
211+
212+
it('should not filter during CJK IME composition until compositionend', async () => {
213+
const emittedBefore = wrapper.emitted('update:modelValue')?.length ?? 0
214+
await input.trigger('compositionstart')
215+
input.element.dispatchEvent(new CompositionEvent('compositionupdate', { data: 'かんじ', bubbles: true }))
216+
input.element.value = 'かんじ'
217+
await input.trigger('input')
218+
await nextTick()
219+
220+
expect(wrapper.find('[role=listbox]').attributes('data-empty')).toBeUndefined()
221+
expect(wrapper.emitted('update:modelValue')?.length ?? 0).toBe(emittedBefore)
222+
223+
await input.trigger('compositionend')
224+
await nextTick()
225+
await nextTick()
226+
227+
expect(wrapper.find('[role=listbox]').attributes('data-empty')).toBeDefined()
228+
expect(wrapper.emitted('update:modelValue')?.at(-1)?.[0]).toBe('かんじ')
229+
})
230+
})
177231
})
178232

179233
describe('data-empty attribute on content', () => {

‎packages/core/src/Autocomplete/AutocompleteInput.vue‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ onMounted(() => {
3838
rootContext.onInputElementChange(currentElement.value as HTMLInputElement)
3939
})
4040
41-
const { isComposing, handleCompositionStart, handleCompositionEnd } = useComposing((event) => {
41+
const { isComposing, shouldDeferInput, handleCompositionStart, handleCompositionUpdate, handleCompositionEnd } = useComposing((event) => {
4242
const el = event.target as HTMLInputElement
4343
if (el)
4444
processInputValue(el.value)
@@ -71,7 +71,7 @@ function processInputValue(value: string) {
7171
}
7272
7373
function handleInput(event: InputEvent) {
74-
if (isComposing.value)
74+
if (shouldDeferInput.value)
7575
return
7676
processInputValue((event.target as HTMLInputElement).value)
7777
}
@@ -123,6 +123,7 @@ watch(rootContext.filterState, (_newValue, oldValue) => {
123123
@keydown.down.up="handleKeyDown"
124124
@focus="handleFocus"
125125
@compositionstart="handleCompositionStart"
126+
@compositionupdate="handleCompositionUpdate"
126127
@compositionend="handleCompositionEnd"
127128
>
128129
<slot />

‎packages/core/src/Combobox/Combobox.test.ts‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { DOMWrapper, VueWrapper } from '@vue/test-utils'
22
import { mount } from '@vue/test-utils'
3-
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
3+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
44
import { axe } from 'vitest-axe'
55
import { defineComponent, h, nextTick, ref } from 'vue'
66
import { handleSubmit, sleep } from '@/test'
@@ -685,6 +685,60 @@ describe('handle IME composition', () => {
685685
expect(content.exists()).toBe(true)
686686
expect(content.attributes('data-empty')).toBeDefined()
687687
})
688+
689+
it('should not update filter during plain-text composition off Android (desktop Pinyin preedit)', async () => {
690+
await input.trigger('compositionstart')
691+
input.element.dispatchEvent(new CompositionEvent('compositionupdate', { data: 'xiang', bubbles: true }))
692+
input.element.value = 'xiang'
693+
await input.trigger('input')
694+
await nextTick()
695+
696+
expect(wrapper.find('[role=listbox]').exists()).toBe(false)
697+
})
698+
699+
describe('on Android soft keyboard', () => {
700+
beforeEach(() => {
701+
Object.defineProperty(window.navigator, 'userAgent', {
702+
value: 'Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Mobile Safari/537.36',
703+
configurable: true,
704+
})
705+
})
706+
707+
afterEach(() => {
708+
delete (window.navigator as { userAgent?: string }).userAgent
709+
})
710+
711+
it('should update filter live during plain-text (autocorrect) composition', async () => {
712+
await input.trigger('compositionstart')
713+
input.element.dispatchEvent(new CompositionEvent('compositionupdate', { data: 'zzzzz', bubbles: true }))
714+
input.element.value = 'zzzzz'
715+
await input.trigger('input')
716+
await nextTick()
717+
await nextTick()
718+
719+
const content = wrapper.find('[role=listbox]')
720+
expect(content.exists()).toBe(true)
721+
expect(content.attributes('data-empty')).toBeDefined()
722+
})
723+
724+
it('should not update filter during CJK IME composition until compositionend', async () => {
725+
await input.trigger('compositionstart')
726+
input.element.dispatchEvent(new CompositionEvent('compositionupdate', { data: 'かんじ', bubbles: true }))
727+
input.element.value = 'かんじ'
728+
await input.trigger('input')
729+
await nextTick()
730+
731+
expect(wrapper.find('[role=listbox]').exists()).toBe(false)
732+
733+
await input.trigger('compositionend')
734+
await nextTick()
735+
await nextTick()
736+
737+
const content = wrapper.find('[role=listbox]')
738+
expect(content.exists()).toBe(true)
739+
expect(content.attributes('data-empty')).toBeDefined()
740+
})
741+
})
688742
})
689743

690744
describe('given combobox handleBlur with deferred close', () => {

‎packages/core/src/Combobox/ComboboxInput.vue‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ onMounted(() => {
3535
rootContext.onInputElementChange(currentElement.value as HTMLInputElement)
3636
})
3737
38-
const { isComposing, handleCompositionStart, handleCompositionEnd } = useComposing((event) => {
38+
const { isComposing, shouldDeferInput, handleCompositionStart, handleCompositionUpdate, handleCompositionEnd } = useComposing((event) => {
3939
const el = event.target as HTMLInputElement
4040
if (el)
4141
processInputValue(el.value)
@@ -66,7 +66,7 @@ function processInputValue(value: string) {
6666
}
6767
6868
function handleInput(event: InputEvent) {
69-
if (isComposing.value)
69+
if (shouldDeferInput.value)
7070
return
7171
processInputValue((event.target as HTMLInputElement).value)
7272
}
@@ -170,6 +170,7 @@ watch(rootContext.filterState, (_newValue, oldValue) => {
170170
@focus="handleFocus"
171171
@blur="handleBlur"
172172
@compositionstart="handleCompositionStart"
173+
@compositionupdate="handleCompositionUpdate"
173174
@compositionend="handleCompositionEnd"
174175
>
175176
<slot />

‎packages/core/src/DropdownMenu/DropdownMenuFilter.test.ts‎

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { VueWrapper } from '@vue/test-utils'
22
import { mount } from '@vue/test-utils'
3-
import { beforeEach, describe, expect, it } from 'vitest'
3+
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
44
import { nextTick } from 'vue'
55
import DropdownMenuWithFilter from './story/_DropdownMenuWithFilter.vue'
66

@@ -179,6 +179,68 @@ describe('given DropdownMenu with Filter', () => {
179179
expect(items.length).toBe(0)
180180
})
181181

182+
it('should not update search during plain-text composition off Android (desktop Pinyin preedit)', async () => {
183+
const filterInput = document.querySelector('[role="searchbox"]') as HTMLInputElement
184+
const baseline = document.querySelectorAll('[role="menuitem"]').length
185+
186+
filterInput.dispatchEvent(new CompositionEvent('compositionstart', { bubbles: true }))
187+
filterInput.dispatchEvent(new CompositionEvent('compositionupdate', { data: 'xiang', bubbles: true }))
188+
filterInput.value = 'xiang'
189+
filterInput.dispatchEvent(new Event('input', { bubbles: true }))
190+
await nextTick()
191+
192+
expect(wrapper.vm.filterText).toBe('')
193+
expect(document.querySelectorAll('[role="menuitem"]').length).toBe(baseline)
194+
})
195+
196+
describe('on Android soft keyboard', () => {
197+
beforeEach(() => {
198+
Object.defineProperty(window.navigator, 'userAgent', {
199+
value: 'Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Mobile Safari/537.36',
200+
configurable: true,
201+
})
202+
})
203+
204+
afterEach(() => {
205+
delete (window.navigator as { userAgent?: string }).userAgent
206+
})
207+
208+
it('should update search live during plain-text (autocorrect) composition', async () => {
209+
const filterInput = document.querySelector('[role="searchbox"]') as HTMLInputElement
210+
211+
filterInput.dispatchEvent(new CompositionEvent('compositionstart', { bubbles: true }))
212+
filterInput.dispatchEvent(new CompositionEvent('compositionupdate', { data: 'New', bubbles: true }))
213+
filterInput.value = 'New'
214+
filterInput.dispatchEvent(new Event('input', { bubbles: true }))
215+
await nextTick()
216+
217+
expect(wrapper.vm.filterText).toBe('New')
218+
const items = document.querySelectorAll('[role="menuitem"]')
219+
expect(items.length).toBe(2)
220+
})
221+
222+
it('should not update search during CJK IME composition until compositionend', async () => {
223+
const filterInput = document.querySelector('[role="searchbox"]') as HTMLInputElement
224+
const baseline = document.querySelectorAll('[role="menuitem"]').length
225+
226+
filterInput.dispatchEvent(new CompositionEvent('compositionstart', { bubbles: true }))
227+
filterInput.dispatchEvent(new CompositionEvent('compositionupdate', { data: 'かんじ', bubbles: true }))
228+
filterInput.value = 'かんじ'
229+
filterInput.dispatchEvent(new Event('input', { bubbles: true }))
230+
await nextTick()
231+
232+
expect(wrapper.vm.filterText).toBe('')
233+
expect(document.querySelectorAll('[role="menuitem"]').length).toBe(baseline)
234+
235+
filterInput.dispatchEvent(new CompositionEvent('compositionend', { data: 'かんじ', bubbles: true }))
236+
await nextTick()
237+
await nextTick()
238+
239+
expect(wrapper.vm.filterText).toBe('かんじ')
240+
expect(document.querySelectorAll('[role="menuitem"]').length).toBe(0)
241+
})
242+
})
243+
182244
it('should not navigate items during IME composition (arrow keys are IME candidate navigation)', async () => {
183245
const filterInput = document.querySelector('[role="searchbox"]') as HTMLInputElement
184246
filterInput.focus()

‎packages/core/src/DropdownMenu/DropdownMenuFilter.vue‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ onUnmounted(() => {
7171
contentContext.searchRef.value = ''
7272
})
7373
74-
const { isComposing, handleCompositionStart, handleCompositionEnd } = useComposing((event) => {
74+
const { isComposing, shouldDeferInput, handleCompositionStart, handleCompositionUpdate, handleCompositionEnd } = useComposing((event) => {
7575
const el = event.target as HTMLInputElement
7676
if (el) {
7777
modelValue.value = el.value
@@ -82,7 +82,7 @@ const { isComposing, handleCompositionStart, handleCompositionEnd } = useComposi
8282
function handleInput(event: InputEvent) {
8383
if (disabled.value)
8484
return
85-
if (isComposing.value)
85+
if (shouldDeferInput.value)
8686
return
8787
const target = event.target as HTMLInputElement
8888
modelValue.value = target.value
@@ -131,6 +131,7 @@ function handleKeyDown(event: KeyboardEvent) {
131131
@input="handleInput"
132132
@keydown="handleKeyDown"
133133
@compositionstart="handleCompositionStart"
134+
@compositionupdate="handleCompositionUpdate"
134135
@compositionend="handleCompositionEnd"
135136
>
136137
<slot :model-value="modelValue" />

‎packages/core/src/Listbox/Listbox.test.ts‎

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { DOMWrapper, VueWrapper } from '@vue/test-utils'
22
import { mount } from '@vue/test-utils'
3-
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
3+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
44
import { axe } from 'vitest-axe'
55
import { defineComponent, h, nextTick, ref } from 'vue'
66
import { useKbd } from '@/shared'
77
import { handleSubmit } from '@/test'
8-
import { ListboxContent, ListboxItem, ListboxRoot, ListboxVirtualizer } from '.'
8+
import { ListboxContent, ListboxFilter, ListboxItem, ListboxRoot, ListboxVirtualizer } from '.'
99
import Listbox from './story/_Listbox.vue'
1010

1111
describe('given default Listbox', () => {
@@ -538,3 +538,78 @@ describe('given Listbox in a form', async () => {
538538
})
539539
})
540540
})
541+
542+
describe('given Listbox with ListboxFilter handling IME composition', () => {
543+
let wrapper: VueWrapper
544+
let input: DOMWrapper<HTMLInputElement>
545+
let updates: string[]
546+
547+
window.HTMLElement.prototype.scrollIntoView = vi.fn()
548+
549+
const ANDROID_UA = 'Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Mobile Safari/537.36'
550+
551+
beforeEach(() => {
552+
document.body.innerHTML = ''
553+
updates = []
554+
const search = ref('')
555+
wrapper = mount(defineComponent({
556+
setup() {
557+
return () => h(ListboxRoot, {}, {
558+
default: () => [
559+
h(ListboxFilter, {
560+
'modelValue': search.value,
561+
'onUpdate:modelValue': (v: string) => {
562+
updates.push(v)
563+
search.value = v
564+
},
565+
}),
566+
h(ListboxContent, {}, {
567+
default: () => ['Apple', 'Banana'].map(i => h(ListboxItem, { value: i }, { default: () => i })),
568+
}),
569+
],
570+
})
571+
},
572+
}), { attachTo: document.body })
573+
input = wrapper.find('input')
574+
})
575+
576+
afterEach(() => {
577+
delete (window.navigator as { userAgent?: string }).userAgent
578+
})
579+
580+
it('should not update modelValue during plain-text composition off Android (desktop Pinyin preedit)', async () => {
581+
await input.trigger('compositionstart')
582+
input.element.dispatchEvent(new CompositionEvent('compositionupdate', { data: 'xiang', bubbles: true }))
583+
input.element.value = 'xiang'
584+
await input.trigger('input')
585+
586+
expect(updates).toEqual([])
587+
})
588+
589+
it('should update modelValue live during plain-text (autocorrect) composition on Android', async () => {
590+
Object.defineProperty(window.navigator, 'userAgent', { value: ANDROID_UA, configurable: true })
591+
592+
await input.trigger('compositionstart')
593+
input.element.dispatchEvent(new CompositionEvent('compositionupdate', { data: 'Br', bubbles: true }))
594+
input.element.value = 'Br'
595+
await input.trigger('input')
596+
597+
expect(updates).toEqual(['Br'])
598+
})
599+
600+
it('should not update modelValue during CJK IME composition on Android until compositionend', async () => {
601+
Object.defineProperty(window.navigator, 'userAgent', { value: ANDROID_UA, configurable: true })
602+
603+
await input.trigger('compositionstart')
604+
input.element.dispatchEvent(new CompositionEvent('compositionupdate', { data: 'かんじ', bubbles: true }))
605+
input.element.value = 'かんじ'
606+
await input.trigger('input')
607+
608+
expect(updates).toEqual([])
609+
610+
await input.trigger('compositionend')
611+
await nextTick()
612+
613+
expect(updates).toEqual(['かんじ'])
614+
})
615+
})

0 commit comments

Comments
 (0)