zjk
2026-09-29 54dcf0a82e7490fa58b1c056d5c0b9cb739c2931
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<template>
    <view
        class="u-input"
        :class="[
            {
                'u-input--border': border,
                'u-input--error': validateState
            },
            customClass
        ]"
        :style="{
            padding: type === 'textarea' ? (border ? '20rpx' : '0') : `0 ${border ? 20 : 0}rpx`,
            borderColor: borderColor,
            textAlign: inputAlign
        }"
        @tap.stop="inputClick"
    >
        <textarea
            v-if="type == 'textarea'"
            class="u-input__input u-input__textarea"
            :style="getStyle"
            :value="defaultValue"
            :placeholder="placeholder"
            :placeholderStyle="placeholderStyle"
            :disabled="disabled"
            :maxlength="inputMaxlength"
            :fixed="fixed"
            :focus="focus"
            :autoHeight="autoHeight"
            :selection-end="uSelectionEnd"
            :selection-start="uSelectionStart"
            :cursor-spacing="getCursorSpacing"
            :show-confirm-bar="showConfirmbar"
            :adjust-position="adjustPosition"
            @input="handleInput"
            @blur="handleBlur"
            @focus="onFocus"
            @confirm="onConfirm"
        />
        <input
            v-else
            class="u-input__input"
            :type="type == 'password' ? 'text' : type"
            :style="getStyle"
            :value="defaultValue"
            :password="type == 'password' && !showPassword"
            :placeholder="placeholder"
            :placeholderStyle="placeholderStyle"
            :disabled="disabled || type === 'select'"
            :maxlength="inputMaxlength"
            :focus="focus"
            :confirmType="confirmType"
            :cursor-spacing="getCursorSpacing"
            :selection-end="uSelectionEnd"
            :selection-start="uSelectionStart"
            :show-confirm-bar="showConfirmbar"
            :adjust-position="adjustPosition"
            @focus="onFocus"
            @blur="handleBlur"
            @input="handleInput"
            @confirm="onConfirm"
        />
        <view class="u-input__right-icon u-flex">
            <view
                class="u-input__right-icon__clear u-input__right-icon__item"
                v-if="clearable && modelValue != '' && !disabled"
            >
                <u-icon size="32" name="close-circle-fill" color="var(--u-light-color)" @click="onClear" />
            </view>
            <view
                class="u-input__right-icon__clear u-input__right-icon__item"
                v-if="passwordIcon && type == 'password'"
            >
                <u-icon
                    size="32"
                    :name="!showPassword ? 'eye' : 'eye-fill'"
                    color="var(--u-light-color)"
                    @click="showPassword = !showPassword"
                />
            </view>
            <view
                class="u-input__right-icon--select u-input__right-icon__item"
                v-if="type == 'select'"
                :class="{
                    'u-input__right-icon--select--reverse': selectOpen
                }"
            >
                <u-icon name="arrow-down-fill" size="26" color="var(--u-light-color)"></u-icon>
            </view>
        </view>
        <text
            class="u-input__count"
            :style="{
                'background-color': props.disabled ? 'transparent' : 'var(--u-bg-white)'
            }"
            v-if="props.type === 'textarea' && props.count"
        >
            {{ String(defaultValue).length }}/{{ props.maxlength }}
        </text>
    </view>
</template>
 
<script lang="ts">
export default {
    name: 'u-input',
    options: {
        addGlobalClass: true,
        // #ifndef MP-TOUTIAO
        virtualHost: true,
        // #endif
        styleIsolation: 'shared'
    }
};
</script>
 
<script setup lang="ts">
import { ref, computed, watch } from 'vue';
import { $u, useChildren } from '../..';
import { InputProps } from './types';
 
const props = defineProps(InputProps);
const emit = defineEmits(['update:modelValue', 'input', 'blur', 'focus', 'confirm', 'click']);
 
const { emitToParent } = useChildren('u-input', 'u-form-item');
 
const defaultValue = ref(props.modelValue);
const inputHeight = 70; // input的高度
const textareaHeight = 100; // textarea的高度
const validateState = ref(props.validateState); // 当前input的验证状态,用于错误时,边框是否改为红色
const focused = ref(false); // 当前是否处于获得焦点的状态
const showPassword = ref(false); // 是否预览密码
const lastValue = ref(''); // 用于头条小程序,判断@input中,前后的值是否发生了变化
 
// 监听 value 变化
watch(
    () => props.modelValue,
    (nVal, oVal) => {
        defaultValue.value = nVal;
        // 当值发生变化,且为select类型时(此时input被设置为disabled,不会触发@input事件),模拟触发@input事件
        if (nVal != oVal && props.type == 'select') handleInput({ detail: { value: nVal } });
    }
);
 
