bug
jiangping
2023-11-07 64b432916af9c9218ab3f3eca614e26c542142ae
minipro_standard/uni_modules/uview-ui/components/u-textarea/u-textarea.vue
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,239 @@
<template>
    <view class="u-textarea" :class="textareaClass" :style="[textareaStyle]">
        <textarea
            class="u-textarea__field"
            :value="innerValue"
            :style="{ height: $u.addUnit(height) }"
            :placeholder="placeholder"
            :placeholder-style="$u.addStyle(placeholderStyle, 'string')"
            :placeholder-class="placeholderClass"
            :disabled="disabled"
            :focus="focus"
            :autoHeight="autoHeight"
            :fixed="fixed"
            :cursorSpacing="cursorSpacing"
            :cursor="cursor"
            :showConfirmBar="showConfirmBar"
            :selectionStart="selectionStart"
            :selectionEnd="selectionEnd"
            :adjustPosition="adjustPosition"
            :disableDefaultPadding="disableDefaultPadding"
            :holdKeyboard="holdKeyboard"
            :maxlength="maxlength"
            :confirmType="confirmType"
            :ignoreCompositionEvent="ignoreCompositionEvent"
            @focus="onFocus"
            @blur="onBlur"
            @linechange="onLinechange"
            @input="onInput"
            @confirm="onConfirm"
            @keyboardheightchange="onKeyboardheightchange"
        ></textarea>
        <text
            class="u-textarea__count"
            :style="{
                'background-color': disabled ? 'transparent' : '#fff',
            }"
            v-if="count"
            >{{ innerValue.length }}/{{ maxlength }}</text
        >
    </view>
</template>
<script>
import props from "./props.js";
/**
 * Textarea æ–‡æœ¬åŸŸ
 * @description æ–‡æœ¬åŸŸæ­¤ç»„件满足了可能出现的表单信息补充,编辑等实际逻辑的功能,内置了字数校验等
 * @tutorial https://www.uviewui.com/components/textarea.html
 *
 * @property {String | Number}       value               è¾“入框的内容
 * @property {String | Number}      placeholder            è¾“入框为空时占位符
 * @property {String}             placeholderClass      æŒ‡å®šplaceholder的样式类,注意页面或组件的style中写了scoped时,需要在类名前写/deep/ ï¼ˆ é»˜è®¤ 'input-placeholder' ï¼‰
 * @property {String | Object}       placeholderStyle      æŒ‡å®šplaceholder的样式,字符串/对象形式,如"color: red;"
 * @property {String | Number}      height               è¾“入框高度(默认 70 ï¼‰
 * @property {String}            confirmType            è®¾ç½®é”®ç›˜å³ä¸‹è§’按钮的文字,仅微信小程序,App-vue和H5有效(默认 'done' ï¼‰
 * @property {Boolean}            disabled            æ˜¯å¦ç¦ç”¨ï¼ˆé»˜è®¤ false ï¼‰
 * @property {Boolean}            count               æ˜¯å¦æ˜¾ç¤ºç»Ÿè®¡å­—数(默认 false ï¼‰
 * @property {Boolean}            focus               æ˜¯å¦è‡ªåŠ¨èŽ·å–ç„¦ç‚¹ï¼Œnvue不支持,H5取决于浏览器的实现(默认 false ï¼‰
 * @property {Boolean | Function}   autoHeight            æ˜¯å¦è‡ªåŠ¨å¢žåŠ é«˜åº¦ï¼ˆé»˜è®¤ false ï¼‰
 * @property {Boolean}            fixed               å¦‚æžœtextarea是在一个position:fixed的区域,需要显示指定属性fixed为true(默认 false ï¼‰
 * @property {Number}            cursorSpacing         æŒ‡å®šå…‰æ ‡ä¸Žé”®ç›˜çš„距离(默认 0 ï¼‰
 * @property {String | Number}      cursor               æŒ‡å®šfocus时的光标位置
 * @property {Function}             formatter             å†…容式化函数
 * @property {Boolean}            showConfirmBar         æ˜¯å¦æ˜¾ç¤ºé”®ç›˜ä¸Šæ–¹å¸¦æœ‰â€å®Œæˆâ€œæŒ‰é’®é‚£ä¸€æ ï¼Œï¼ˆé»˜è®¤ true ï¼‰
 * @property {Number}            selectionStart         å…‰æ ‡èµ·å§‹ä½ç½®ï¼Œè‡ªåŠ¨èšç„¦æ—¶æœ‰æ•ˆï¼Œéœ€ä¸Žselection-end搭配使用,(默认 -1 ï¼‰
 * @property {Number | Number}      selectionEnd         å…‰æ ‡ç»“束位置,自动聚焦时有效,需与selection-start搭配使用(默认 -1 ï¼‰
 * @property {Boolean}            adjustPosition         é”®ç›˜å¼¹èµ·æ—¶ï¼Œæ˜¯å¦è‡ªåŠ¨ä¸ŠæŽ¨é¡µé¢ï¼ˆé»˜è®¤ true ï¼‰
 * @property {Boolean | Number}      disableDefaultPadding   æ˜¯å¦åŽ»æŽ‰ iOS ä¸‹çš„默认内边距,只微信小程序有效(默认 false ï¼‰
 * @property {Boolean}            holdKeyboard         focus时,点击页面的时候不收起键盘,只微信小程序有效(默认 false ï¼‰
 * @property {String | Number}      maxlength            æœ€å¤§è¾“入长度,设置为 -1 çš„æ—¶å€™ä¸é™åˆ¶æœ€å¤§é•¿åº¦ï¼ˆé»˜è®¤ 140 ï¼‰
 * @property {String}            border               è¾¹æ¡†ç±»åž‹ï¼Œsurround-四周边框,none-无边框,bottom-底部边框(默认 'surround' ï¼‰
 * @property {Boolean}            ignoreCompositionEvent   æ˜¯å¦å¿½ç•¥ç»„件内对文本合成系统事件的处理
 *
 * @event {Function(e)} focus               è¾“入框聚焦时触发,event.detail = { value, height },height ä¸ºé”®ç›˜é«˜åº¦
 * @event {Function(e)} blur               è¾“入框失去焦点时触发,event.detail = {value, cursor}
 * @event {Function(e)} linechange            è¾“入框行数变化时调用,event.detail = {height: 0, heightRpx: 0, lineCount: 0}
 * @event {Function(e)} input               å½“键盘输入时,触发 input äº‹ä»¶
 * @event {Function(e)} confirm               ç‚¹å‡»å®Œæˆæ—¶ï¼Œ è§¦å‘ confirm äº‹ä»¶
 * @event {Function(e)} keyboardheightchange   é”®ç›˜é«˜åº¦å‘生变化的时候触发此事件
 * @example <u--textarea v-model="value1" placeholder="请输入内容" ></u--textarea>
 */
