🐛(textarea): 修复拼字阶段触发 update:modelValue 的问题

This commit is contained in:
就眠儀式 2022-11-03 22:33:53 +08:00
parent e8078f76b1
commit 4a42c6c162
3 changed files with 20 additions and 5 deletions

View File

@ -73,7 +73,7 @@ var timer: any;
const getOption = (nodes: VNode[], newOptions: any[]) => { const getOption = (nodes: VNode[], newOptions: any[]) => {
nodes?.map((item) => { nodes?.map((item) => {
let component = item.type as Component; let component = item.type as Component;
if(item.type.toString() == "Symbol(Fragment)") { if (item.type.toString() == "Symbol(Fragment)") {
getOption(item.children as VNode[], newOptions); getOption(item.children as VNode[], newOptions);
} else { } else {
if (component.name == LaySelectOption.name) { if (component.name == LaySelectOption.name) {

View File

@ -25,7 +25,7 @@ import {
reactive, reactive,
h, h,
createTextVNode, createTextVNode,
Fragment Fragment,
} from "vue"; } from "vue";
import { useResizeObserver } from "@vueuse/core"; import { useResizeObserver } from "@vueuse/core";
import { TabData, TabInjectKey, TabPosition } from "./interface"; import { TabData, TabInjectKey, TabPosition } from "./interface";
@ -47,7 +47,7 @@ const tabMap = reactive(new Map<number, TabData>());
const setItemInstanceBySlot = function (nodes: VNode[]) { const setItemInstanceBySlot = function (nodes: VNode[]) {
nodes?.map((item) => { nodes?.map((item) => {
let component = item.type as Component; let component = item.type as Component;
if(item.type.toString() == "Symbol(Fragment)") { if (item.type.toString() == "Symbol(Fragment)") {
setItemInstanceBySlot(item.children as VNode[]); setItemInstanceBySlot(item.children as VNode[]);
} else { } else {
if (component.name == tabItem.name) { if (component.name == tabItem.name) {

View File

@ -6,7 +6,7 @@ export default {
<script setup lang="ts"> <script setup lang="ts">
import { LayIcon } from "@layui/icons-vue"; import { LayIcon } from "@layui/icons-vue";
import { computed } from "vue"; import { computed, ref } from "vue";
import "./index.less"; import "./index.less";
export interface TextareaProps { export interface TextareaProps {
@ -31,11 +31,15 @@ interface TextareaEmits {
} }
const emit = defineEmits<TextareaEmits>(); const emit = defineEmits<TextareaEmits>();
const composing = ref(false);
const onInput = function (event: Event) { const onInput = function (event: Event) {
const inputElement = event.target as HTMLInputElement; const inputElement = event.target as HTMLInputElement;
emit("update:modelValue", inputElement.value);
emit("input", inputElement.value); emit("input", inputElement.value);
if (composing.value) {
return;
}
emit("update:modelValue", inputElement.value);
}; };
const onFocus = function (event: Event) { const onFocus = function (event: Event) {
@ -56,6 +60,15 @@ const onClear = function () {
emit("clear"); emit("clear");
}; };
const onCompositionstart = () => {
composing.value = true;
};
const onCompositionend = (event: Event) => {
composing.value = false;
onInput(event);
};
const hasContent = computed(() => (props.modelValue as string)?.length > 0); const hasContent = computed(() => (props.modelValue as string)?.length > 0);
const wordCount = computed(() => { const wordCount = computed(() => {
@ -77,6 +90,8 @@ const wordCount = computed(() => {
:disabled="disabled" :disabled="disabled"
:maxlength="maxlength" :maxlength="maxlength"
:class="{ 'layui-textarea-disabled': disabled }" :class="{ 'layui-textarea-disabled': disabled }"
@compositionstart="onCompositionstart"
@compositionend="onCompositionend"
@input="onInput" @input="onInput"
@focus="onFocus" @focus="onFocus"
@change="onChange" @change="onChange"