ant-design-vue/components/_util/antInputDirective.ts

42 lines
1.2 KiB
TypeScript
Raw Normal View History

2020-09-30 09:15:17 +00:00
function onCompositionStart(e: Event) {
(e.target as any).composing = true;
}
2020-09-30 09:15:17 +00:00
function onCompositionEnd(e: Event) {
const target = e.target as any;
if (target.composing) {
target.composing = false;
trigger(target, 'input');
}
}
2020-09-30 09:15:17 +00:00
function trigger(el: HTMLElement, type: string) {
2019-01-12 03:33:27 +00:00
const e = document.createEvent('HTMLEvents');
e.initEvent(type, true, true);
el.dispatchEvent(e);
}
2020-09-30 09:15:17 +00:00
export function addEventListener(
el: Element,
event: string,
handler: EventListener,
options?: EventListenerOptions,
) {
el.addEventListener(event, handler, options);
}
const antInput = {
2020-09-30 09:15:17 +00:00
created(el: Element, binding: { modifiers: { lazy: any } }) {
if (!binding.modifiers || !binding.modifiers.lazy) {
addEventListener(el, 'compositionstart', onCompositionStart);
addEventListener(el, 'compositionend', onCompositionEnd);
// Safari < 10.2 & UIWebView doesn't fire compositionend when
// switching focus before confirming composition choice
// this also fixes the issue where some browsers e.g. iOS Chrome
// fires "change" instead of "input" on autocomplete.
addEventListener(el, 'change', onCompositionEnd);
2020-07-03 14:53:50 +00:00
}
},
};
2018-12-13 14:47:23 +00:00
export default antInput;