Skip to content

Commit

Permalink
feat: 动态默认值
Browse files Browse the repository at this point in the history
  • Loading branch information
dream2023 committed Feb 15, 2020
1 parent 0898285 commit 33ab270
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 41 deletions.
16 changes: 14 additions & 2 deletions lib/EleForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@

<script>
import responsiveMixin from './mixins/responsiveMixin'
import { isUnDef, is, castArray, getDeepVal, setDeepVal } from './tools/utils'
import { isUnDef, is, castArray, getDeepVal, setDeepVal, isEmpty } from './tools/utils'
import { throttle } from 'throttle-debounce'
import localeMixin from 'element-ui/src/mixins/locale'
import { t } from './locale'
Expand Down Expand Up @@ -573,12 +573,24 @@ export default {
typeof disabled === 'function' ? disabled(formData) : disabled
)
// 4.默认值
let defaultValue = typeof formItem.default === 'function' ? formItem.default(formData) : formItem.default
// 默认值不为空 & (值为空 || 老值和当前值)
if (!isEmpty(defaultValue) && (isEmpty(this.formData[field]) || formItem._defaultValue === this.formData[field])) {
// 判断是否有格式化函数
if (this.computedFormDesc[field].displayFormatter) {
defaultValue = this.desc.displayFormatter(defaultValue, this.formData)
}
setDeepVal(this.formData, field, defaultValue)
}
const fullPath = field.split('.').join('.chidlren.')
setDeepVal(this.formDesc, fullPath + '._type', type)
setDeepVal(this.formDesc, fullPath + '._vif', vif)
setDeepVal(this.formDesc, fullPath + '._disabled', disabled)
setDeepVal(this.formDesc, fullPath + '._defaultValue', defaultValue)
// 4.重新获取 options
// 5.重新获取 options
if (formItem.isReloadOptions) {
this.changeOptions(
getDeepVal(this.formDesc, fullPath),
Expand Down
34 changes: 1 addition & 33 deletions lib/mixins/formMixin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ExtendSlot from '../ExtendSlot'
import attrsMixin from './attrsMixin'
import { is, isUnDef, isDef } from '../tools/utils'
import { is, isDef } from '../tools/utils'
import localMixin from './locale'
import mock from '../tools/mock'

Expand Down Expand Up @@ -105,35 +105,6 @@ export default {
}
},

// 触发默认值
triggerDefault () {
const value = this.value
const isArr = Array.isArray(value)
// 值为空
if (
isUnDef(value) ||
value === '' ||
(isArr && value.length === 0)
) {
// 默认值不为空
if (isDef(this.desc.default)) {
let defaultValue = this.desc.default
// 判断是否有格式化函数
if (this.desc.displayFormatter) {
defaultValue = this.desc.displayFormatter(defaultValue)
}

// 默认值类型检查
if (this.checkType && !this.checkType(defaultValue, true)) {
return
}

this.newValue = defaultValue
this.handleChange(this.newValue)
}
}
},

// 初始化数据
handleValueChanged (value) {
if (this.desc.displayFormatter) {
Expand All @@ -147,8 +118,5 @@ export default {
this.newValue = value
}
}
},
created () {
this.triggerDefault()
}
}
18 changes: 12 additions & 6 deletions lib/tools/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,16 @@ export function castArray (value) {
}
}

export default {
isDef,
isUnDef,
is,
isProd,
getSize
// 判断是否为空
// 空数组 / null / undefined / 空字符串
export function isEmpty (val) {
if (Array.isArray(val) && val.length === 0) {
return true
} else if (isUnDef(val)) {
return true
} else if (typeof val === 'string' && val === '') {
return true
} else {
return false
}
}

0 comments on commit 33ab270

Please sign in to comment.