uview-pro 0.3.6 → 0.3.8

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.
@@ -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: any[] = []; // 存放错误信息
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
- validateState.value = 'success';
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.indexList ?? getIndexList()).value;
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
- emit('update:modelValue', '');
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
  }
@@ -22,10 +22,12 @@
22
22
  :class="['u-text__price', props.type && `u-text__value--${props.type}`]"
23
23
  :style="textValueStyle"
24
24
  >
25
- {{ displayValue }}
25
+ <slot>¥{{ displayValue }}</slot>
26
26
  </text>
27
27
  <!-- link 模式 -->
28
- <u-link v-else-if="props.mode === 'link'" :href="props.href" underLine>{{ displayValue }}</u-link>
28
+ <u-link v-else-if="props.mode === 'link'" :href="props.href" underLine>
29
+ <slot>{{ displayValue }}</slot>
30
+ </u-link>
29
31
  <template v-else-if="props.openType && isMp">
30
32
  <button
31
33
  class="u-reset-button u-text__value"
@@ -46,7 +48,7 @@
46
48
  :show-message-card="props.showMessageCard"
47
49
  :app-parameter="props.appParameter"
48
50
  >
49
- {{ displayValue }}
51
+ <slot>{{ displayValue }}</slot>
50
52
  </button>
51
53
  </template>
52
54
  <!-- 默认模式 -->
@@ -56,7 +58,7 @@
56
58
  :style="textValueStyle"
57
59
  :class="[props.type && `u-text__value--${props.type}`, props.lines ? `u-line-${props.lines}` : '']"
58
60
  >
59
- {{ displayValue }}
61
+ <slot>{{ displayValue }}</slot>
60
62
  </text>
61
63
  <!-- 后缀图标 -->
62
64
  <view class="u-text__icon u-text__suffix-icon" v-if="props.suffixIcon">
@@ -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;