@vtj/materials 0.10.1-alpha.6 → 0.10.1

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.
Files changed (98) hide show
  1. package/dist/assets/antdv/index.umd.js +2 -2
  2. package/dist/assets/charts/index.umd.js +2 -2
  3. package/dist/assets/element/index.umd.js +2 -2
  4. package/dist/assets/ui/index.umd.js +2 -2
  5. package/dist/assets/uni-h5/index.umd.js +2 -2
  6. package/dist/assets/uni-ui/index.umd.js +3 -3
  7. package/dist/assets/vant/index.umd.js +5 -5
  8. package/dist/deps/@vtj/charts/index.umd.js +2 -2
  9. package/dist/deps/@vtj/icons/index.umd.js +2 -2
  10. package/dist/deps/@vtj/ui/index.umd.js +2 -2
  11. package/dist/deps/@vtj/utils/index.umd.js +2 -2
  12. package/dist/deps/uni-ui/index.umd.js +1 -1
  13. package/dist/deps/uni-ui/style.css +1 -1
  14. package/package.json +6 -6
  15. package/src/uni-ui/components/badge.ts +5 -1
  16. package/src/uni-ui/components/breadcrumb.ts +76 -0
  17. package/src/uni-ui/components/calendar.ts +63 -0
  18. package/src/uni-ui/components/card.ts +113 -0
  19. package/src/uni-ui/components/collapse.ts +118 -0
  20. package/src/uni-ui/components/combox.ts +51 -0
  21. package/src/uni-ui/components/countdown.ts +92 -0
  22. package/src/uni-ui/components/dataCheckbox.ts +104 -0
  23. package/src/uni-ui/components/dataPicker.ts +146 -0
  24. package/src/uni-ui/components/dataSelect.ts +68 -0
  25. package/src/uni-ui/components/dateformat.ts +42 -0
  26. package/src/uni-ui/components/datetimePicker.ts +103 -0
  27. package/src/uni-ui/components/drawer.ts +38 -0
  28. package/src/uni-ui/components/easyinput.ts +159 -0
  29. package/src/uni-ui/components/fab.ts +89 -0
  30. package/src/uni-ui/components/fav.ts +65 -0
  31. package/src/uni-ui/components/filePicker.ts +127 -0
  32. package/src/uni-ui/components/forms.ts +209 -0
  33. package/src/uni-ui/components/goodsNav.ts +31 -0
  34. package/src/uni-ui/components/grid.ts +104 -0
  35. package/src/uni-ui/components/group.ts +50 -0
  36. package/src/uni-ui/components/icons.ts +47 -0
  37. package/src/uni-ui/components/index.ts +89 -1
  38. package/src/uni-ui/components/indexedList.ts +51 -0
  39. package/src/uni-ui/components/link.ts +60 -0
  40. package/src/uni-ui/components/list.ts +259 -0
  41. package/src/uni-ui/components/loadMore.ts +61 -0
  42. package/src/uni-ui/components/navbar.ts +121 -0
  43. package/src/uni-ui/components/noticeBar.ts +83 -0
  44. package/src/uni-ui/components/numberbox.ts +49 -0
  45. package/src/uni-ui/components/pagination.ts +59 -0
  46. package/src/uni-ui/components/popup.ts +206 -0
  47. package/src/uni-ui/components/rate.ts +89 -0
  48. package/src/uni-ui/components/row.ts +126 -0
  49. package/src/uni-ui/components/searchBar.ts +84 -0
  50. package/src/uni-ui/components/section.ts +68 -0
  51. package/src/uni-ui/components/segmentedControl.ts +48 -0
  52. package/src/uni-ui/components/steps.ts +55 -0
  53. package/src/uni-ui/components/swipeAction.ts +133 -0
  54. package/src/uni-ui/components/swiperDot.ts +114 -0
  55. package/src/uni-ui/components/table.ts +132 -0
  56. package/src/uni-ui/components/tag.ts +56 -0
  57. package/src/uni-ui/components/title.ts +49 -0
  58. package/src/uni-ui/components/tooltip.ts +32 -0
  59. package/src/uni-ui/components/transition.ts +49 -0
  60. package/src/uni-ui/index.ts +30 -13
  61. package/src/uni-ui/lib/uni-col/uni-col.vue +324 -0
  62. package/src/uni-ui/lib/uni-data-select/uni-data-select.vue +590 -0
  63. package/src/uni-ui/lib/uni-datetime-picker/calendar-item.vue +177 -177
  64. package/src/uni-ui/lib/uni-datetime-picker/calendar.vue +947 -947
  65. package/src/uni-ui/lib/uni-datetime-picker/i18n/en.json +22 -22
  66. package/src/uni-ui/lib/uni-datetime-picker/i18n/index.js +7 -7
  67. package/src/uni-ui/lib/uni-datetime-picker/time-picker.vue +939 -939
  68. package/src/uni-ui/lib/uni-datetime-picker/util.js +419 -419
  69. package/src/uni-ui/lib/uni-easyinput/common.js +54 -0
  70. package/src/uni-ui/lib/uni-easyinput/uni-easyinput.vue +790 -0
  71. package/src/uni-ui/lib/uni-forms/uni-forms.vue +416 -416
  72. package/src/uni-ui/lib/uni-forms/utils.js +281 -281
  73. package/src/uni-ui/lib/uni-forms/validate.js +485 -485
  74. package/src/uni-ui/lib/uni-forms-item/uni-forms-item.vue +630 -0
  75. package/src/uni-ui/lib/uni-goods-nav/i18n/en.json +6 -0
  76. package/src/uni-ui/lib/uni-goods-nav/i18n/index.js +8 -0
  77. package/src/uni-ui/lib/uni-goods-nav/i18n/zh-Hans.json +6 -0
  78. package/src/uni-ui/lib/uni-goods-nav/i18n/zh-Hant.json +6 -0
  79. package/src/uni-ui/lib/uni-goods-nav/uni-goods-nav.vue +231 -0
  80. package/src/uni-ui/lib/uni-icons/uni-icons.uvue +91 -0
  81. package/src/uni-ui/lib/uni-icons/uni-icons.vue +114 -0
  82. package/src/uni-ui/lib/uni-icons/uniicons.css +664 -0
  83. package/src/uni-ui/lib/uni-icons/uniicons.ttf +0 -0
  84. package/src/uni-ui/lib/uni-icons/uniicons_file.ts +664 -0
  85. package/src/uni-ui/lib/uni-icons/uniicons_file_vue.js +649 -0
  86. package/src/uni-ui/lib/uni-indexed-list/uni-indexed-list-item.vue +144 -0
  87. package/src/uni-ui/lib/uni-indexed-list/uni-indexed-list.vue +404 -0
  88. package/src/uni-ui/lib/uni-list/uni-list.vue +123 -0
  89. package/src/uni-ui/lib/uni-list/uni-refresh.vue +65 -0
  90. package/src/uni-ui/lib/uni-list/uni-refresh.wxs +87 -0
  91. package/src/uni-ui/lib/uni-popup-dialog/keypress.js +45 -0
  92. package/src/uni-ui/lib/uni-popup-dialog/uni-popup-dialog.vue +324 -0
  93. package/src/uni-ui/lib/uni-rate/uni-rate.vue +361 -0
  94. package/src/uni-ui/lib/uni-table/uni-table.vue +496 -0
  95. package/src/uni-ui/lib/uni-td/uni-td.vue +99 -0
  96. package/src/uni-ui/lib/uni-th/uni-th.vue +310 -283
  97. package/src/uni-ui/lib/uni-tr/uni-tr.vue +194 -170
  98. package/src/version.ts +2 -2
