Skip to content

Commit

Permalink
fix: handle nested form options normalization (#5247)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobmllr95 authored Apr 26, 2020
1 parent ee7e8b8 commit 0c57ffe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/components/form-select/helpers/mixin-options.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import get from '../../../utils/get'
import { isArray, isPlainObject, isUndefined } from '../../../utils/inspect'
import { isNull, isPlainObject, isUndefined } from '../../../utils/inspect'
import formOptionsMixin from '../../../mixins/form-options'

// @vue/component
Expand All @@ -21,12 +21,12 @@ export default {
if (isPlainObject(option)) {
const value = get(option, this.valueField)
const text = get(option, this.textField)
const options = get(option, this.optionsField)
const options = get(option, this.optionsField, null)
// When it has options, create an `<optgroup>` object
if (isArray(options)) {
if (!isNull(options)) {
return {
label: String(get(option, this.labelField) || text),
options
options: this.normalizeOptions(options)
}
}
// Otherwise create an `<option>` object
Expand Down
28 changes: 15 additions & 13 deletions src/mixins/form-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,7 @@ export default {
},
computed: {
formOptions() {
const options = this.options
// Normalize the given options array
if (isArray(options)) {
return options.map(option => this.normalizeOption(option))
} else if (isPlainObject(options)) {
// Deprecate the object options format
warn(OPTIONS_OBJECT_DEPRECATED_MSG, this.$options.name)
// Normalize a `options` object to an array of options
return keys(options).map(key => this.normalizeOption(options[key] || {}, key))
}
// If not an array or object, return an empty array
/* istanbul ignore next */
return []
return this.normalizeOptions(this.options)
}
},
methods: {
Expand All @@ -67,6 +55,20 @@ export default {
text: stripTags(String(option)),
disabled: false
}
},
normalizeOptions(options) {
// Normalize the given options array
if (isArray(options)) {
return options.map(option => this.normalizeOption(option))
} else if (isPlainObject(options)) {
// Deprecate the object options format
warn(OPTIONS_OBJECT_DEPRECATED_MSG, this.$options.name)
// Normalize a `options` object to an array of options
return keys(options).map(key => this.normalizeOption(options[key] || {}, key))
}
// If not an array or object, return an empty array
/* istanbul ignore next */
return []
}
}
}

0 comments on commit 0c57ffe

Please sign in to comment.