bug
jiangping
2023-11-07 64b432916af9c9218ab3f3eca614e26c542142ae
minipro_standard/uni_modules/uview-ui/components/u-input/u-input.vue
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,354 @@
<template>
    <view class="u-input" :class="inputClass" :style="[wrapperStyle]">
        <view class="u-input__content">
            <view
                class="u-input__content__prefix-icon"
                v-if="prefixIcon || $slots.prefix"
            >
                <slot name="prefix">
                    <u-icon
                        :name="prefixIcon"
                        size="18"
                        :customStyle="prefixIconStyle"
                    ></u-icon>
                </slot>
            </view>
            <view class="u-input__content__field-wrapper" @tap="clickHandler">
            <!-- æ ¹æ®uni-app的input组件文档,H5和APP中只要声明了password参数(无论true还是false),type均失效,此时
               ä¸ºäº†é˜²æ­¢type=number时,又存在password属性,type无效,此时需要设置password为undefined
             -->
               <input
                   class="u-input__content__field-wrapper__field"
                   :style="[inputStyle]"
                   :type="type"
                   :focus="focus"
                   :cursor="cursor"
                   :value="innerValue"
                   :auto-blur="autoBlur"
                   :disabled="disabled || readonly"
                   :maxlength="maxlength"
                   :placeholder="placeholder"
                   :placeholder-style="placeholderStyle"
                   :placeholder-class="placeholderClass"
                   :confirm-type="confirmType"
                   :confirm-hold="confirmHold"
                   :hold-keyboard="holdKeyboard"
                   :cursor-spacing="cursorSpacing"
                   :adjust-position="adjustPosition"
                   :selection-end="selectionEnd"
                   :selection-start="selectionStart"
                   :password="password || type === 'password' || undefined"
                    :ignoreCompositionEvent="ignoreCompositionEvent"
                   @input="onInput"
                   @blur="onBlur"
                   @focus="onFocus"
                   @confirm="onConfirm"
                   @keyboardheightchange="onkeyboardheightchange"
               />
            </view>
            <view
                class="u-input__content__clear"
                v-if="isShowClear"
                @tap="onClear"
            >
                <u-icon
                    name="close"
                    size="11"
                    color="#ffffff"
                    customStyle="line-height: 12px"
                ></u-icon>
            </view>
            <view
                class="u-input__content__subfix-icon"
                v-if="suffixIcon || $slots.suffix"
            >
                <slot name="suffix">
                    <u-icon
                        :name="suffixIcon"
                        size="18"
                        :customStyle="suffixIconStyle"
                    ></u-icon>
                </slot>
            </view>
        </view>
    </view>
