forked from bootstrap-vue/bootstrap-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform-options.js
73 lines (67 loc) · 2.34 KB
/
form-options.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { extend } from '../vue'
import { PROP_TYPE_ARRAY_OBJECT, PROP_TYPE_STRING } from '../constants/props'
import { get } from '../utils/get'
import { stripTags } from '../utils/html'
import { isArray, isPlainObject, isUndefined } from '../utils/inspect'
import { keys } from '../utils/object'
import { makeProp, makePropsConfigurable } from '../utils/props'
import { warn } from '../utils/warn'
// --- Constants ---
const OPTIONS_OBJECT_DEPRECATED_MSG =
'Setting prop "options" to an object is deprecated. Use the array format instead.'
// --- Props ---
export const props = makePropsConfigurable(
{
disabledField: makeProp(PROP_TYPE_STRING, 'disabled'),
htmlField: makeProp(PROP_TYPE_STRING, 'html'),
options: makeProp(PROP_TYPE_ARRAY_OBJECT, []),
textField: makeProp(PROP_TYPE_STRING, 'text'),
valueField: makeProp(PROP_TYPE_STRING, 'value')
},
'formOptionControls'
)
// --- Mixin ---
// @vue/component
export const formOptionsMixin = extend({
props,
computed: {
formOptions() {
return this.normalizeOptions(this.options)
}
},
methods: {
normalizeOption(option, key = null) {
// When the option is an object, normalize it
if (isPlainObject(option)) {
const value = get(option, this.valueField)
const text = get(option, this.textField)
return {
value: isUndefined(value) ? key || text : value,
text: stripTags(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: 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 []
}
}
})