@@ -1,416 +1,416 @@
1
- <template>
2
- <view class="uni-forms">
3
- <form>
4
- <slot></slot>
5
- </form>
6
- </view>
7
- </template>
8
-
9
- <script>
10
- import Validator from './validate.js';
11
- import {
12
- deepCopy,
13
- getValue,
14
- isRequiredField,
15
- setDataValue,
16
- getDataValue,
17
- realName,
18
- isRealName,
19
- rawData,
20
- isEqual
21
- } from './utils.js';
22
-
23
- // #ifndef VUE3
24
- // 后续会慢慢废弃这个方法
25
- // import Vue from 'vue';
26
- // Vue.prototype.binddata = function(name, value, formName) {
27
- // if (formName) {
28
- // this.$refs[formName].setValue(name, value);
29
- // } else {
30
- // let formVm;
31
- // for (let i in this.$refs) {
32
- // const vm = this.$refs[i];
33
- // if (vm && vm.$options && vm.$options.name === 'uniForms') {
34
- // formVm = vm;
35
- // break;
36
- // }
37
- // }
38
- // if (!formVm) return console.error('当前 uni-froms 组件缺少 ref 属性');
39
- // formVm.setValue(name, value);
40
- // }
41
- // };
42
- // #endif
43
- /**
44
- * Forms 表单
45
- * @description 由输入框、选择器、单选框、多选框等控件组成,用以收集、校验、提交数据
46
- * @tutorial https://ext.dcloud.net.cn/plugin?id=2773
47
- * @property {Object} rules 表单校验规则
48
- * @property {String} validateTrigger = [bind|submit|blur] 校验触发器方式 默认 submit
49
- * @value bind 发生变化时触发
50
- * @value submit 提交时触发
51
- * @value blur 失去焦点时触发
52
- * @property {String} labelPosition = [top|left] label 位置 默认 left
53
- * @value top 顶部显示 label
54
- * @value left 左侧显示 label
55
- * @property {String} labelWidth label 宽度,默认 70px
56
- * @property {String} labelAlign = [left|center|right] label 居中方式 默认 left
57
- * @value left label 左侧显示
58
- * @value center label 居中
59
- * @value right label 右侧对齐
60
- * @property {String} errShowType = [undertext|toast|modal] 校验错误信息提示方式
61
- * @value undertext 错误信息在底部显示
62
- * @value toast 错误信息toast显示
63
- * @value modal 错误信息modal显示
64
- * @event {Function} submit 提交时触发
65
- * @event {Function} validate 校验结果发生变化触发
66
- */
67
- export default {
68
- name: 'uniForms',
69
- emits: ['validate', 'submit'],
70
- options: {
71
- // #ifdef MP-TOUTIAO
72
- virtualHost: false,
73
- // #endif
74
- // #ifndef MP-TOUTIAO
75
- virtualHost: true
76
- // #endif
77
- },
78
- props: {
79
- // 即将弃用
80
- value: {
81
- type: Object,
82
- default() {
83
- return null;
84
- }
85
- },
86
- // vue3 替换 value 属性
87
- modelValue: {
88
- type: Object,
89
- default() {
90
- return null;
91
- }
92
- },
93
- // 1.4.0 开始将不支持 v-model ,且废弃 value 和 modelValue
94
- model: {
95
- type: Object,
96
- default() {
97
- return null;
98
- }
99
- },
100
- // 表单校验规则
101
- rules: {
102
- type: Object,
103
- default() {
104
- return {};
105
- }
106
- },
107
- //校验错误信息提示方式 默认 undertext 取值 [undertext|toast|modal]
108
- errShowType: {
109
- type: String,
110
- default: 'undertext'
111
- },
112
- // 校验触发器方式 默认 bind 取值 [bind|submit]
113
- validateTrigger: {
114
- type: String,
115
- default: 'submit'
116
- },
117
- // label 位置,默认 left 取值 top/left
118
- labelPosition: {
119
- type: String,
120
- default: 'left'
121
- },
122
- // label 宽度
123
- labelWidth: {
124
- type: [String, Number],
125
- default: ''
126
- },
127
- // label 居中方式,默认 left 取值 left/center/right
128
- labelAlign: {
129
- type: String,
130
- default: 'left'
131
- },
132
- border: {
133
- type: Boolean,
134
- default: false
135
- }
136
- },
137
- provide() {
138
- return {
139
- uniForm: this
140
- };
141
- },
142
- data() {
143
- return {
144
- // 表单本地值的记录,不应该与传如的值进行关联
145
- formData: {},
146
- formRules: {}
147
- };
148
- },
149
- computed: {
150
- // 计算数据源变化的
151
- localData() {
152
- const localVal = this.model || this.modelValue || this.value;
153
- if (localVal) {
154
- return deepCopy(localVal);
155
- }
156
- return {};
157
- }
158
- },
159
- watch: {
160
- // 监听数据变化 ,暂时不使用,需要单独赋值
161
- // localData: {},
162
- // 监听规则变化
163
- rules: {
164
- handler: function (val, oldVal) {
165
- this.setRules(val);
166
- },
167
- deep: true,
168
- immediate: true
169
- }
170
- },
171
- created() {
172
- // #ifdef VUE3
173
- let getbinddata =
174
- getApp().$vm.$.appContext.config.globalProperties.binddata;
175
- if (!getbinddata) {
176
- getApp().$vm.$.appContext.config.globalProperties.binddata = function (
177
- name,
178
- value,
179
- formName
180
- ) {
181
- if (formName) {
182
- this.$refs[formName].setValue(name, value);
183
- } else {
184
- let formVm;
185
- for (let i in this.$refs) {
186
- const vm = this.$refs[i];
187
- if (vm && vm.$options && vm.$options.name === 'uniForms') {
188
- formVm = vm;
189
- break;
190
- }
191
- }
192
- if (!formVm)
193
- return console.error('当前 uni-froms 组件缺少 ref 属性');
194
- if (formVm.model) formVm.model[name] = value;
195
- if (formVm.modelValue) formVm.modelValue[name] = value;
196
- if (formVm.value) formVm.value[name] = value;
197
- }
198
- };
199
- }
200
- // #endif
201
-
202
- // 子组件实例数组
203
- this.childrens = [];
204
- // TODO 兼容旧版 uni-data-picker ,新版本中无效,只是避免报错
205
- this.inputChildrens = [];
206
- this.setRules(this.rules);
207
- },
208
- methods: {
209
- /**
210
- * 外部调用方法
211
- * 设置规则 ,主要用于小程序自定义检验规则
212
- * @param {Array} rules 规则源数据
213
- */
214
- setRules(rules) {
215
- // TODO 有可能子组件合并规则的时机比这个要早,所以需要合并对象 ,而不是直接赋值,可能会被覆盖
216
- this.formRules = Object.assign({}, this.formRules, rules);
217
- // 初始化校验函数
218
- this.validator = new Validator(rules);
219
- },
220
-
221
- /**
222
- * 外部调用方法
223
- * 设置数据,用于设置表单数据,公开给用户使用 , 不支持在动态表单中使用
224
- * @param {Object} key
225
- * @param {Object} value
226
- */
227
- setValue(key, value) {
228
- let example = this.childrens.find((child) => child.name === key);
229
- if (!example) return null;
230
- this.formData[key] = getValue(
231
- key,
232
- value,
233
- (this.formRules[key] && this.formRules[key].rules) || []
234
- );
235
- return example.onFieldChange(this.formData[key]);
236
- },
237
-
238
- /**
239
- * 外部调用方法
240
- * 手动提交校验表单
241
- * 对整个表单进行校验的方法,参数为一个回调函数。
242
- * @param {Array} keepitem 保留不参与校验的字段
243
- * @param {type} callback 方法回调
244
- */
245
- validate(keepitem, callback) {
246
- return this.checkAll(this.formData, keepitem, callback);
247
- },
248
-
249
- /**
250
- * 外部调用方法
251
- * 部分表单校验
252
- * @param {Array|String} props 需要校验的字段
253
- * @param {Function} 回调函数
254
- */
255
- validateField(props = [], callback) {
256
- props = [].concat(props);
257
- let invalidFields = {};
258
- this.childrens.forEach((item) => {
259
- const name = realName(item.name);
260
- if (props.indexOf(name) !== -1) {
261
- invalidFields = Object.assign({}, invalidFields, {
262
- [name]: this.formData[name]
263
- });
264
- }
265
- });
266
- return this.checkAll(invalidFields, [], callback);
267
- },
268
-
269
- /**
270
- * 外部调用方法
271
- * 移除表单项的校验结果。传入待移除的表单项的 prop 属性或者 prop 组成的数组,如不传则移除整个表单的校验结果
272
- * @param {Array|String} props 需要移除校验的字段 ,不填为所有
273
- */
274
- clearValidate(props = []) {
275
- props = [].concat(props);
276
- this.childrens.forEach((item) => {
277
- if (props.length === 0) {
278
- item.errMsg = '';
279
- } else {
280
- const name = realName(item.name);
281
- if (props.indexOf(name) !== -1) {
282
- item.errMsg = '';
283
- }
284
- }
285
- });
286
- },
287
-
288
- /**
289
- * 外部调用方法 ,即将废弃
290
- * 手动提交校验表单
291
- * 对整个表单进行校验的方法,参数为一个回调函数。
292
- * @param {Array} keepitem 保留不参与校验的字段
293
- * @param {type} callback 方法回调
294
- */
295
- submit(keepitem, callback, type) {
296
- for (let i in this.dataValue) {
297
- const itemData = this.childrens.find((v) => v.name === i);
298
- if (itemData) {
299
- if (this.formData[i] === undefined) {
300
- this.formData[i] = this._getValue(i, this.dataValue[i]);
301
- }
302
- }
303
- }
304
-
305
- if (!type) {
306
- console.warn('submit 方法即将废弃,请使用validate方法代替!');
307
- }
308
-
309
- return this.checkAll(this.formData, keepitem, callback, 'submit');
310
- },
311
-
312
- // 校验所有
313
- async checkAll(invalidFields, keepitem, callback, type) {
314
- // 不存在校验规则 ,则停止校验流程
315
- if (!this.validator) return;
316
- let childrens = [];
317
- // 处理参与校验的item实例
318
- for (let i in invalidFields) {
319
- const item = this.childrens.find((v) => realName(v.name) === i);
320
- if (item) {
321
- childrens.push(item);
322
- }
323
- }
324
-
325
- // 如果validate第一个参数是funciont ,那就走回调
326
- if (!callback && typeof keepitem === 'function') {
327
- callback = keepitem;
328
- }
329
-
330
- let promise;
331
- // 如果不存在回调,那么使用 Promise 方式返回
332
- if (!callback && typeof callback !== 'function' && Promise) {
333
- promise = new Promise((resolve, reject) => {
334
- callback = function (valid, invalidFields) {
335
- !valid ? resolve(invalidFields) : reject(valid);
336
- };
337
- });
338
- }
339
-
340
- let results = [];
341
- // 避免引用错乱 ,建议拷贝对象处理
342
- let tempFormData = JSON.parse(JSON.stringify(invalidFields));
343
- // 所有子组件参与校验,使用 for 可以使用 awiat
344
- for (let i in childrens) {
345
- const child = childrens[i];
346
- let name = realName(child.name);
347
- const result = await child.onFieldChange(tempFormData[name]);
348
- if (result) {
349
- results.push(result);
350
- // toast ,modal 只需要执行第一次就可以
351
- if (this.errShowType === 'toast' || this.errShowType === 'modal')
352
- break;
353
- }
354
- }
355
-
356
- if (Array.isArray(results)) {
357
- if (results.length === 0) results = null;
358
- }
359
- if (Array.isArray(keepitem)) {
360
- keepitem.forEach((v) => {
361
- let vName = realName(v);
362
- let value = getDataValue(v, this.localData);
363
- if (value !== undefined) {
364
- tempFormData[vName] = value;
365
- }
366
- });
367
- }
368
-
369
- // TODO submit 即将废弃
370
- if (type === 'submit') {
371
- this.$emit('submit', {
372
- detail: {
373
- value: tempFormData,
374
- errors: results
375
- }
376
- });
377
- } else {
378
- this.$emit('validate', results);
379
- }
380
-
381
- // const resetFormData = rawData(tempFormData, this.localData, this.name)
382
- let resetFormData = {};
383
- resetFormData = rawData(tempFormData, this.name);
384
- callback &&
385
- typeof callback === 'function' &&
386
- callback(results, resetFormData);
387
-
388
- if (promise && callback) {
389
- return promise;
390
- } else {
391
- return null;
392
- }
393
- },
394
-
395
- /**
396
- * 返回validate事件
397
- * @param {Object} result
398
- */
399
- validateCheck(result) {
400
- this.$emit('validate', result);
401
- },
402
- _getValue: getValue,
403
- _isRequiredField: isRequiredField,
404
- _setDataValue: setDataValue,
405
- _getDataValue: getDataValue,
406
- _realName: realName,
407
- _isRealName: isRealName,
408
- _isEqual: isEqual
409
- }
410
- };
411
- </script>
412
-
413
- <style lang="scss">
414
- .uni-forms {
415
- }
416
- </style>
1
+ <template>
2
+ <view class="uni-forms">
3
+ <form>
4
+ <slot></slot>
5
+ </form>
6
+ </view>
7
+ </template>
8
+
9
+ <script>
10
+ import Validator from './validate.js';
11
+ import {
12
+ deepCopy,
13
+ getValue,
14
+ isRequiredField,
15
+ setDataValue,
16
+ getDataValue,
17
+ realName,
18
+ isRealName,
19
+ rawData,
20
+ isEqual
21
+ } from './utils.js';
22
+
23
+ // #ifndef VUE3
24
+ // 后续会慢慢废弃这个方法
25
+ // import Vue from 'vue';
26
+ // Vue.prototype.binddata = function(name, value, formName) {
27
+ // if (formName) {
28
+ // this.$refs[formName].setValue(name, value);
29
+ // } else {
30
+ // let formVm;
31
+ // for (let i in this.$refs) {
32
+ // const vm = this.$refs[i];
33
+ // if (vm && vm.$options && vm.$options.name === 'uniForms') {
34
+ // formVm = vm;
35
+ // break;
36
+ // }
37
+ // }
38
+ // if (!formVm) return console.error('当前 uni-froms 组件缺少 ref 属性');
39
+ // formVm.setValue(name, value);
40
+ // }
41
+ // };
42
+ // #endif
43
+ /**
44
+ * Forms 表单
45
+ * @description 由输入框、选择器、单选框、多选框等控件组成,用以收集、校验、提交数据
46
+ * @tutorial https://ext.dcloud.net.cn/plugin?id=2773
47
+ * @property {Object} rules 表单校验规则
48
+ * @property {String} validateTrigger = [bind|submit|blur] 校验触发器方式 默认 submit
49
+ * @value bind 发生变化时触发
50
+ * @value submit 提交时触发
51
+ * @value blur 失去焦点时触发
52
+ * @property {String} labelPosition = [top|left] label 位置 默认 left
53
+ * @value top 顶部显示 label
54
+ * @value left 左侧显示 label
55
+ * @property {String} labelWidth label 宽度,默认 70px
56
+ * @property {String} labelAlign = [left|center|right] label 居中方式 默认 left
57
+ * @value left label 左侧显示
58
+ * @value center label 居中
59
+ * @value right label 右侧对齐
60
+ * @property {String} errShowType = [undertext|toast|modal] 校验错误信息提示方式
61
+ * @value undertext 错误信息在底部显示
62
+ * @value toast 错误信息toast显示
63
+ * @value modal 错误信息modal显示
64
+ * @event {Function} submit 提交时触发
65
+ * @event {Function} validate 校验结果发生变化触发
66
+ */
67
+ export default {
68
+ name: 'uniForms',
69
+ emits: ['validate', 'submit'],
70
+ options: {
71
+ // #ifdef MP-TOUTIAO
72
+ virtualHost: false,
73
+ // #endif
74
+ // #ifndef MP-TOUTIAO
75
+ virtualHost: true
76
+ // #endif
77
+ },
78
+ props: {
79
+ // 即将弃用
80
+ value: {
81
+ type: Object,
82
+ default() {
83
+ return null;
84
+ }
85
+ },
86
+ // vue3 替换 value 属性
87
+ modelValue: {
88
+ type: Object,
89
+ default() {
90
+ return null;
91
+ }
92
+ },
93
+ // 1.4.0 开始将不支持 v-model ,且废弃 value 和 modelValue
94
+ model: {
95
+ type: Object,
96
+ default() {
97
+ return null;
98
+ }
99
+ },
100
+ // 表单校验规则
101
+ rules: {
102
+ type: Object,
103
+ default() {
104
+ return {};
105
+ }
106
+ },
107
+ //校验错误信息提示方式 默认 undertext 取值 [undertext|toast|modal]
108
+ errShowType: {
109
+ type: String,
110
+ default: 'undertext'
111
+ },
112
+ // 校验触发器方式 默认 bind 取值 [bind|submit]
113
+ validateTrigger: {
114
+ type: String,
115
+ default: 'submit'
116
+ },
117
+ // label 位置,默认 left 取值 top/left
118
+ labelPosition: {
119
+ type: String,
120
+ default: 'left'
121
+ },
122
+ // label 宽度
123
+ labelWidth: {
124
+ type: [String, Number],
125
+ default: ''
126
+ },
127
+ // label 居中方式,默认 left 取值 left/center/right
128
+ labelAlign: {
129
+ type: String,
130
+ default: 'left'
131
+ },
132
+ border: {
133
+ type: Boolean,
134
+ default: false
135
+ }
136
+ },
137
+ provide() {
138
+ return {
139
+ uniForm: this
140
+ };
141
+ },
142
+ data() {
143
+ return {
144
+ // 表单本地值的记录,不应该与传如的值进行关联
145
+ formData: {},
146
+ formRules: {}
147
+ };
148
+ },
149
+ computed: {
150
+ // 计算数据源变化的
151
+ localData() {
152
+ const localVal = this.model || this.modelValue || this.value;
153
+ if (localVal) {
154
+ return deepCopy(localVal);
155
+ }
156
+ return {};
157
+ }
158
+ },
159
+ watch: {
160
+ // 监听数据变化 ,暂时不使用,需要单独赋值
161
+ // localData: {},
162
+ // 监听规则变化
163
+ rules: {
164
+ handler: function (val, oldVal) {
165
+ this.setRules(val);
166
+ },
167
+ deep: true,
168
+ immediate: true
169
+ }
170
+ },
171
+ created() {
172
+ // #ifdef VUE3
173
+ let getbinddata =
174
+ getApp().$vm.$.appContext.config.globalProperties.binddata;
175
+ if (!getbinddata) {
176
+ getApp().$vm.$.appContext.config.globalProperties.binddata = function (
177
+ name,
178
+ value,
179
+ formName
180
+ ) {
181
+ if (formName) {
182
+ this.$refs[formName].setValue(name, value);
183
+ } else {
184
+ let formVm;
185
+ for (let i in this.$refs) {
186
+ const vm = this.$refs[i];
187
+ if (vm && vm.$options && vm.$options.name === 'uniForms') {
188
+ formVm = vm;
189
+ break;
190
+ }
191
+ }
192
+ if (!formVm)
193
+ return console.error('当前 uni-froms 组件缺少 ref 属性');
194
+ if (formVm.model) formVm.model[name] = value;
195
+ if (formVm.modelValue) formVm.modelValue[name] = value;
196
+ if (formVm.value) formVm.value[name] = value;
197
+ }
198
+ };
199
+ }
200
+ // #endif
201
+
202
+ // 子组件实例数组
203
+ this.childrens = [];
204
+ // TODO 兼容旧版 uni-data-picker ,新版本中无效,只是避免报错
205
+ this.inputChildrens = [];
206
+ this.setRules(this.rules);
207
+ },
208
+ methods: {
209
+ /**
210
+ * 外部调用方法
211
+ * 设置规则 ,主要用于小程序自定义检验规则
212
+ * @param {Array} rules 规则源数据
213
+ */
214
+ setRules(rules) {
215
+ // TODO 有可能子组件合并规则的时机比这个要早,所以需要合并对象 ,而不是直接赋值,可能会被覆盖
216
+ this.formRules = Object.assign({}, this.formRules, rules);
217
+ // 初始化校验函数
218
+ this.validator = new Validator(rules);
219
+ },
220
+
221
+ /**
222
+ * 外部调用方法
223
+ * 设置数据,用于设置表单数据,公开给用户使用 , 不支持在动态表单中使用
224
+ * @param {Object} key
225
+ * @param {Object} value
226
+ */
227
+ setValue(key, value) {
228
+ let example = this.childrens.find((child) => child.name === key);
229
+ if (!example) return null;
230
+ this.formData[key] = getValue(
231
+ key,
232
+ value,
233
+ (this.formRules[key] && this.formRules[key].rules) || []
234
+ );
235
+ return example.onFieldChange(this.formData[key]);
236
+ },
237
+
238
+ /**
239
+ * 外部调用方法
240
+ * 手动提交校验表单
241
+ * 对整个表单进行校验的方法,参数为一个回调函数。
242
+ * @param {Array} keepitem 保留不参与校验的字段
243
+ * @param {type} callback 方法回调
244
+ */
245
+ validate(keepitem, callback) {
246
+ return this.checkAll(this.formData, keepitem, callback);
247
+ },
248
+
249
+ /**
250
+ * 外部调用方法
251
+ * 部分表单校验
252
+ * @param {Array|String} props 需要校验的字段
253
+ * @param {Function} 回调函数
254
+ */
255
+ validateField(props = [], callback) {
256
+ props = [].concat(props);
257
+ let invalidFields = {};
258
+ this.childrens.forEach((item) => {
259
+ const name = realName(item.name);
260
+ if (props.indexOf(name) !== -1) {
261
+ invalidFields = Object.assign({}, invalidFields, {
262
+ [name]: this.formData[name]
263
+ });
264
+ }
265
+ });
266
+ return this.checkAll(invalidFields, [], callback);
267
+ },
268
+
269
+ /**
270
+ * 外部调用方法
271
+ * 移除表单项的校验结果。传入待移除的表单项的 prop 属性或者 prop 组成的数组,如不传则移除整个表单的校验结果
272
+ * @param {Array|String} props 需要移除校验的字段 ,不填为所有
273
+ */
274
+ clearValidate(props = []) {
275
+ props = [].concat(props);
276
+ this.childrens.forEach((item) => {
277
+ if (props.length === 0) {
278
+ item.errMsg = '';
279
+ } else {
280
+ const name = realName(item.name);
281
+ if (props.indexOf(name) !== -1) {
282
+ item.errMsg = '';
283
+ }
284
+ }
285
+ });
286
+ },
287
+
288
+ /**
289
+ * 外部调用方法 ,即将废弃
290
+ * 手动提交校验表单
291
+ * 对整个表单进行校验的方法,参数为一个回调函数。
292
+ * @param {Array} keepitem 保留不参与校验的字段
293
+ * @param {type} callback 方法回调
294
+ */
295
+ submit(keepitem, callback, type) {
296
+ for (let i in this.dataValue) {
297
+ const itemData = this.childrens.find((v) => v.name === i);
298
+ if (itemData) {
299
+ if (this.formData[i] === undefined) {
300
+ this.formData[i] = this._getValue(i, this.dataValue[i]);
301
+ }
302
+ }
303
+ }
304
+
305
+ if (!type) {
306
+ console.warn('submit 方法即将废弃,请使用validate方法代替!');
307
+ }
308
+
309
+ return this.checkAll(this.formData, keepitem, callback, 'submit');
310
+ },
311
+
312
+ // 校验所有
313
+ async checkAll(invalidFields, keepitem, callback, type) {
314
+ // 不存在校验规则 ,则停止校验流程
315
+ if (!this.validator) return;
316
+ let childrens = [];
317
+ // 处理参与校验的item实例
318
+ for (let i in invalidFields) {
319
+ const item = this.childrens.find((v) => realName(v.name) === i);
320
+ if (item) {
321
+ childrens.push(item);
322
+ }
323
+ }
324
+
325
+ // 如果validate第一个参数是funciont ,那就走回调
326
+ if (!callback && typeof keepitem === 'function') {
327
+ callback = keepitem;
328
+ }
329
+
330
+ let promise;
331
+ // 如果不存在回调,那么使用 Promise 方式返回
332
+ if (!callback && typeof callback !== 'function' && Promise) {
333
+ promise = new Promise((resolve, reject) => {
334
+ callback = function (valid, invalidFields) {
335
+ !valid ? resolve(invalidFields) : reject(valid);
336
+ };
337
+ });
338
+ }
339
+
340
+ let results = [];
341
+ // 避免引用错乱 ,建议拷贝对象处理
342
+ let tempFormData = JSON.parse(JSON.stringify(invalidFields));
343
+ // 所有子组件参与校验,使用 for 可以使用 awiat
344
+ for (let i in childrens) {
345
+ const child = childrens[i];
346
+ let name = realName(child.name);
347
+ const result = await child.onFieldChange(tempFormData[name]);
348
+ if (result) {
349
+ results.push(result);
350
+ // toast ,modal 只需要执行第一次就可以
351
+ if (this.errShowType === 'toast' || this.errShowType === 'modal')
352
+ break;
353
+ }
354
+ }
355
+
356
+ if (Array.isArray(results)) {
357
+ if (results.length === 0) results = null;
358
+ }
359
+ if (Array.isArray(keepitem)) {
360
+ keepitem.forEach((v) => {
361
+ let vName = realName(v);
362
+ let value = getDataValue(v, this.localData);
363
+ if (value !== undefined) {
364
+ tempFormData[vName] = value;
365
+ }
366
+ });
367
+ }
368
+
369
+ // TODO submit 即将废弃
370
+ if (type === 'submit') {
371
+ this.$emit('submit', {
372
+ detail: {
373
+ value: tempFormData,
374
+ errors: results
375
+ }
376
+ });
377
+ } else {
378
+ this.$emit('validate', results);
379
+ }
380
+
381
+ // const resetFormData = rawData(tempFormData, this.localData, this.name)
382
+ let resetFormData = {};
383
+ resetFormData = rawData(tempFormData, this.name);
384
+ callback &&
385
+ typeof callback === 'function' &&
386
+ callback(results, resetFormData);
387
+
388
+ if (promise && callback) {
389
+ return promise;
390
+ } else {
391
+ return null;
392
+ }
393
+ },
394
+
395
+ /**
396
+ * 返回validate事件
397
+ * @param {Object} result
398
+ */
399
+ validateCheck(result) {
400
+ this.$emit('validate', result);
401
+ },
402
+ _getValue: getValue,
403
+ _isRequiredField: isRequiredField,
404
+ _setDataValue: setDataValue,
405
+ _getDataValue: getDataValue,
406
+ _realName: realName,
407
+ _isRealName: isRealName,
408
+ _isEqual: isEqual
409
+ }
410
+ };
411
+ </script>
412
+
413
+ <style lang="scss">
414
+ .uni-forms {
415
+ }
416
+ </style>