Skip to content

Commit 33ab270

Browse files
committed
feat: 动态默认值
1 parent 0898285 commit 33ab270

File tree

3 files changed

+27
-41
lines changed

3 files changed

+27
-41
lines changed

lib/EleForm.vue

+14-2
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101

102102
<script>
103103
import responsiveMixin from './mixins/responsiveMixin'
104-
import { isUnDef, is, castArray, getDeepVal, setDeepVal } from './tools/utils'
104+
import { isUnDef, is, castArray, getDeepVal, setDeepVal, isEmpty } from './tools/utils'
105105
import { throttle } from 'throttle-debounce'
106106
import localeMixin from 'element-ui/src/mixins/locale'
107107
import { t } from './locale'
@@ -573,12 +573,24 @@ export default {
573573
typeof disabled === 'function' ? disabled(formData) : disabled
574574
)
575575
576+
// 4.默认值
577+
let defaultValue = typeof formItem.default === 'function' ? formItem.default(formData) : formItem.default
578+
// 默认值不为空 & (值为空 || 老值和当前值)
579+
if (!isEmpty(defaultValue) && (isEmpty(this.formData[field]) || formItem._defaultValue === this.formData[field])) {
580+
// 判断是否有格式化函数
581+
if (this.computedFormDesc[field].displayFormatter) {
582+
defaultValue = this.desc.displayFormatter(defaultValue, this.formData)
583+
}
584+
setDeepVal(this.formData, field, defaultValue)
585+
}
586+
576587
const fullPath = field.split('.').join('.chidlren.')
577588
setDeepVal(this.formDesc, fullPath + '._type', type)
578589
setDeepVal(this.formDesc, fullPath + '._vif', vif)
579590
setDeepVal(this.formDesc, fullPath + '._disabled', disabled)
591+
setDeepVal(this.formDesc, fullPath + '._defaultValue', defaultValue)
580592
581-
// 4.重新获取 options
593+
// 5.重新获取 options
582594
if (formItem.isReloadOptions) {
583595
this.changeOptions(
584596
getDeepVal(this.formDesc, fullPath),

lib/mixins/formMixin.js

+1-33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import ExtendSlot from '../ExtendSlot'
22
import attrsMixin from './attrsMixin'
3-
import { is, isUnDef, isDef } from '../tools/utils'
3+
import { is, isDef } from '../tools/utils'
44
import localMixin from './locale'
55
import mock from '../tools/mock'
66

@@ -105,35 +105,6 @@ export default {
105105
}
106106
},
107107

108-
// 触发默认值
109-
triggerDefault () {
110-
const value = this.value
111-
const isArr = Array.isArray(value)
112-
// 值为空
113-
if (
114-
isUnDef(value) ||
115-
value === '' ||
116-
(isArr && value.length === 0)
117-
) {
118-
// 默认值不为空
119-
if (isDef(this.desc.default)) {
120-
let defaultValue = this.desc.default
121-
// 判断是否有格式化函数
122-
if (this.desc.displayFormatter) {
123-
defaultValue = this.desc.displayFormatter(defaultValue)
124-
}
125-
126-
// 默认值类型检查
127-
if (this.checkType && !this.checkType(defaultValue, true)) {
128-
return
129-
}
130-
131-
this.newValue = defaultValue
132-
this.handleChange(this.newValue)
133-
}
134-
}
135-
},
136-
137108
// 初始化数据
138109
handleValueChanged (value) {
139110
if (this.desc.displayFormatter) {
@@ -147,8 +118,5 @@ export default {
147118
this.newValue = value
148119
}
149120
}
150-
},
151-
created () {
152-
this.triggerDefault()
153121
}
154122
}

lib/tools/utils.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,16 @@ export function castArray (value) {
137137
}
138138
}
139139

140-
export default {
141-
isDef,
142-
isUnDef,
143-
is,
144-
isProd,
145-
getSize
140+
// 判断是否为空
141+
// 空数组 / null / undefined / 空字符串
142+
export function isEmpty (val) {
143+
if (Array.isArray(val) && val.length === 0) {
144+
return true
145+
} else if (isUnDef(val)) {
146+
return true
147+
} else if (typeof val === 'string' && val === '') {
148+
return true
149+
} else {
150+
return false
151+
}
146152
}

0 commit comments

Comments
 (0)