uview-pro 0.3.6 → 0.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/changelog.md +24 -0
- package/components/u-checkbox/u-checkbox.vue +1 -1
- package/components/u-form/u-form.vue +14 -9
- package/components/u-form-item/u-form-item.vue +9 -6
- package/components/u-index-list/u-index-list.vue +2 -2
- package/components/u-input/types.ts +5 -0
- package/components/u-input/u-input.vue +28 -8
- package/components/u-textarea/types.ts +88 -0
- package/components/u-textarea/u-textarea.vue +339 -0
- package/package.json +1 -1
- package/types/components.d.ts +1 -0
package/changelog.md
CHANGED
|
@@ -1,3 +1,27 @@
|
|
|
1
|
+
## 0.3.7(2025-10-28)
|
|
2
|
+
|
|
3
|
+
### 🐛 Bug Fixes | Bug 修复
|
|
4
|
+
|
|
5
|
+
- **u-index-list:** 修复 u-index-list 组件中 indexList 的响应式,确保数据正确对应,滚动到正确的锚点 ([536479b](https://github.com/anyup/uView-Pro/commit/536479b0c444f50892c789191265b373297ec0b5))
|
|
6
|
+
- **u-form:** 优化 u-form-item 组件样式,修复微信小程序光标样式偏移问题 ([7e6694b](https://github.com/anyup/uView-Pro/commit/7e6694b5db11f654b081d10bb83d53c99437c730))
|
|
7
|
+
- **u-form:** 优化 model 属性的响应式更新,修复 model 对象整体替换导致的表单校验失效问题 ([bc49b5b](https://github.com/anyup/uView-Pro/commit/bc49b5b2fa6f51c803d64252184f5eba93a0d3c1))
|
|
8
|
+
- **u-form:** 修复 u-form 的重置表单方法 resetFields 失效问题 ([a31f800](https://github.com/anyup/uView-Pro/commit/a31f800e206fa9671bdd0d6a46ca7a4afba6aefd))
|
|
9
|
+
- **u-checkbox:** 修复 u-checkbox 单独使用报错的问题 ([dad2832](https://github.com/anyup/uView-Pro/commit/dad2832a2e7219b0454ec97970c9cb8f4fac0625))
|
|
10
|
+
|
|
11
|
+
### ✨ Features | 新功能
|
|
12
|
+
|
|
13
|
+
- **u-textarea:** 新增 u-textarea 组件及演示示例,增强 textarea 功能,分离 u-input 的 textarea 模式 ([efbb75e](https://github.com/anyup/uView-Pro/commit/efbb75e21a044a664267930396ce496fd64d33fe))
|
|
14
|
+
- **u-input:** 增强 u-input 的 textarea 字数统计功能 ([5e14354](https://github.com/anyup/uView-Pro/commit/5e14354a2e34768650cd2417f81b14cdbbced132))
|
|
15
|
+
- **u-form:** 增强表单校验功能,发射校验错误,便于自定义提示 ([a553b53](https://github.com/anyup/uView-Pro/commit/a553b53581cc470938030c0f1f8d5f7671d8c25a))
|
|
16
|
+
|
|
17
|
+
### ♻️ Code Refactoring | 代码重构
|
|
18
|
+
|
|
19
|
+
- **form:** 优化表单组件的提示文本 ([04b5fc7](https://github.com/anyup/uView-Pro/commit/04b5fc7a071835425786958c5f6de04e233c151c))
|
|
20
|
+
|
|
21
|
+
### 👥 Contributors
|
|
22
|
+
|
|
23
|
+
<a href="https://github.com/anyup"><img src="https://github.com/anyup.png?size=40" width="40" height="40" alt="anyup" title="anyup"/></a>
|
|
24
|
+
|
|
1
25
|
## 0.3.6(2025-10-24)
|
|
2
26
|
|
|
3
27
|
### ✨ Features | 新功能
|
|
@@ -180,7 +180,7 @@ function setValue() {
|
|
|
180
180
|
emitEvent();
|
|
181
181
|
emit('update:modelValue', false);
|
|
182
182
|
} else {
|
|
183
|
-
if (!parentExposed
|
|
183
|
+
if (parentExposed?.value?.validateSelection && !parentExposed?.value?.validateSelection()) {
|
|
184
184
|
return;
|
|
185
185
|
}
|
|
186
186
|
emitEvent();
|
|
@@ -19,7 +19,7 @@ export default {
|
|
|
19
19
|
|
|
20
20
|
<script setup lang="ts">
|
|
21
21
|
import { FormProps } from './types';
|
|
22
|
-
import { ref } from 'vue';
|
|
22
|
+
import { computed, ref } from 'vue';
|
|
23
23
|
import { $u, useParent } from '../..';
|
|
24
24
|
|
|
25
25
|
/**
|
|
@@ -41,6 +41,11 @@ const props = defineProps(FormProps);
|
|
|
41
41
|
|
|
42
42
|
useParent('u-form');
|
|
43
43
|
|
|
44
|
+
interface ErrorItem {
|
|
45
|
+
prop: string;
|
|
46
|
+
message: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
44
49
|
// 存储当前form下的所有u-form-item的实例
|
|
45
50
|
const fields = ref<any[]>([]);
|
|
46
51
|
|
|
@@ -69,15 +74,15 @@ function resetFields() {
|
|
|
69
74
|
* @param callback 校验回调
|
|
70
75
|
* @returns Promise<boolean>
|
|
71
76
|
*/
|
|
72
|
-
function validate(callback?: (valid: boolean) => void): Promise<boolean> {
|
|
77
|
+
function validate(callback?: (valid: boolean, errorArr: ErrorItem[]) => void): Promise<boolean> {
|
|
73
78
|
return new Promise(resolve => {
|
|
74
79
|
// 对所有的u-form-item进行校验
|
|
75
80
|
let valid = true; // 默认通过
|
|
76
81
|
let count = 0; // 用于标记是否检查完毕
|
|
77
|
-
let errorArr:
|
|
82
|
+
let errorArr: ErrorItem[] = []; // 存放错误信息
|
|
78
83
|
if (fields.value.length === 0) {
|
|
79
84
|
resolve(true);
|
|
80
|
-
if (typeof callback === 'function') callback(true);
|
|
85
|
+
if (typeof callback === 'function') callback(true, []);
|
|
81
86
|
return;
|
|
82
87
|
}
|
|
83
88
|
// 调用每一个u-form-item实例的validation的校验方法
|
|
@@ -87,7 +92,7 @@ function validate(callback?: (valid: boolean) => void): Promise<boolean> {
|
|
|
87
92
|
field.validation('', (error: any) => {
|
|
88
93
|
if (error) {
|
|
89
94
|
valid = false;
|
|
90
|
-
errorArr.push(error);
|
|
95
|
+
errorArr.push({ prop: field.prop, message: error });
|
|
91
96
|
}
|
|
92
97
|
// 当历遍了所有的u-form-item时,调用promise的then方法
|
|
93
98
|
if (++count === fields.value.length) {
|
|
@@ -96,12 +101,12 @@ function validate(callback?: (valid: boolean) => void): Promise<boolean> {
|
|
|
96
101
|
if (
|
|
97
102
|
props.errorType.indexOf('none') === -1 &&
|
|
98
103
|
props.errorType.indexOf('toast') >= 0 &&
|
|
99
|
-
errorArr.length
|
|
104
|
+
errorArr.length > 0
|
|
100
105
|
) {
|
|
101
|
-
$u.toast(errorArr[0]);
|
|
106
|
+
errorArr[0].message && $u.toast(errorArr[0].message);
|
|
102
107
|
}
|
|
103
108
|
// 调用回调方法
|
|
104
|
-
if (typeof callback === 'function') callback(valid);
|
|
109
|
+
if (typeof callback === 'function') callback(valid, errorArr);
|
|
105
110
|
}
|
|
106
111
|
});
|
|
107
112
|
});
|
|
@@ -121,7 +126,7 @@ defineExpose({
|
|
|
121
126
|
fields,
|
|
122
127
|
rules,
|
|
123
128
|
props,
|
|
124
|
-
model: props.model
|
|
129
|
+
model: computed(() => props.model)
|
|
125
130
|
});
|
|
126
131
|
</script>
|
|
127
132
|
|
|
@@ -269,7 +269,7 @@ function getFilteredRule(triggerType = '') {
|
|
|
269
269
|
*/
|
|
270
270
|
function validation(trigger: string, callback: (msg: string) => void = () => {}) {
|
|
271
271
|
// 检验之前,先获取需要校验的值
|
|
272
|
-
fieldValue.value = parentExposed?.value?.model?.[props.prop];
|
|
272
|
+
fieldValue.value = parentExposed?.value?.model?.value[props.prop];
|
|
273
273
|
// blur和change是否有当前方式的校验规则
|
|
274
274
|
let rules = getFilteredRule(trigger);
|
|
275
275
|
// 判断是否有验证规则,如果没有规则,也调用回调方法,否则父组件u-form会因为
|
|
@@ -295,11 +295,14 @@ function validation(trigger: string, callback: (msg: string) => void = () => {})
|
|
|
295
295
|
* 清空当前的u-form-item
|
|
296
296
|
*/
|
|
297
297
|
function resetField() {
|
|
298
|
-
if (parentExposed?.value?.model && props.prop) {
|
|
299
|
-
parentExposed.value.model[props.prop] = initialValue.value;
|
|
298
|
+
if (parentExposed?.value?.model?.value && props.prop) {
|
|
299
|
+
parentExposed.value.model.value[props.prop] = initialValue.value;
|
|
300
300
|
}
|
|
301
301
|
// 设置为`success`状态,只是为了清空错误标记
|
|
302
|
-
|
|
302
|
+
// 延时50毫秒,如果立即清空状态,则无法清空错误标记
|
|
303
|
+
setTimeout(() => {
|
|
304
|
+
validateState.value = 'success';
|
|
305
|
+
}, 50);
|
|
303
306
|
}
|
|
304
307
|
|
|
305
308
|
// 组件挂载时注册到父表单
|
|
@@ -322,7 +325,7 @@ onMounted(() => {
|
|
|
322
325
|
});
|
|
323
326
|
errorType.value = parentExposed?.value?.errorType || errorType.value;
|
|
324
327
|
// 设置初始值
|
|
325
|
-
fieldValue.value = parentExposed?.value?.model?.[props.prop];
|
|
328
|
+
fieldValue.value = parentExposed?.value?.model?.value[props.prop];
|
|
326
329
|
// 设置初始值
|
|
327
330
|
initialValue.value = fieldValue.value;
|
|
328
331
|
}
|
|
@@ -355,7 +358,7 @@ defineExpose({
|
|
|
355
358
|
font-size: 28rpx;
|
|
356
359
|
color: $u-main-color;
|
|
357
360
|
box-sizing: border-box;
|
|
358
|
-
line-height: $u-form-item-height;
|
|
361
|
+
// line-height: $u-form-item-height;
|
|
359
362
|
flex-direction: column;
|
|
360
363
|
|
|
361
364
|
&__border-bottom--error:after {
|
|
@@ -95,7 +95,7 @@ const stickyOffsetTop = ref(0);
|
|
|
95
95
|
// 弹出toast的z-index值
|
|
96
96
|
const alertZIndex = computed(() => $u.zIndex.toast).value;
|
|
97
97
|
// indexList 响应式
|
|
98
|
-
const indexList = computed(() => props
|
|
98
|
+
const indexList = computed(() => props?.indexList ?? getIndexList());
|
|
99
99
|
const zIndex = computed(() => props.zIndex).value;
|
|
100
100
|
const activeColor = computed(() => props.activeColor).value;
|
|
101
101
|
|
|
@@ -278,7 +278,7 @@ function scrollToAnchor(index: number) {
|
|
|
278
278
|
return;
|
|
279
279
|
}
|
|
280
280
|
scrollToAnchorIndex.value = index;
|
|
281
|
-
const anchor = children.find(item => item.getExposed().props.index === indexList[index]);
|
|
281
|
+
const anchor = children.find(item => item.getExposed().props.index === indexList.value[index]);
|
|
282
282
|
if (anchor) {
|
|
283
283
|
emit('select', anchor.getExposed().props.index);
|
|
284
284
|
uni.pageScrollTo({
|
|
@@ -39,6 +39,11 @@ export const InputProps = {
|
|
|
39
39
|
type: [Number, String] as PropType<number | string>,
|
|
40
40
|
default: 140
|
|
41
41
|
},
|
|
42
|
+
// 是否显示统计字数,仅textarea有效
|
|
43
|
+
count: {
|
|
44
|
+
type: Boolean,
|
|
45
|
+
default: false
|
|
46
|
+
},
|
|
42
47
|
/** placeholder的样式,字符串形式,如"color: red;"(默认 "color: #c0c4cc;") */
|
|
43
48
|
placeholderStyle: {
|
|
44
49
|
type: String,
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
customClass
|
|
10
10
|
]"
|
|
11
11
|
:style="{
|
|
12
|
-
padding: `0 ${border ? 20 : 0}rpx`,
|
|
12
|
+
padding: type === 'textarea' ? (border ? '20rpx' : '0') : `0 ${border ? 20 : 0}rpx`,
|
|
13
13
|
borderColor: borderColor,
|
|
14
14
|
textAlign: inputAlign
|
|
15
15
|
}"
|
|
@@ -88,6 +88,15 @@
|
|
|
88
88
|
<u-icon name="arrow-down-fill" size="26" color="#c0c4cc"></u-icon>
|
|
89
89
|
</view>
|
|
90
90
|
</view>
|
|
91
|
+
<text
|
|
92
|
+
class="u-input__count"
|
|
93
|
+
:style="{
|
|
94
|
+
'background-color': props.disabled ? 'transparent' : '#fff'
|
|
95
|
+
}"
|
|
96
|
+
v-if="props.type === 'textarea' && props.count"
|
|
97
|
+
>
|
|
98
|
+
{{ String(defaultValue).length }}/{{ props.maxlength }}
|
|
99
|
+
</text>
|
|
91
100
|
</view>
|
|
92
101
|
</template>
|
|
93
102
|
|
|
@@ -167,10 +176,10 @@ function handleInput(event: any) {
|
|
|
167
176
|
let value = event.detail.value;
|
|
168
177
|
// 判断是否去除空格
|
|
169
178
|
if (props.trim) value = $u.trim(value);
|
|
170
|
-
emit('update:modelValue', value);
|
|
171
|
-
emit('input', value);
|
|
172
179
|
// 当前model 赋值
|
|
173
180
|
defaultValue.value = value;
|
|
181
|
+
emit('update:modelValue', value);
|
|
182
|
+
emit('input', value);
|
|
174
183
|
// 过一个生命周期再发送事件给u-form-item,否则this.$emit('update:modelValue')更新了父组件的值,但是微信小程序上
|
|
175
184
|
// 尚未更新到u-form-item,导致获取的值为空,从而校验混论
|
|
176
185
|
// 这里不能延时时间太短,或者使用this.$nextTick,否则在头条上,会造成混乱
|
|
@@ -192,12 +201,12 @@ function handleInput(event: any) {
|
|
|
192
201
|
function handleBlur(event: any) {
|
|
193
202
|
// 最开始使用的是监听图标@touchstart事件,自从hx2.8.4后,此方法在微信小程序出错
|
|
194
203
|
// 这里改为监听点击事件,手点击清除图标时,同时也发生了@blur事件,导致图标消失而无法点击,这里做一个延时
|
|
195
|
-
let value = event.detail.value;
|
|
196
204
|
setTimeout(() => {
|
|
197
205
|
focused.value = false;
|
|
198
206
|
}, 100);
|
|
199
|
-
emit('blur', value);
|
|
200
207
|
setTimeout(() => {
|
|
208
|
+
let value = String(defaultValue.value);
|
|
209
|
+
emit('blur', value);
|
|
201
210
|
// 头条小程序由于自身bug,导致中文下,每按下一个键(尚未完成输入),都会触发一次@input,导致错误,这里进行判断处理
|
|
202
211
|
// #ifdef MP-TOUTIAO
|
|
203
212
|
if ($u.trim(value) == lastValue.value) return;
|
|
@@ -226,7 +235,7 @@ function onClear(event: any) {
|
|
|
226
235
|
} catch (e) {
|
|
227
236
|
console.log(e);
|
|
228
237
|
}
|
|
229
|
-
|
|
238
|
+
handleInput({ detail: { value: '' } });
|
|
230
239
|
}
|
|
231
240
|
|
|
232
241
|
function inputClick() {
|
|
@@ -257,13 +266,24 @@ defineExpose({
|
|
|
257
266
|
width: auto;
|
|
258
267
|
font-size: 28rpx;
|
|
259
268
|
color: $u-main-color;
|
|
260
|
-
padding: 10rpx 0;
|
|
269
|
+
// padding: 10rpx 0;
|
|
261
270
|
line-height: normal;
|
|
262
271
|
flex: 1;
|
|
263
272
|
}
|
|
264
273
|
|
|
274
|
+
&__count {
|
|
275
|
+
position: absolute;
|
|
276
|
+
right: 1px;
|
|
277
|
+
bottom: 0;
|
|
278
|
+
font-size: 12px;
|
|
279
|
+
color: $u-tips-color;
|
|
280
|
+
background-color: #ffffff;
|
|
281
|
+
padding: 1px 4px;
|
|
282
|
+
border-radius: 10px;
|
|
283
|
+
line-height: 16px;
|
|
284
|
+
}
|
|
285
|
+
|
|
265
286
|
&--border {
|
|
266
|
-
border-radius: 6rpx;
|
|
267
287
|
border-radius: 4px;
|
|
268
288
|
border: 1px solid $u-form-item-border-color;
|
|
269
289
|
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { ExtractPropTypes, PropType } from 'vue';
|
|
2
|
+
import { baseProps } from '../common/props';
|
|
3
|
+
|
|
4
|
+
const textarea = {
|
|
5
|
+
value: '',
|
|
6
|
+
placeholder: '',
|
|
7
|
+
placeholderClass: 'input-placeholder',
|
|
8
|
+
placeholderStyle: 'color: #c0c4cc',
|
|
9
|
+
height: '100rpx',
|
|
10
|
+
confirmType: 'done',
|
|
11
|
+
disabled: false,
|
|
12
|
+
count: false,
|
|
13
|
+
focus: false,
|
|
14
|
+
autoHeight: false,
|
|
15
|
+
fixed: false,
|
|
16
|
+
cursorSpacing: 0,
|
|
17
|
+
cursor: '',
|
|
18
|
+
showConfirmBar: true,
|
|
19
|
+
selectionStart: -1,
|
|
20
|
+
selectionEnd: -1,
|
|
21
|
+
adjustPosition: true,
|
|
22
|
+
disableDefaultPadding: false,
|
|
23
|
+
holdKeyboard: false,
|
|
24
|
+
maxlength: 140,
|
|
25
|
+
border: 'surround',
|
|
26
|
+
formatter: null
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const TextareaProps = {
|
|
30
|
+
...baseProps,
|
|
31
|
+
// 输入框的内容
|
|
32
|
+
modelValue: { type: [String, Number] as PropType<string | number>, default: textarea.value },
|
|
33
|
+
// 输入框为空时占位符
|
|
34
|
+
placeholder: { type: [String, Number] as PropType<string | number>, default: textarea.placeholder },
|
|
35
|
+
// 指定placeholder的样式类,注意页面或组件的style中写了scoped时,需要在类名前写/deep/
|
|
36
|
+
placeholderClass: { type: String as PropType<string>, default: textarea.placeholderClass },
|
|
37
|
+
// 指定placeholder的样式
|
|
38
|
+
placeholderStyle: {
|
|
39
|
+
type: [String, Object] as PropType<string | Record<string, any>>,
|
|
40
|
+
default: textarea.placeholderStyle
|
|
41
|
+
},
|
|
42
|
+
// 输入框高度
|
|
43
|
+
height: { type: [String, Number] as PropType<string | number>, default: textarea.height },
|
|
44
|
+
// 设置键盘右下角按钮的文字,仅微信小程序,App-vue和H5有效
|
|
45
|
+
confirmType: { type: String as PropType<string>, default: textarea.confirmType },
|
|
46
|
+
// 是否禁用
|
|
47
|
+
disabled: { type: Boolean as PropType<boolean>, default: textarea.disabled },
|
|
48
|
+
// 是否显示统计字数
|
|
49
|
+
count: { type: Boolean as PropType<boolean>, default: textarea.count },
|
|
50
|
+
// 是否自动获取焦点,nvue不支持,H5取决于浏览器的实现
|
|
51
|
+
focus: { type: Boolean as PropType<boolean>, default: textarea.focus },
|
|
52
|
+
// 是否自动增加高度
|
|
53
|
+
autoHeight: { type: Boolean as PropType<boolean>, default: textarea.autoHeight },
|
|
54
|
+
// 如果textarea是在一个position:fixed的区域,需要显示指定属性fixed为true
|
|
55
|
+
fixed: { type: Boolean as PropType<boolean>, default: textarea.fixed },
|
|
56
|
+
// 指定光标与键盘的距离
|
|
57
|
+
cursorSpacing: { type: Number as PropType<number>, default: textarea.cursorSpacing },
|
|
58
|
+
// 指定focus时的光标位置
|
|
59
|
+
cursor: { type: [String, Number] as PropType<string | number>, default: textarea.cursor },
|
|
60
|
+
// 是否显示键盘上方带有”完成“按钮那一栏,
|
|
61
|
+
showConfirmBar: { type: Boolean as PropType<boolean>, default: textarea.showConfirmBar },
|
|
62
|
+
// 光标起始位置,自动聚焦时有效,需与selection-end搭配使用
|
|
63
|
+
selectionStart: { type: Number as PropType<number>, default: textarea.selectionStart },
|
|
64
|
+
// 光标结束位置,自动聚焦时有效,需与selection-start搭配使用
|
|
65
|
+
selectionEnd: { type: Number as PropType<number>, default: textarea.selectionEnd },
|
|
66
|
+
// 键盘弹起时,是否自动上推页面
|
|
67
|
+
adjustPosition: { type: Boolean as PropType<boolean>, default: textarea.adjustPosition },
|
|
68
|
+
// 是否去掉 iOS 下的默认内边距,只微信小程序有效
|
|
69
|
+
disableDefaultPadding: { type: Boolean as PropType<boolean>, default: textarea.disableDefaultPadding },
|
|
70
|
+
// focus时,点击页面的时候不收起键盘,只微信小程序有效
|
|
71
|
+
holdKeyboard: { type: Boolean as PropType<boolean>, default: textarea.holdKeyboard },
|
|
72
|
+
// 最大输入长度,设置为 -1 的时候不限制最大长度
|
|
73
|
+
maxlength: { type: [String, Number] as PropType<string | number>, default: textarea.maxlength },
|
|
74
|
+
// 边框类型,surround-四周边框,bottom-底部边框
|
|
75
|
+
border: { type: [String, Boolean] as PropType<string | boolean>, default: textarea.border },
|
|
76
|
+
// 用于处理或者过滤输入框内容的方法
|
|
77
|
+
formatter: { type: Function as PropType<((val: any) => any) | null>, default: textarea.formatter },
|
|
78
|
+
// 是否忽略组件内对文本合成系统事件的处理
|
|
79
|
+
ignoreCompositionEvent: { type: Boolean as PropType<boolean>, default: true },
|
|
80
|
+
/** 是否可清空(默认true) */
|
|
81
|
+
clearable: { type: Boolean, default: true },
|
|
82
|
+
/** 输入框的验证状态,用于错误时,边框是否改为红色 */
|
|
83
|
+
validateState: { type: Boolean, default: false }
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export type TextareaProps = ExtractPropTypes<typeof TextareaProps>;
|
|
87
|
+
|
|
88
|
+
export default TextareaProps;
|
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<view
|
|
3
|
+
class="u-textarea"
|
|
4
|
+
:class="[
|
|
5
|
+
{
|
|
6
|
+
'u-textarea--error': validateState
|
|
7
|
+
},
|
|
8
|
+
textareaClass,
|
|
9
|
+
customClass
|
|
10
|
+
]"
|
|
11
|
+
:style="$u.toStyle(textareaStyle, customStyle)"
|
|
12
|
+
>
|
|
13
|
+
<textarea
|
|
14
|
+
class="u-textarea__field"
|
|
15
|
+
:value="innerValue"
|
|
16
|
+
:style="getStyle"
|
|
17
|
+
:placeholder="props.placeholder"
|
|
18
|
+
:placeholder-style="$u.toStyle(props.placeholderStyle)"
|
|
19
|
+
:placeholder-class="props.placeholderClass"
|
|
20
|
+
:disabled="props.disabled"
|
|
21
|
+
:focus="props.focus"
|
|
22
|
+
:autoHeight="props.autoHeight"
|
|
23
|
+
:fixed="props.fixed"
|
|
24
|
+
:cursorSpacing="props.cursorSpacing"
|
|
25
|
+
:cursor="props.cursor"
|
|
26
|
+
:showConfirmBar="props.showConfirmBar"
|
|
27
|
+
:selectionStart="props.selectionStart"
|
|
28
|
+
:selectionEnd="props.selectionEnd"
|
|
29
|
+
:adjustPosition="props.adjustPosition"
|
|
30
|
+
:disableDefaultPadding="props.disableDefaultPadding"
|
|
31
|
+
:holdKeyboard="props.holdKeyboard"
|
|
32
|
+
:maxlength="props.maxlength"
|
|
33
|
+
:confirmType="props.confirmType"
|
|
34
|
+
:ignoreCompositionEvent="props.ignoreCompositionEvent"
|
|
35
|
+
@focus="onFocus"
|
|
36
|
+
@blur="onBlur"
|
|
37
|
+
@linechange="onLinechange"
|
|
38
|
+
@input="onInput"
|
|
39
|
+
@confirm="onConfirm"
|
|
40
|
+
@keyboardheightchange="onKeyboardheightchange"
|
|
41
|
+
></textarea>
|
|
42
|
+
<text
|
|
43
|
+
class="u-textarea__count"
|
|
44
|
+
:style="{
|
|
45
|
+
'background-color': props.disabled ? 'transparent' : '#fff'
|
|
46
|
+
}"
|
|
47
|
+
v-if="props.count"
|
|
48
|
+
>
|
|
49
|
+
{{ innerValue.length }}/{{ props.maxlength }}
|
|
50
|
+
</text>
|
|
51
|
+
|
|
52
|
+
<view class="u-textarea__right-icon u-flex">
|
|
53
|
+
<view
|
|
54
|
+
class="u-textarea__right-icon__clear u-textarea__right-icon__item"
|
|
55
|
+
v-if="clearable && modelValue != '' && !disabled"
|
|
56
|
+
>
|
|
57
|
+
<u-icon size="32" name="close-circle-fill" color="#c0c4cc" @click="onClear" />
|
|
58
|
+
</view>
|
|
59
|
+
</view>
|
|
60
|
+
</view>
|
|
61
|
+
</template>
|
|
62
|
+
|
|
63
|
+
<script lang="ts">
|
|
64
|
+
export default {
|
|
65
|
+
name: 'u-textarea',
|
|
66
|
+
options: {
|
|
67
|
+
addGlobalClass: true,
|
|
68
|
+
// #ifndef MP-TOUTIAO
|
|
69
|
+
virtualHost: true,
|
|
70
|
+
// #endif
|
|
71
|
+
styleIsolation: 'shared'
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
</script>
|
|
75
|
+
|
|
76
|
+
<script setup lang="ts">
|
|
77
|
+
import { ref, computed, watch, nextTick } from 'vue';
|
|
78
|
+
import { TextareaProps } from './types';
|
|
79
|
+
import { $u, useChildren } from '../../';
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Textarea 文本域
|
|
83
|
+
* @description 文本域此组件满足了可能出现的表单信息补充,编辑等实际逻辑的功能,内置了字数校验等
|
|
84
|
+
* @tutorial https://uviewpro.cn/zh/components/textarea.html
|
|
85
|
+
* @property {String | Number} value 输入框的内容
|
|
86
|
+
* @property {String | Number} placeholder 输入框为空时占位符
|
|
87
|
+
* @property {String} placeholderClass 指定placeholder的样式类,注意页面或组件的style中写了scoped时,需要在类名前写/deep/ ( 默认 'input-placeholder' )
|
|
88
|
+
* @property {String | Object} placeholderStyle 指定placeholder的样式,字符串/对象形式,如"color: red;"
|
|
89
|
+
* @property {String | Number} height 输入框高度(默认 70 )
|
|
90
|
+
* @property {String} confirmType 设置键盘右下角按钮的文字,仅微信小程序,App-vue和H5有效(默认 'done' )
|
|
91
|
+
* @property {Boolean} disabled 是否禁用(默认 false )
|
|
92
|
+
* @property {Boolean} count 是否显示统计字数(默认 false )
|
|
93
|
+
* @property {Boolean} focus 是否自动获取焦点,nvue不支持,H5取决于浏览器的实现(默认 false )
|
|
94
|
+
* @property {Boolean | Function} autoHeight 是否自动增加高度(默认 false )
|
|
95
|
+
* @property {Boolean} fixed 如果textarea是在一个position:fixed的区域,需要显示指定属性fixed为true(默认 false )
|
|
96
|
+
* @property {Number} cursorSpacing 指定光标与键盘的距离(默认 0 )
|
|
97
|
+
* @property {String | Number} cursor 指定focus时的光标位置
|
|
98
|
+
* @property {Function} formatter 内容式化函数
|
|
99
|
+
* @property {Boolean} showConfirmBar 是否显示键盘上方带有”完成“按钮那一栏,(默认 true )
|
|
100
|
+
* @property {Number} selectionStart 光标起始位置,自动聚焦时有效,需与selection-end搭配使用,(默认 -1 )
|
|
101
|
+
* @property {Number | Number} selectionEnd 光标结束位置,自动聚焦时有效,需与selection-start搭配使用(默认 -1 )
|
|
102
|
+
* @property {Boolean} adjustPosition 键盘弹起时,是否自动上推页面(默认 true )
|
|
103
|
+
* @property {Boolean | Number} disableDefaultPadding 是否去掉 iOS 下的默认内边距,只微信小程序有效(默认 false )
|
|
104
|
+
* @property {Boolean} holdKeyboard focus时,点击页面的时候不收起键盘,只微信小程序有效(默认 false )
|
|
105
|
+
* @property {String | Number} maxlength 最大输入长度,设置为 -1 的时候不限制最大长度(默认 140 )
|
|
106
|
+
* @property {String} border 边框类型,surround-四周边框,none-无边框,bottom-底部边框(默认 'surround' )
|
|
107
|
+
* @property {Boolean} ignoreCompositionEvent 是否忽略组件内对文本合成系统事件的处理
|
|
108
|
+
* @event {Function(e)} focus 输入框聚焦时触发,event.detail = { value, height },height 为键盘高度
|
|
109
|
+
* @event {Function(e)} blur 输入框失去焦点时触发,event.detail = {value, cursor}
|
|
110
|
+
* @event {Function(e)} linechange 输入框行数变化时调用,event.detail = {height: 0, heightRpx: 0, lineCount: 0}
|
|
111
|
+
* @event {Function(e)} input 当键盘输入时,触发 input 事件
|
|
112
|
+
* @event {Function(e)} confirm 点击完成时, 触发 confirm 事件
|
|
113
|
+
* @event {Function(e)} keyboardheightchange 键盘高度发生变化的时候触发此事件
|
|
114
|
+
* @example <u-textarea v-model="value1" placeholder="请输入内容" ></u-textarea>
|
|
115
|
+
*/
|
|
116
|
+
|
|
117
|
+
const props = defineProps(TextareaProps);
|
|
118
|
+
const emit = defineEmits([
|
|
119
|
+
'update:modelValue',
|
|
120
|
+
'focus',
|
|
121
|
+
'blur',
|
|
122
|
+
'linechange',
|
|
123
|
+
'input',
|
|
124
|
+
'confirm',
|
|
125
|
+
'keyboardheightchange',
|
|
126
|
+
'change'
|
|
127
|
+
]);
|
|
128
|
+
|
|
129
|
+
const { emitToParent } = useChildren('u-textarea', 'u-form-item');
|
|
130
|
+
|
|
131
|
+
// state
|
|
132
|
+
const innerValue = ref('');
|
|
133
|
+
const focused = ref(false);
|
|
134
|
+
const firstChange = ref(true);
|
|
135
|
+
const changeFromInner = ref(false);
|
|
136
|
+
const innerFormatter = ref((v: any) => v);
|
|
137
|
+
const validateState = ref(props.validateState); // 当前input的验证状态,用于错误时,边框是否改为红色
|
|
138
|
+
|
|
139
|
+
// watch value prop
|
|
140
|
+
watch(
|
|
141
|
+
() => props.modelValue,
|
|
142
|
+
(newVal: any) => {
|
|
143
|
+
innerValue.value = newVal;
|
|
144
|
+
/* #ifdef H5 */
|
|
145
|
+
// 在H5中,外部value变化后,修改input中的值,不会触发@input事件,此时手动调用值变化方法
|
|
146
|
+
if (firstChange.value === false && changeFromInner.value === false) {
|
|
147
|
+
valueChange();
|
|
148
|
+
}
|
|
149
|
+
/* #endif */
|
|
150
|
+
firstChange.value = false;
|
|
151
|
+
// 重置changeFromInner的值为false,标识下一次引起默认为外部引起的
|
|
152
|
+
changeFromInner.value = false;
|
|
153
|
+
},
|
|
154
|
+
{ immediate: true }
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
// 监听 validateState 变化
|
|
158
|
+
watch(
|
|
159
|
+
() => props.validateState,
|
|
160
|
+
val => {
|
|
161
|
+
validateState.value = val;
|
|
162
|
+
}
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
// 组件的类名
|
|
166
|
+
const textareaClass = computed(() => {
|
|
167
|
+
let classes: string[] = [];
|
|
168
|
+
if (props.border) {
|
|
169
|
+
if (props.border === 'surround') {
|
|
170
|
+
classes = classes.concat(['u-textarea--border', 'u-textarea--radius']);
|
|
171
|
+
} else if (props.border === 'bottom') {
|
|
172
|
+
classes = classes.concat(['u-border-bottom', 'u-textarea--no-radius']);
|
|
173
|
+
} else {
|
|
174
|
+
classes = classes.concat(['u-textarea--border', 'u-textarea--radius']);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
props.disabled && classes.push('u-textarea--disabled');
|
|
178
|
+
return classes.join(' ');
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
// 组件的样式
|
|
182
|
+
const textareaStyle = computed(() => {
|
|
183
|
+
const style: Record<string, any> = {};
|
|
184
|
+
if (props.border) {
|
|
185
|
+
style.padding = `${props.border ? 20 : 0}rpx`;
|
|
186
|
+
}
|
|
187
|
+
// #ifdef APP-NVUE
|
|
188
|
+
if ($u.os() === 'android') {
|
|
189
|
+
style.paddingTop = '6px';
|
|
190
|
+
style.paddingLeft = '9px';
|
|
191
|
+
style.paddingBottom = '3px';
|
|
192
|
+
style.paddingRight = '6px';
|
|
193
|
+
}
|
|
194
|
+
// #endif
|
|
195
|
+
return style;
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
const getStyle = computed(() => {
|
|
199
|
+
let style: Record<string, any> = {};
|
|
200
|
+
// 如果没有自定义高度,就根据textarea来分配一个默认的高度
|
|
201
|
+
if (props.autoHeight) {
|
|
202
|
+
style.minHeight = $u.addUnit(props.height || '100rpx');
|
|
203
|
+
style.height = 'auto';
|
|
204
|
+
} else {
|
|
205
|
+
style.height = $u.addUnit(props.height);
|
|
206
|
+
}
|
|
207
|
+
return $u.toStyle(style, props.customStyle);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
function onFormItemError(status: boolean) {
|
|
211
|
+
validateState.value = status;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// methods
|
|
215
|
+
function setFormatter(e: any) {
|
|
216
|
+
innerFormatter.value = e;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function onFocus(e: any) {
|
|
220
|
+
emit('focus', e);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function onBlur(e: any) {
|
|
224
|
+
setTimeout(() => {
|
|
225
|
+
e.detail.value = innerValue.value;
|
|
226
|
+
let value = e.detail.value;
|
|
227
|
+
emit('blur', e);
|
|
228
|
+
emitToParent('onFormBlur', value);
|
|
229
|
+
}, 40);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function onLinechange(e: any) {
|
|
233
|
+
emit('linechange', e);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function onInput(e: any) {
|
|
237
|
+
let { value = '' } = e.detail || {};
|
|
238
|
+
// 格式化过滤方法
|
|
239
|
+
const formatter = props.formatter || innerFormatter.value;
|
|
240
|
+
const formatValue = typeof formatter === 'function' ? formatter(value) : value;
|
|
241
|
+
// 为了避免props的单向数据流特性,需要先将innerValue值设置为当前值,再在$nextTick中重新赋予设置后的值才有效
|
|
242
|
+
innerValue.value = value;
|
|
243
|
+
nextTick(() => {
|
|
244
|
+
innerValue.value = formatValue;
|
|
245
|
+
valueChange();
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function valueChange() {
|
|
250
|
+
const value = innerValue.value;
|
|
251
|
+
nextTick(() => {
|
|
252
|
+
emit('input', value);
|
|
253
|
+
emit('update:modelValue', value);
|
|
254
|
+
// 标识value值的变化是由内部引起的
|
|
255
|
+
changeFromInner.value = true;
|
|
256
|
+
emit('change', value);
|
|
257
|
+
emitToParent('onFormChange', value);
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function onConfirm(e: any) {
|
|
262
|
+
emit('confirm', e);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function onKeyboardheightchange(e: any) {
|
|
266
|
+
emit('keyboardheightchange', e);
|
|
267
|
+
}
|
|
268
|
+
function onClear(event: any) {
|
|
269
|
+
try {
|
|
270
|
+
event.stopPropagation();
|
|
271
|
+
} catch (e) {
|
|
272
|
+
console.log(e);
|
|
273
|
+
}
|
|
274
|
+
innerValue.value = '';
|
|
275
|
+
valueChange();
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
defineExpose({
|
|
279
|
+
onFormItemError
|
|
280
|
+
});
|
|
281
|
+
</script>
|
|
282
|
+
|
|
283
|
+
<style lang="scss" scoped>
|
|
284
|
+
@import '../../libs/css/style.components.scss';
|
|
285
|
+
|
|
286
|
+
.u-textarea {
|
|
287
|
+
border-radius: 4px;
|
|
288
|
+
background-color: #fff;
|
|
289
|
+
position: relative;
|
|
290
|
+
@include flex;
|
|
291
|
+
flex: 1;
|
|
292
|
+
|
|
293
|
+
&--border {
|
|
294
|
+
border-radius: 4px;
|
|
295
|
+
border: 1px solid $u-form-item-border-color;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
&--error {
|
|
299
|
+
border-color: $u-type-error !important;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
&--radius {
|
|
303
|
+
border-radius: 4px;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
&--no-radius {
|
|
307
|
+
border-radius: 0;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
&--disabled {
|
|
311
|
+
background-color: #f5f7fa;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
&__field {
|
|
315
|
+
flex: 1;
|
|
316
|
+
font-size: 28rpx;
|
|
317
|
+
color: $u-content-color;
|
|
318
|
+
width: 100%;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
&__count {
|
|
322
|
+
position: absolute;
|
|
323
|
+
right: 1px;
|
|
324
|
+
bottom: 0;
|
|
325
|
+
font-size: 12px;
|
|
326
|
+
color: $u-tips-color;
|
|
327
|
+
background-color: #ffffff;
|
|
328
|
+
padding: 1px 4px;
|
|
329
|
+
border-radius: 10px;
|
|
330
|
+
line-height: 16px;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
&__right-icon {
|
|
334
|
+
&__item {
|
|
335
|
+
margin-left: 10rpx;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
</style>
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"id": "uview-pro",
|
|
3
3
|
"name": "uview-pro",
|
|
4
4
|
"displayName": "【Vue3重构版】uView Pro|基于Vue3+TS全面重构的70+精选UI组件库",
|
|
5
|
-
"version": "0.3.
|
|
5
|
+
"version": "0.3.7",
|
|
6
6
|
"description": "uView Pro,是全面支持Vue3的uni-app生态框架,70+精选组件已使用TypeScript重构,已全面支持uni-app Vue3.0",
|
|
7
7
|
"main": "index.ts",
|
|
8
8
|
"module": "index.ts",
|
package/types/components.d.ts
CHANGED
|
@@ -94,6 +94,7 @@ declare module 'vue' {
|
|
|
94
94
|
uRootPortal: (typeof import('../components/u-root-portal/u-root-portal.vue'))['default'];
|
|
95
95
|
uStatusBar: (typeof import('../components/u-status-bar/u-status-bar.vue'))['default'];
|
|
96
96
|
uSafeBottom: (typeof import('../components/u-safe-bottom/u-safe-bottom.vue'))['default'];
|
|
97
|
+
uTextarea: (typeof import('../components/u-textarea/u-textarea.vue'))['default'];
|
|
97
98
|
}
|
|
98
99
|
}
|
|
99
100
|
|