Skip to content

Commit

Permalink
fix(form-options): Handle object special cases (#1099)
Browse files Browse the repository at this point in the history
Handle case where object format is missing value and/or text keys
  • Loading branch information
tmorehouse authored Sep 23, 2017
1 parent b4e0750 commit 1b17df3
Showing 1 changed file with 32 additions and 30 deletions.
62 changes: 32 additions & 30 deletions lib/mixins/form-options.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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.