Skip to content

Commit

Permalink
Fix the bug of textarea with maxlength can't input before limit (#3044)
Browse files Browse the repository at this point in the history
* fix(textarea.tsx): 修复textarea在折叠面板内时,高度计算不正确,会出现滚动条的问题

修复用户反馈issue"[Textarea]
textarea在折叠面板内时,高度计算不正确,会出现滚动条",issue详情见:#2809

* fix(textarea.tsx): fixed the bug where textarea with maxlength can't input before limit

用户提出issue,反馈设置maxlength后,windows自带输入法,当输入中文时所输入字符还没有达到最大长度也会没法输入.
  • Loading branch information
azx1573 authored Jan 22, 2024
1 parent b5a18d8 commit f82e178
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions src/textarea/textarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,20 @@ export default mixins(Vue as VueConstructor<Textarea>, classPrefixMixins).extend

methods: {
adjustTextareaHeight() {
if (!this.$refs.refTextareaElem) return;
if (this.autosize === true) {
this.textareaStyle = calcTextareaHeight(this.$refs.refTextareaElem as HTMLTextAreaElement);
} else if (this.autosize && typeof this.autosize === 'object') {
this.textareaStyle = calcTextareaHeight(
this.$refs.refTextareaElem as HTMLTextAreaElement,
this.autosize?.minRows,
this.autosize?.maxRows,
);
} else if (this.$attrs.rows) {
this.textareaStyle = { height: 'auto', minHeight: 'auto' };
}
this.$nextTick(() => {
if (!this.$refs.refTextareaElem) return;
if (this.autosize === true) {
this.textareaStyle = calcTextareaHeight(this.$refs.refTextareaElem as HTMLTextAreaElement);
} else if (this.autosize && typeof this.autosize === 'object') {
this.textareaStyle = calcTextareaHeight(
this.$refs.refTextareaElem as HTMLTextAreaElement,
this.autosize?.minRows,
this.autosize?.maxRows,
);
} else if (this.$attrs.rows) {
this.textareaStyle = { height: 'auto', minHeight: 'auto' };
}
});
},
/**
* Provide a method to calculate the height of textArea component after DOM mounted
Expand Down Expand Up @@ -168,16 +170,21 @@ export default mixins(Vue as VueConstructor<Textarea>, classPrefixMixins).extend
inputValueChangeHandle(e: InputEvent) {
const { target } = e;
let val = (target as HTMLInputElement).value;
if (this.maxlength) {
val = limitUnicodeMaxLength(val, Number(this.maxlength));
}
if (this.maxcharacter && this.maxcharacter >= 0) {
const stringInfo = getCharacterLength(val, this.maxcharacter);
val = typeof stringInfo === 'object' && stringInfo.characters;
if (!this.isComposing) {
if (this.maxlength) {
val = limitUnicodeMaxLength(val, Number(this.maxlength));
}
if (this.maxcharacter && this.maxcharacter >= 0) {
const stringInfo = getCharacterLength(val, this.maxcharacter);
val = typeof stringInfo === 'object' && stringInfo.characters;
}
}

this.$emit('input', val);
// 中文输入时不触发 onChange
!this.isComposing && this.emitEvent('change', val, { e });
// 中文输入结束才触发 onChange
if (!this.isComposing) {
this.emitEvent('change', val, { e });
}

this.$nextTick(() => this.setInputValue(val));
this.adjustTextareaHeight();
Expand Down

0 comments on commit f82e178

Please sign in to comment.