</template>
<script>
import props from "./props.js";
/**
 * Input è¾“入框
 * @description  æ­¤ç»„件为一个输入框,默认没有边框和样式,是专门为配合表单组件u-form而设计的,利用它可以快速实现表单验证,输入内容,下拉选择等功能。
 * @tutorial https://uviewui.com/components/input.html
 * @property {String | Number}   value               è¾“入的值
 * @property {String}         type               è¾“入框类型,见上方说明 ï¼ˆ é»˜è®¤ 'text' ï¼‰
 * @property {Boolean}         fixed               å¦‚æžœ textarea æ˜¯åœ¨ä¸€ä¸ª position:fixed çš„区域,需要显示指定属性 fixed ä¸º true,兼容性:微信小程序、百度小程序、字节跳动小程序、QQ小程序 ï¼ˆ é»˜è®¤ false ï¼‰
 * @property {Boolean}         disabled            æ˜¯å¦ç¦ç”¨è¾“入框 ï¼ˆ é»˜è®¤ false ï¼‰
 * @property {String}         disabledColor         ç¦ç”¨çŠ¶æ€æ—¶çš„èƒŒæ™¯è‰²ï¼ˆ é»˜è®¤ '#f5f7fa' ï¼‰
 * @property {Boolean}         clearable            æ˜¯å¦æ˜¾ç¤ºæ¸…除控件 ï¼ˆ é»˜è®¤ false ï¼‰
 * @property {Boolean}         password            æ˜¯å¦å¯†ç ç±»åž‹ ï¼ˆ é»˜è®¤ false ï¼‰
 * @property {String | Number}   maxlength            æœ€å¤§è¾“入长度,设置为 -1 çš„æ—¶å€™ä¸é™åˆ¶æœ€å¤§é•¿åº¦ ï¼ˆ é»˜è®¤ -1 ï¼‰
 * @property {String}         placeholder            è¾“入框为空时的占位符
 * @property {String}         placeholderClass      æŒ‡å®šplaceholder的样式类,注意页面或组件的style中写了scoped时,需要在类名前写/deep/ ï¼ˆ é»˜è®¤ 'input-placeholder' ï¼‰
 * @property {String | Object}   placeholderStyle      æŒ‡å®šplaceholder的样式,字符串/对象形式,如"color: red;"
 * @property {Boolean}         showWordLimit         æ˜¯å¦æ˜¾ç¤ºè¾“入字数统计,只在 type ="text"或type ="textarea"时有效 ï¼ˆ é»˜è®¤ false ï¼‰
 * @property {String}         confirmType            è®¾ç½®å³ä¸‹è§’按钮的文字,兼容性详见uni-app文档 ï¼ˆ é»˜è®¤ 'done' ï¼‰
 * @property {Boolean}         confirmHold            ç‚¹å‡»é”®ç›˜å³ä¸‹è§’按钮时是否保持键盘不收起,H5无效 ï¼ˆ é»˜è®¤ false ï¼‰
 * @property {Boolean}         holdKeyboard         focus时,点击页面的时候不收起键盘,微信小程序有效 ï¼ˆ é»˜è®¤ false ï¼‰
 * @property {Boolean}         focus               è‡ªåŠ¨èŽ·å–ç„¦ç‚¹ï¼Œåœ¨ H5 å¹³å°èƒ½å¦èšç„¦ä»¥åŠè½¯é”®ç›˜æ˜¯å¦è·Ÿéšå¼¹å‡ºï¼Œå–决于当前浏览器本身的实现。nvue é¡µé¢ä¸æ”¯æŒï¼Œéœ€ä½¿ç”¨ç»„ä»¶çš„ focus()、blur() æ–¹æ³•控制焦点 ï¼ˆ é»˜è®¤ false ï¼‰
 * @property {Boolean}         autoBlur            é”®ç›˜æ”¶èµ·æ—¶ï¼Œæ˜¯å¦è‡ªåŠ¨å¤±åŽ»ç„¦ç‚¹ï¼Œç›®å‰ä»…App3.0.0+有效 ï¼ˆ é»˜è®¤ false ï¼‰
 * @property {Boolean}         disableDefaultPadding   æ˜¯å¦åŽ»æŽ‰ iOS ä¸‹çš„默认内边距,仅微信小程序,且type=textarea时有效 ï¼ˆ é»˜è®¤ false ï¼‰
 * @property {String ï½œ Number}   cursor               æŒ‡å®šfocus时光标的位置( é»˜è®¤ -1 ï¼‰
 * @property {String ï½œ Number}   cursorSpacing         è¾“入框聚焦时底部与键盘的距离 ï¼ˆ é»˜è®¤ 30 ï¼‰
 * @property {String ï½œ Number}   selectionStart         å…‰æ ‡èµ·å§‹ä½ç½®ï¼Œè‡ªåŠ¨èšé›†æ—¶æœ‰æ•ˆï¼Œéœ€ä¸Žselection-end搭配使用 ï¼ˆ é»˜è®¤ -1 ï¼‰
 * @property {String ï½œ Number}   selectionEnd         å…‰æ ‡ç»“束位置,自动聚集时有效,需与selection-start搭配使用 ï¼ˆ é»˜è®¤ -1 ï¼‰
 * @property {Boolean}         adjustPosition         é”®ç›˜å¼¹èµ·æ—¶ï¼Œæ˜¯å¦è‡ªåŠ¨ä¸ŠæŽ¨é¡µé¢ ï¼ˆ é»˜è®¤ true ï¼‰
 * @property {String}         inputAlign            è¾“入框内容对齐方式( é»˜è®¤ 'left' ï¼‰
 * @property {String | Number}   fontSize            è¾“入框字体的大小 ï¼ˆ é»˜è®¤ '15px' ï¼‰
 * @property {String}         color               è¾“入框字体颜色   ï¼ˆ é»˜è®¤ '#303133' ï¼‰
 * @property {Function}         formatter             å†…容式化函数
 * @property {String}         prefixIcon            è¾“入框前置图标
 * @property {String | Object}   prefixIconStyle         å‰ç½®å›¾æ ‡æ ·å¼ï¼Œå¯¹è±¡æˆ–字符串
 * @property {String}         suffixIcon            è¾“入框后置图标
 * @property {String | Object}   suffixIconStyle         åŽç½®å›¾æ ‡æ ·å¼ï¼Œå¯¹è±¡æˆ–字符串
 * @property {String}         border               è¾¹æ¡†ç±»åž‹ï¼Œsurround-四周边框,bottom-底部边框,none-无边框 ï¼ˆ é»˜è®¤ 'surround' ï¼‰
 * @property {Boolean}         readonly            æ˜¯å¦åªè¯»ï¼Œä¸Ždisabled不同之处在于disabled会置灰组件,而readonly则不会 ï¼ˆ é»˜è®¤ false ï¼‰
 * @property {String}         shape               è¾“入框形状,circle-圆形,square-方形 ï¼ˆ é»˜è®¤ 'square' ï¼‰
 * @property {Object}         customStyle            å®šä¹‰éœ€è¦ç”¨åˆ°çš„外部样式
 * @property {Boolean}         ignoreCompositionEvent   æ˜¯å¦å¿½ç•¥ç»„件内对文本合成系统事件的处理。
 * @example <u-input v-model="value" :password="true" suffix-icon="lock-fill" />
 */
