-
Notifications
You must be signed in to change notification settings - Fork 289
/
Copy pathuse-textarea-event.ts
55 lines (46 loc) · 1.69 KB
/
use-textarea-event.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { inject, Ref, SetupContext, ref } from 'vue';
import { FormItemContext, FORM_ITEM_TOKEN } from '../../../form';
import { TextareaProps, UseTextareaEvent } from '../textarea-types';
export function useTextareaEvent(isFocus: Ref<boolean>, props: TextareaProps, ctx: SetupContext) {
const formItemContext = inject(FORM_ITEM_TOKEN, undefined) as FormItemContext;
const isComposition = ref(false);
const onFocus = (e: FocusEvent) => {
isFocus.value = true;
ctx.emit('focus', e);
};
const onBlur = (e: FocusEvent) => {
isFocus.value = false;
ctx.emit('blur', e);
if (props.validateEvent) {
formItemContext?.validate('blur').catch((err) => console.warn(err));
}
};
const onInput = (e: Event) => {
if (isComposition.value) {
return;
}
ctx.emit('update:modelValue', (e.target as HTMLInputElement).value);
ctx.emit('update', (e.target as HTMLInputElement).value);
};
const onChange = (e: Event) => {
ctx.emit('change', (e.target as HTMLInputElement).value);
};
const onKeydown = (e: KeyboardEvent) => {
ctx.emit('keydown', e);
};
const onCompositionStart = () => {
isComposition.value = true;
};
const onCompositionUpdate = (e: CompositionEvent) => {
const text = (e.target as HTMLInputElement)?.value;
const lastCharacter = text[text.length - 1] || '';
isComposition.value = !/([(\uAC00-\uD7AF)|(\u3130-\u318F)])+/gi.test(lastCharacter);
};
const onCompositionEnd = (e: CompositionEvent) => {
if (isComposition.value) {
isComposition.value = false;
onInput(e);
}
};
return { onFocus, onBlur, onInput, onChange, onKeydown, onCompositionStart, onCompositionUpdate, onCompositionEnd };
}