Permalink
Browse files

fix(form-options): Handle object special cases (#1099)

Handle case where object format is missing value and/or text keys
  • Loading branch information...
tmorehouse committed Sep 23, 2017
1 parent b4e0750 commit 1b17df39fe441ed55d54b77d4eb4ebe96392d223
Showing with 32 additions and 30 deletions.
  1. +32 −30 lib/mixins/form-options.js
@@ -1,6 +1,10 @@
import { isArray } from '../utils/array';
import { keys } from '../utils/object';
function isObject(obj) {
return obj && ({}).toString.call(obj) === '[object Object]';
}
export default {
props: {
options: {
@@ -26,50 +30,48 @@ export default {
formOptions() {
let options = this.options || [];
const valueField = this.valueField || 'value';
const textField = this.textField || 'text';
const disabledField = this.disabledField || 'disabled';
if (isArray(options)) {
// Normalize flat arrays to Array of Objects
options = options.map(option => {
if (typeof option === 'object') {
// Normalize flat-ish arrays to Array of Objects
return options.map(option => {
if (isObject(option)) {
return {
value: option[this.valueField],
text: option[this.textField],
disabled: option[this.disabledField] || false
value: option[valueField],
text: String(option[textField]),
disabled: option[disabledField] || false
};
}
return {
text: String(option),
value: option,
disabled: false
};
});
} else {
// Normalize Objects keys to Array of Objects
options = keys(options).map(key => {
} else if (isObject(options)) {
// Normalize Objects to Array of Objects
return keys(options).map(key => {
let option = options[key] || {};
// Resolve text
if (typeof option !== 'object') {
option = {[this.textField]: String(option)};
}
// Resolve text field (uses key as text if not provided)
if (option[this.textField] === 0) {
option.text = option[this.textField];
} else {
option.text = option[this.textField] || key;
if (isObject(option)) {
const value = option[valueField];
const text = option[textField];
return {
text: typeof text === 'undefined' ? key : String(text),
value: typeof value === 'undefined' ? key : value,
disabled: option[disabledField] || false
};
}
// Resolve value (uses null/undef value if not provided)
option.value = option[this.valueField];
// Resolve disabled
option.disabled = option[this.disabledField] || false;
return option;
return {
text: String(option),
value: key,
disabled: false
};
});
}
// Return nomalized options array
return options;
// Option unsupported type
return [];
}
}
};

0 comments on commit 1b17df3

Please sign in to comment.