Skip to content
Merged
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
14 changes: 13 additions & 1 deletion packages/@headlessui-react/src/components/combobox/combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,16 @@ let Input = forwardRefWithAs(function Input<
[currentValue, data.comboboxState]
)

let isComposing = useRef(false)
let handleCompositionStart = useEvent(() => {
isComposing.current = true
})
let handleCompositionEnd = useEvent(() => {
setTimeout(() => {
isComposing.current = false
})
})

let handleKeyDown = useEvent((event: ReactKeyboardEvent<HTMLInputElement>) => {
switch (event.key) {
// Ref: https://www.w3.org/TR/wai-aria-practices-1.2/#keyboard-interaction-12
Expand All @@ -713,7 +723,7 @@ let Input = forwardRefWithAs(function Input<

case Keys.Enter:
if (data.comboboxState !== ComboboxState.Open) return
if (event.nativeEvent.isComposing) return
if (isComposing.current) return

event.preventDefault()
event.stopPropagation()
Expand Down Expand Up @@ -815,6 +825,8 @@ let Input = forwardRefWithAs(function Input<
'aria-multiselectable': data.mode === ValueMode.Multi ? true : undefined,
'aria-labelledby': labelledby,
disabled: data.disabled,
onCompositionStart: handleCompositionStart,
onCompositionEnd: handleCompositionEnd,
onKeyDown: handleKeyDown,
onChange: handleChange,
}
Expand Down
16 changes: 14 additions & 2 deletions packages/@headlessui-vue/src/components/combobox/combobox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ export let ComboboxInput = defineComponent({
}

// Workaround Vue bug where watching [ref(undefined)] is not fired immediately even when value is true
const __fixVueImmediateWatchBug__ = ref('')
let __fixVueImmediateWatchBug__ = ref('')

onMounted(() => {
watch(
Expand Down Expand Up @@ -659,6 +659,16 @@ export let ComboboxInput = defineComponent({
)
})

let isComposing = ref(false)
function handleCompositionstart() {
isComposing.value = true
}
function handleCompositionend() {
setTimeout(() => {
isComposing.value = false
})
}

function handleKeyDown(event: KeyboardEvent) {
switch (event.key) {
// Ref: https://www.w3.org/TR/wai-aria-practices-1.2/#keyboard-interaction-12
Expand All @@ -683,7 +693,7 @@ export let ComboboxInput = defineComponent({

case Keys.Enter:
if (api.comboboxState.value !== ComboboxStates.Open) return
if (event.isComposing) return
if (isComposing.value) return

event.preventDefault()
event.stopPropagation()
Expand Down Expand Up @@ -774,6 +784,8 @@ export let ComboboxInput = defineComponent({
'aria-multiselectable': api.mode.value === ValueMode.Multi ? true : undefined,
'aria-labelledby': dom(api.labelRef)?.id ?? dom(api.buttonRef)?.id,
id,
onCompositionstart: handleCompositionstart,
onCompositionend: handleCompositionend,
onKeydown: handleKeyDown,
onChange: handleChange,
onInput: handleInput,
Expand Down