@gitlab/ui
Version:
GitLab UI Components
64 lines (58 loc) • 1.87 kB
JavaScript
import { extend } from '../../../vue';
import { PROP_TYPE_STRING } from '../../../constants/props';
import { get } from '../../../utils/get';
import { isPlainObject, isNull, isUndefined } from '../../../utils/inspect';
import { sortKeys } from '../../../utils/object';
import { props as props$1, formOptionsMixin } from '../../../mixins/form-options';
// --- Props ---
const props = sortKeys({
...props$1,
labelField: {
type: PROP_TYPE_STRING,
required: false,
default: 'label'
},
optionsField: {
type: PROP_TYPE_STRING,
required: false,
default: 'options'
}
});
// --- Mixin ---
// @vue/component
const optionsMixin = extend({
mixins: [formOptionsMixin],
props,
methods: {
normalizeOption(option) {
let key = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
// When the option is an object, normalize it
if (isPlainObject(option)) {
const value = get(option, this.valueField);
const text = get(option, this.textField);
const options = get(option, this.optionsField, null);
// When it has options, create an `<optgroup>` object
if (!isNull(options)) {
return {
label: String(get(option, this.labelField) || text),
options: this.normalizeOptions(options)
};
}
// Otherwise create an `<option>` object
return {
value: isUndefined(value) ? key || text : value,
text: String(isUndefined(text) ? key : text),
html: get(option, this.htmlField),
disabled: Boolean(get(option, this.disabledField))
};
}
// Otherwise create an `<option>` object from the given value
return {
value: key || option,
text: String(option),
disabled: false
};
}
}
});
export { optionsMixin, props };