export default {
    name: "u-textarea",
    mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
   data() {
      return {
         // è¾“入框的值
         innerValue: "",
         // æ˜¯å¦å¤„于获得焦点状态
         focused: false,
         // value是否第一次变化,在watch中,由于加入immediate属性,会在第一次触发,此时不应该认为value发生了变化
         firstChange: true,
         // value绑定值的变化是由内部还是外部引起的
         changeFromInner: false,
         // è¿‡æ»¤å¤„理方法
         innerFormatter: value => value
      }
   },
   watch: {
       value: {
           immediate: true,
           handler(newVal, oldVal) {
               this.innerValue = newVal;
               /* #ifdef H5 */
               // åœ¨H5中,外部value变化后,修改input中的值,不会触发@input事件,此时手动调用值变化方法
               if (
                   this.firstChange === false &&
                   this.changeFromInner === false
               ) {
                   this.valueChange();
               }
               /* #endif */
               this.firstChange = false;
               // é‡ç½®changeFromInner的值为false,标识下一次引起默认为外部引起的
               this.changeFromInner = false;
           },
       },
   },
    computed: {
        // ç»„件的类名
        textareaClass() {
            let classes = [],
                { border, disabled, shape } = this;
            border === "surround" &&
                (classes = classes.concat(["u-border", "u-textarea--radius"]));
            border === "bottom" &&
                (classes = classes.concat([
                    "u-border-bottom",
                    "u-textarea--no-radius",
                ]));
            disabled && classes.push("u-textarea--disabled");
            return classes.join(" ");
        },
        // ç»„件的样式
        textareaStyle() {
            const style = {};
            // #ifdef APP-NVUE
            // ç”±äºŽtextarea在安卓nvue上的差异性,需要额外再调整其内边距
            if (uni.$u.os() === "android") {
                style.paddingTop = "6px";
                style.paddingLeft = "9px";
                style.paddingBottom = "3px";
                style.paddingRight = "6px";
            }
            // #endif
            return uni.$u.deepMerge(style, uni.$u.addStyle(this.customStyle));
        },
    },
    methods: {
      // åœ¨å¾®ä¿¡å°ç¨‹åºä¸­ï¼Œä¸æ”¯æŒå°†å‡½æ•°å½“做props参数,故只能通过ref形式调用
      setFormatter(e) {
         this.innerFormatter = e
      },
        onFocus(e) {
            this.$emit("focus", e);
        },
        onBlur(e) {
            this.$emit("blur", e);
            // å°è¯•调用u-form的验证方法
            uni.$u.formValidate(this, "blur");
        },
        onLinechange(e) {
            this.$emit("linechange", e);
        },
        onInput(e) {
         let { value = "" } = e.detail || {};
         // æ ¼å¼åŒ–过滤方法
         const formatter = this.formatter || this.innerFormatter
         const formatValue = formatter(value)
         // ä¸ºäº†é¿å…props的单向数据流特性,需要先将innerValue值设置为当前值,再在$nextTick中重新赋予设置后的值才有效
         this.innerValue = value
         this.$nextTick(() => {
            this.innerValue = formatValue;
            this.valueChange();
         })
        },
      // å†…容发生变化,进行处理
      valueChange() {
          const value = this.innerValue;
          this.$nextTick(() => {
              this.$emit("input", value);
              // æ ‡è¯†value值的变化是由内部引起的
              this.changeFromInner = true;
              this.$emit("change", value);
              // å°è¯•调用u-form的验证方法
              uni.$u.formValidate(this, "change");
          });
      },
        onConfirm(e) {
            this.$emit("confirm", e);
        },
        onKeyboardheightchange(e) {
            this.$emit("keyboardheightchange", e);
        },
    },
};
</script>
<style lang="scss" scoped>
@import "../../libs/css/components.scss";
.u-textarea {
    border-radius: 4px;
    background-color: #fff;
    position: relative;
    @include flex;
    flex: 1;
   padding: 9px;
    &--radius {
        border-radius: 4px;
    }
    &--no-radius {
        border-radius: 0;
    }
    &--disabled {
        background-color: #f5f7fa;
    }
    &__field {
        flex: 1;
        font-size: 15px;
        color: $u-content-color;
      width: 100%;
    }
    &__count {
        position: absolute;
        right: 5px;
        bottom: 2px;
        font-size: 12px;
        color: $u-tips-color;
        background-color: #ffffff;
        padding: 1px 4px;
    }
}
</style>