element-plus/packages/hooks/use-composition/index.ts
qiang 233d38b631
refactor(hooks): rewrite composition as a composable function (#14240)
* refactor(hooks): rewrite composition as a composable function

* fix(components): [select] avoid navigateOptions when composing Chinese

* fix: error

* chore: change afterComposition
2024-07-28 21:11:20 +08:00

51 lines
1.4 KiB
TypeScript

import { nextTick, ref } from 'vue'
import { isKorean } from '@element-plus/utils'
interface UseCompositionOptions {
afterComposition: (event: CompositionEvent) => void
emit?: ((event: 'compositionstart', evt: CompositionEvent) => void) &
((event: 'compositionupdate', evt: CompositionEvent) => void) &
((event: 'compositionend', evt: CompositionEvent) => void)
}
export function useComposition({
afterComposition,
emit,
}: UseCompositionOptions) {
const isComposing = ref(false)
const handleCompositionStart = (event: CompositionEvent) => {
emit?.('compositionstart', event)
isComposing.value = true
}
const handleCompositionUpdate = (event: CompositionEvent) => {
emit?.('compositionupdate', event)
const text = (event.target as HTMLInputElement)?.value
const lastCharacter = text[text.length - 1] || ''
isComposing.value = !isKorean(lastCharacter)
}
const handleCompositionEnd = (event: CompositionEvent) => {
emit?.('compositionend', event)
if (isComposing.value) {
isComposing.value = false
nextTick(() => afterComposition(event))
}
}
const handleComposition = (event: CompositionEvent) => {
event.type === 'compositionend'
? handleCompositionEnd(event)
: handleCompositionUpdate(event)
}
return {
isComposing,
handleComposition,
handleCompositionStart,
handleCompositionUpdate,
handleCompositionEnd,
}
}