export default {
    name: "u-input",
    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: {
        // æ˜¯å¦æ˜¾ç¤ºæ¸…除控件
        isShowClear() {
            const { clearable, readonly, focused, innerValue } = this;
            return !!clearable && !readonly && !!focused && innerValue !== "";
        },
        // ç»„件的类名
        inputClass() {
            let classes = [],
                { border, disabled, shape } = this;
            border === "surround" &&
                (classes = classes.concat(["u-border", "u-input--radius"]));
            classes.push(`u-input--${shape}`);
            border === "bottom" &&
                (classes = classes.concat([
                    "u-border-bottom",
                    "u-input--no-radius",
                ]));
            return classes.join(" ");
        },
        // ç»„件的样式
        wrapperStyle() {
            const style = {};
            // ç¦ç”¨çŠ¶æ€ä¸‹ï¼Œè¢«èƒŒæ™¯è‰²åŠ ä¸Šå¯¹åº”çš„æ ·å¼
            if (this.disabled) {
                style.backgroundColor = this.disabledColor;
            }
            // æ— è¾¹æ¡†æ—¶ï¼ŒåŽ»é™¤å†…è¾¹è·
            if (this.border === "none") {
                style.padding = "0";
            } else {
                // ç”±äºŽuni-app的iOS开发者能力有限,导致需要分开写才有效
                style.paddingTop = "6px";
                style.paddingBottom = "6px";
                style.paddingLeft = "9px";
                style.paddingRight = "9px";
            }
            return uni.$u.deepMerge(style, uni.$u.addStyle(this.customStyle));
        },
        // è¾“入框的样式
        inputStyle() {
            const style = {
                color: this.color,
                fontSize: uni.$u.addUnit(this.fontSize),
            textAlign: this.inputAlign
            };
            return style;
        },
    },
    methods: {
      // åœ¨å¾®ä¿¡å°ç¨‹åºä¸­ï¼Œä¸æ”¯æŒå°†å‡½æ•°å½“做props参数,故只能通过ref形式调用
      setFormatter(e) {
         this.innerFormatter = e
      },
        // å½“键盘输入时,触发input事件
        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();
            })
        },
        // è¾“入框失去焦点时触发
        onBlur(event) {
            this.$emit("blur", event.detail.value);
            // H5端的blur会先于点击清除控件的点击click事件触发,导致focused
            // çž¬é—´ä¸ºfalse,从而隐藏了清除控件而无法被点击到
            uni.$u.sleep(50).then(() => {
                this.focused = false;
            });
            // å°è¯•调用u-form的验证方法
            uni.$u.formValidate(this, "blur");
        },
        // è¾“入框聚焦时触发
        onFocus(event) {
            this.focused = true;
            this.$emit("focus");
        },
        // ç‚¹å‡»å®ŒæˆæŒ‰é’®æ—¶è§¦å‘
        onConfirm(event) {
            this.$emit("confirm", this.innerValue);
        },
        // é”®ç›˜é«˜åº¦å‘生变化的时候触发此事件
        // å…¼å®¹æ€§ï¼šå¾®ä¿¡å°ç¨‹åº2.7.0+、App 3.1.0+
      onkeyboardheightchange() {
            this.$emit("keyboardheightchange");
        },
        // å†…容发生变化,进行处理
        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");
            });
        },
        // ç‚¹å‡»æ¸…除控件
        onClear() {
            this.innerValue = "";
            this.$nextTick(() => {
                this.valueChange();
                this.$emit("clear");
            });
        },
        /**
         * åœ¨å®‰å“nvue上,事件无法冒泡
         * åœ¨æŸäº›æ—¶é—´ï¼Œæˆ‘们希望监听u-from-item的点击事件,此时会导致点击u-form-item内的u-input后
         * æ— æ³•触发u-form-item的点击事件,这里通过手动调用u-form-item的方法进行触发
         */
        clickHandler() {
            // #ifdef APP-NVUE
            if (uni.$u.os() === "android") {
                const formItem = uni.$u.$parent.call(this, "u-form-item");
                if (formItem) {
                    formItem.clickHandler();
                }
            }
            // #endif
        },
    },
};
</script>
<style lang="scss" scoped>
@import "../../libs/css/components.scss";
.u-input {
    @include flex(row);
    align-items: center;
    justify-content: space-between;
    flex: 1;
    &--radius,
    &--square {
        border-radius: 4px;
    }
    &--no-radius {
        border-radius: 0;
    }
    &--circle {
        border-radius: 100px;
    }
    &__content {
        flex: 1;
        @include flex(row);
        align-items: center;
        justify-content: space-between;
        &__field-wrapper {
            position: relative;
            @include flex(row);
            margin: 0;
            flex: 1;
         &__field {
            line-height: 26px;
            text-align: left;
            color: $u-main-color;
            height: 24px;
            font-size: 15px;
            flex: 1;
         }
        }
        &__clear {
            width: 20px;
            height: 20px;
            border-radius: 100px;
            background-color: #c6c7cb;
            @include flex(row);
            align-items: center;
            justify-content: center;
            transform: scale(0.82);
            margin-left: 4px;
        }
        &__subfix-icon {
            margin-left: 4px;
        }
        &__prefix-icon {
            margin-right: 4px;
        }
    }
}
</style>