From d9d6284dad482c5a823c559bacd54552f14b111d Mon Sep 17 00:00:00 2001
From: coder77 <2293444075@qq.com>
Date: Tue, 01 Apr 2025 14:31:03 +0800
Subject: [PATCH] feat: CTextArea组件增加maxLength

---
 forms/textarea/CTextArea.vue |   32 +++++++++++++++++++++++---------
 1 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/forms/textarea/CTextArea.vue b/forms/textarea/CTextArea.vue
index a72589e..8260c98 100644
--- a/forms/textarea/CTextArea.vue
+++ b/forms/textarea/CTextArea.vue
@@ -1,16 +1,13 @@
 /**
  * CTextArea
  * 多行文本输入组件,用于在表单中收集用户的多行文本输入
- * 支持设置输入区域高度,可以通过行数或像素值来控制
+ * 支持设置输入区域高度,可以通过字数、行数或像素值来控制
  * 支持只读模式和自动增高功能
  * @author Tevin
  */
 
 <template>
-    <view
-        class="c-textarea"
-        :class="readOnly?'read-only':''"
-    >
+    <view class="c-textarea" :class="readOnly ? 'read-only' : ''">
         <AtInput
             ref="input"
             :name="itemRes.name"
@@ -22,12 +19,16 @@
         <textarea
             ref="textarea"
             class="textarea"
-            :style="{minHeight: areaHeight, height: areaHeight}"
+            :style="{
+                minHeight: minHeight,
+                maxHeight: maxHeight,
+            }"
             :placeholder="placeholder"
             :value="itemRes.formData[itemRes.name]"
+            :maxlength="maxLength"
             :autoFocus="false"
             :autoHeight="true"
-            @input="evt=>itemRes.onChange(evt.detail.value)"
+            @input="evt => itemRes.onChange(evt.detail.value)"
         />
     </view>
 </template>
@@ -53,6 +54,8 @@
         },
         // 文本雨输入区行数
         rows: Number,
+        // 最大输入长度
+        maxLength: Number,
         // 只读模式
         readOnly: {
             type: Boolean,
@@ -65,10 +68,21 @@
         return {};
     },
     computed: {
-        areaHeight() {
-            if (this.rows) {
+        minHeight() {
+            // 默认最小高度为2行
+            const defaultRows = 2;
+            return Taro.pxTransform(defaultRows * 40, 750);
+        },
+        maxHeight() {
+            if (this.maxLength > 0) {
+                // maxLength 优先级最高
+                const estimatedRows = Math.ceil(this.maxLength / 25);
+                return Taro.pxTransform(estimatedRows * 40, 750);
+            } else if (this.rows) {
+                // 其次是 rows
                 return Taro.pxTransform(this.rows * 40, 750);
             } else {
+                // 最后是 height
                 return Taro.pxTransform(this.height, 750);
             }
         },

--
Gitblit v1.9.1