WebApp【公共组件库】@前端(For Git Submodule)
coder77
2025-04-01 d9d6284dad482c5a823c559bacd54552f14b111d
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
 * CTextArea
 * 多行文本输入组件,用于在表单中收集用户的多行文本输入
 * 支持设置输入区域高度,可以通过字数、行数或像素值来控制
 * 支持只读模式和自动增高功能
 * @author Tevin
 */
 
<template>
    <view class="c-textarea" :class="readOnly ? 'read-only' : ''">
        <AtInput
            ref="input"
            :name="itemRes.name"
            :title="itemRes.label"
            :required="itemRes.required"
            :disabled="itemRes.disabled"
            :error="itemRes.error"
        />
        <textarea
            ref="textarea"
            class="textarea"
            :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)"
        />
    </view>
</template>
 
<script>
import Taro from '@tarojs/taro';
import { $ } from '@tarojs/extend';
import { AtInput } from 'taro-ui-vue';
import './cTextArea.scss';
 
export default {
    name: 'CTextArea',
    components: {
        AtInput,
    },
    props: {
        // 表单数据资源(表单组件内部机制专用)
        itemRes: Object,
        // 文本域输入区域高度
        height: {
            type: Number,
            default: 94,
        },
        // 文本雨输入区行数
        rows: Number,
        // 最大输入长度
        maxLength: Number,
        // 只读模式
        readOnly: {
            type: Boolean,
            default: false,
        },
        // 占位提示
        placeholder: String,
    },
    data() {
        return {};
    },
    computed: {
        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);
            }
        },
    },
    methods: {},
    mounted() {
        if (process.env.TARO_ENV === 'h5') {
            $(this.$refs.input.$el)
                .find('.at-input__input')
                .prepend(this.$refs.textarea.$el);
        } else if (process.env.TARO_ENV === 'weapp') {
            $(this.$refs.input.$el)
                .find('.at-input__container')
                .append(this.$refs.textarea);
        }
    },
};
</script>