// 监听 validateState 变化
watch(
    () => props.validateState,
    val => {
        validateState.value = val;
    }
);
 
// 计算属性
const inputMaxlength = computed(() => Number(props.maxlength));
 
const getStyle = computed(() => {
    let style: Record<string, any> = {};
    // 如果没有自定义高度,就根据type为input还是textarea来分配一个默认的高度
    style.minHeight = props.height
        ? props.height + 'rpx'
        : props.type == 'textarea'
          ? `${textareaHeight}rpx`
          : `${inputHeight}rpx`;
    return $u.toStyle(style, props.customStyle);
});
const getCursorSpacing = computed(() => Number(props.cursorSpacing));
// 光标起始位置
const uSelectionStart = computed(() => String(props.selectionStart));
// 光标结束位置
const uSelectionEnd = computed(() => String(props.selectionEnd));
 
/**
 * change 事件
 * @param event
 */
function handleInput(event: any) {
    let value = event.detail.value;
    // 判断是否去除空格
    if (props.trim) value = $u.trim(value);
    // 当前model 赋值
    defaultValue.value = value;
    emit('update:modelValue', value);
    emit('input', value);
    // 过一个生命周期再发送事件给u-form-item,否则this.$emit('update:modelValue')更新了父组件的值,但是微信小程序上
    // 尚未更新到u-form-item,导致获取的值为空,从而校验混论
    // 这里不能延时时间太短,或者使用this.$nextTick,否则在头条上,会造成混乱
    setTimeout(() => {
        // 头条小程序由于自身bug,导致中文下,每按下一个键(尚未完成输入),都会触发一次@input,导致错误,这里进行判断处理
        // #ifdef MP-TOUTIAO
        if ($u.trim(value) == lastValue.value) return;
        lastValue.value = value;
        // #endif
        // 通过 emitter 派发事件
        emitToParent('onFormChange', value);
    }, 40);
}
 
/**
 * blur 事件
 * @param event
 */
function handleBlur(event: any) {
    // 最开始使用的是监听图标@touchstart事件,自从hx2.8.4后,此方法在微信小程序出错
    // 这里改为监听点击事件,手点击清除图标时,同时也发生了@blur事件,导致图标消失而无法点击,这里做一个延时
    setTimeout(() => {
        focused.value = false;
    }, 100);
    setTimeout(() => {
        let value = String(defaultValue.value);
        emit('blur', value);
        // 头条小程序由于自身bug,导致中文下,每按下一个键(尚未完成输入),都会触发一次@input,导致错误,这里进行判断处理
        // #ifdef MP-TOUTIAO
        if ($u.trim(value) == lastValue.value) return;
        lastValue.value = value;
        // #endif
        emitToParent('onFormBlur', value);
    }, 40);
}
 
function onFormItemError(status: boolean) {
    validateState.value = status;
}
 
function onFocus(e: any) {
    focused.value = true;
    emit('focus', e.detail.value);
}
 
function onConfirm(e: any) {
    emit('confirm', e.detail.value);
}
 
function onClear(event: any) {
    try {
        event.stopPropagation();
    } catch (e) {
        console.log(e);
    }
    handleInput({ detail: { value: '' } });
}
 
function inputClick() {
    emit('click');
}
 
defineExpose({
    onFormItemError
});
</script>
 
<style lang="scss" scoped>
@import '../../libs/css/style.components.scss';
 
.u-input {
    position: relative;
    flex: 1;
    @include vue-flex;
 
    &__input {
        //height: 70rpx;
        font-size: 28rpx;
        color: $u-main-color;
        flex: 1;
    }
 
    &__textarea {
        width: auto;
        font-size: 28rpx;
        color: $u-main-color;
        // padding: 10rpx 0;
        line-height: normal;
        flex: 1;
    }
 
    &__count {
        position: absolute;
        right: 1px;
        bottom: 0;
        font-size: 12px;
        color: $u-tips-color;
        background-color: var(--u-bg-white);
        padding: 1px 4px;
        border-radius: 10px;
        line-height: 16px;
    }
 
    &--border {
        border-radius: 4px;
        border: 1px solid $u-border-color;
    }
 
    &--error {
        border-color: $u-type-error !important;
    }
 
    &__right-icon {
        &__item {
            margin-left: 10rpx;
        }
 
        &--select {
            transition: transform 0.4s;
 
            &--reverse {
                transform: rotate(-180deg);
            }
        }
    }
}
</style>