Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 56f0c29

Browse files
refactor: 更改 formDesc 引用方式
1 parent c3a62b6 commit 56f0c29

File tree

3 files changed

+53
-53
lines changed

3 files changed

+53
-53
lines changed

‎lib/EleForm.vue‎

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ export default {
253253
},
254254
data() {
255255
return {
256+
formDescData: {},
256257
oldFormData: {},
257258
// 是否正在请求中
258259
innerIsLoading: false,
@@ -262,7 +263,9 @@ export default {
262263
},
263264
computed: {
264265
isMock() {
265-
return this.mock || Object.values(this.formDesc).some(item => item.mock)
266+
return (
267+
this.mock || Object.values(this.formDescData).some(item => item.mock)
268+
)
266269
},
267270
// 按钮
268271
btns() {
@@ -387,48 +390,48 @@ export default {
387390
return this.formDescKeys.reduce((rules, field) => {
388391
// 合并 (全局 和 局部) 的rules
389392
const formRules = castArray(this.rules[field])
390-
const formItemRules = castArray(this.formDesc[field].rules)
393+
const formItemRules = castArray(this.formDescData[field].rules)
391394
rules[field] = [...formRules, ...formItemRules]
392395
393396
// 如果采用required, 则判断已有的规则有无, 如果没有, 则添加
394397
if (
395-
this.formDesc[field].required &&
398+
this.formDescData[field].required &&
396399
!rules[field].some(rule => rule.required)
397400
) {
398401
rules[field].push({
399402
required: true,
400-
message: this.formDesc[field]._label + t('ele-form.required')
403+
message: this.formDescData[field]._label + t('ele-form.required')
401404
})
402405
}
403406
return rules
404407
}, {})
405408
},
406409
// formDesc的key
407410
formDescKeys() {
408-
return Object.keys(this.formDesc)
411+
return Object.keys(this.formDescData)
409412
},
410413
// 通过order数组排序后的formDesc
411414
orderedFormDesc() {
412415
if (this.order && this.order.length > 0) {
413416
const orderedFormDesc = {}
414417
// 根据order遍历,先添加到orderedFormDesc的key在之后遍历的时候,会先遍历,从而实现排序的目的。
415418
this.order.forEach(field => {
416-
if (this.formDesc[field]) {
417-
orderedFormDesc[field] = this.formDesc[field]
419+
if (this.formDescData[field]) {
420+
orderedFormDesc[field] = this.formDescData[field]
418421
} else {
419422
throw new Error('order中定义的key在formDesc中不存在')
420423
}
421424
})
422425
// 如果key不在order数组的时候,按照原序添加到orderedFormDesc
423-
Object.keys(this.formDesc).forEach(field => {
426+
Object.keys(this.formDescData).forEach(field => {
424427
// 当key不在order数组的时候
425428
if (!orderedFormDesc[field]) {
426-
orderedFormDesc[field] = this.formDesc[field]
429+
orderedFormDesc[field] = this.formDescData[field]
427430
}
428431
})
429432
return orderedFormDesc
430433
} else {
431-
return this.formDesc
434+
return this.formDescData
432435
}
433436
}
434437
},
@@ -438,16 +441,35 @@ export default {
438441
this.$refs.form.clearValidate()
439442
}
440443
},
441-
// 处理options参数
444+
// 同步数据
442445
formDesc: {
446+
handler(formDesc) {
447+
const oldFormDescData = {}
448+
// 去除被删除字段
449+
Object.keys(this.formDescData)
450+
.filter(key => formDesc[key])
451+
.forEach(key => {
452+
oldFormDescData[key] = this.formDescData[key]
453+
})
454+
this.formDescData = Object.assign(
455+
{},
456+
oldFormDescData,
457+
cloneDeep(formDesc)
458+
)
459+
},
460+
immediate: true,
461+
deep: true
462+
},
463+
formDescData: {
443464
handler(desc) {
444465
if (desc) {
445466
Object.keys(desc).forEach(field => {
446-
// 当全局设置mock为true时, 所有子项都标记为true
467+
// 当全局设置 mock 为 true 时, 所有子项都标记为 true
447468
if (this.mock && isUnDef(desc[field].mock)) {
448469
desc[field].mock = true
449470
}
450471
472+
// 设置默认值
451473
this.setDefaultvalue(desc[field], field)
452474
453475
// 转换 tip, 内部属性不显示
@@ -519,7 +541,7 @@ export default {
519541
},
520542
// 当类型为函数时的请求
521543
getFunctionAttr(fn, field) {
522-
return fn(this.formData, this.formDesc[field], this.formDesc)
544+
return fn(this.formData, this.formDescData[field], this.formDescData)
523545
},
524546
// 获取动态属性
525547
getDynamicAttr(attr, field) {
@@ -533,10 +555,10 @@ export default {
533555
this.checkLinkageFn()
534556
} else {
535557
this.checkLinkageFn = throttle(300, () => {
536-
const formDesc = this.formDesc
558+
const formDescData = this.formDescData
537559
const formData = this.formData
538-
Object.keys(formDesc).forEach(field => {
539-
const formItem = formDesc[field]
560+
Object.keys(formDescData).forEach(field => {
561+
const formItem = formDescData[field]
540562
// 1.设置 type
541563
let type = formItem.type
542564
if (typeof formItem.type === 'function') {
@@ -547,13 +569,13 @@ export default {
547569
// 获取此类型的以前值
548570
const newVal = formItem._oldValue['type-' + type] || null
549571
// 保存现在的数据作为老数据
550-
this.formDesc[field]._oldValue['type-' + formItem._type] =
572+
this.formDescData[field]._oldValue['type-' + formItem._type] =
551573
formData[field]
552574
553575
// 类型改变, 则删除原数据
554576
this.handleChange(field, newVal)
555577
556-
this.setDefaultvalue(this.formDesc[field], field)
578+
this.setDefaultvalue(this.formDescData[field], field)
557579
}
558580
} else {
559581
type = this.getComponentName(formItem.type)
@@ -595,8 +617,6 @@ export default {
595617
if (formItem._vif) {
596618
this.changeOptions(formItem.options || formItem._options, field)
597619
}
598-
599-
this.hidePrivateAttr(formItem)
600620
})
601621
})
602622
this.checkLinkageFn()
@@ -633,20 +653,6 @@ export default {
633653
}
634654
this.$set(formItem, '_defaultValue', defaultValue)
635655
},
636-
// 隐藏私有属性
637-
hidePrivateAttr(formItem) {
638-
if (process.env.NODE_ENV !== 'production') return
639-
Object.keys(formItem).forEach(key => {
640-
if (key.startsWith('_')) {
641-
Object.defineProperty(formItem, key, {
642-
enumerable: false,
643-
configurable: true,
644-
writable: true,
645-
value: formItem[key]
646-
})
647-
}
648-
})
649-
},
650656
// 组件名称
651657
getComponentName(type) {
652658
if (this.$EleFormBuiltInNames.includes(type)) {
@@ -687,7 +693,7 @@ export default {
687693
},
688694
isReloadOptions(field) {
689695
// 第一次进入 _options 为 false
690-
const formItem = this.formDesc[field]
696+
const formItem = this.formDescData[field]
691697
if (!formItem._options) return true
692698
// 如果关联字段不存在,则直接返回 false
693699
if (!formItem._optionsLinkageFields.length) return false
@@ -705,22 +711,22 @@ export default {
705711
} else if (options instanceof Function) {
706712
// 当options为Promise时: 等待Promise结束, 并获取值
707713
if (
708-
(this.formDesc[field]._optionsIsPromise &&
714+
(this.formDescData[field]._optionsIsPromise &&
709715
!this.isReloadOptions(field)) ||
710-
this.formDesc[field]._isLoadingOptions
716+
this.formDescData[field]._isLoadingOptions
711717
) {
712718
return
713719
}
714720
const res = this.getFunctionAttr(options, field)
715721
if (res instanceof Promise) {
716-
this.formDesc[field]._isLoadingOptions = true
717-
this.formDesc[field]._optionsIsPromise = true
722+
this.formDescData[field]._isLoadingOptions = true
723+
this.formDescData[field]._optionsIsPromise = true
718724
}
719725
// 当options为函数: 执行函数并递归
720726
this.changeOptions(res, field)
721727
} else if (options instanceof Promise) {
722728
options.then(options => {
723-
this.formDesc[field]._isLoadingOptions = false
729+
this.formDescData[field]._isLoadingOptions = false
724730
this.changeOptions(options, field)
725731
})
726732
} else if (typeof options === 'string' && this.optionsFn) {
@@ -741,15 +747,15 @@ export default {
741747
}
742748
}
743749
} else {
744-
if (this.formDesc[field]._options) {
750+
if (this.formDescData[field]._options) {
745751
// 如果new options为null / undefined, 且 old Options 存在, 则设置
746752
this.setOptions([], field)
747753
}
748754
}
749755
},
750756
// 设置options
751757
setOptions(options, field) {
752-
const formItem = this.formDesc[field]
758+
const formItem = this.formDescData[field]
753759
const prop = formItem._prop
754760
// 将options每一项转为对象
755761
let newOptions = this.getObjArrOptions(options)
@@ -759,15 +765,14 @@ export default {
759765
// 改变prop为规定的prop
760766
newOptions = this.changeProp(newOptions, prop)
761767
const newOptionsValues = newOptions.map(item => item.value).join(',')
762-
this.$set(this.formDesc[field], '_options', newOptions)
768+
this.$set(this.formDescData[field], '_options', newOptions)
763769
764770
// 新 options 和老 options 不同时,触发值的改变
765771
if (formItem.isRestValByOptions !== false) {
766772
if (oldOptionsValues && newOptionsValues !== oldOptionsValues) {
767773
this.setValue(field, null)
768774
}
769775
}
770-
this.hidePrivateAttr(this.formDesc[field])
771776
},
772777
// 验证表单
773778
validateForm() {
@@ -807,7 +812,7 @@ export default {
807812
if (!this.isShowErrorNotify) return
808813
try {
809814
const messageArr = Object.keys(errObj).reduce((acc, key) => {
810-
const formItem = this.formDesc[key]
815+
const formItem = this.formDescData[key]
811816
const label =
812817
formItem && formItem._label ? formItem._label + ': ' : key + ': '
813818
if (errObj[key] instanceof Array) {
@@ -855,7 +860,7 @@ export default {
855860
const data = cloneDeep(this.formData)
856861
// valueFormatter的处理
857862
for (const field in data) {
858-
const formItem = this.formDesc[field]
863+
const formItem = this.formDescData[field]
859864
if (formItem && formItem.valueFormatter) {
860865
data[field] = formItem.valueFormatter(data[field], data)
861866
}

‎lib/EleFormGroup.vue‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,6 @@ export default {
8989
// 修改form属性
9090
computedGroups() {
9191
return this.groups.map(item => {
92-
if (this.getDeepFormDesc) {
93-
item.form.formDesc = this.getDeepFormDesc(item.form.formDesc)
94-
}
9592
item.form = Object.assign({}, this.$attrs, item.form)
9693
item.on = Object.assign({}, this.$listeners, item.on)
9794
return item
@@ -124,9 +121,6 @@ export default {
124121
}
125122
},
126123
mounted() {
127-
this.$nextTick(() => {
128-
this.getDeepFormDesc = this.$refs['ele-form'].getDeepFormDesc
129-
})
130124
// 获取默认激活的分组
131125
if (isDef(this.activeGroupId)) {
132126
this.currentGroupId = this.activeGroupId

‎lib/components/EleFormSelect.vue‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ export default {
6161
this.loading = true
6262
this.remoteMethod(q, options => {
6363
this.loading = false
64-
this.EleForm.formDesc[this.field].isRestValByOptions = false
64+
this.EleForm.formDescData[this.field].options = options
65+
this.EleForm.formDescData[this.field].isRestValByOptions = false
6566
this.EleForm.changeOptions(options, this.field)
6667
})
6768
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /