/
dropdown.js
198 lines (194 loc) · 4.91 KB
/
dropdown.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import Vue from '../../utils/vue'
import { arrayIncludes } from '../../utils/array'
import { stripTags } from '../../utils/html'
import { getComponentConfig } from '../../utils/config'
import { HTMLElement } from '../../utils/safe-types'
import idMixin from '../../mixins/id'
import dropdownMixin from '../../mixins/dropdown'
import normalizeSlotMixin from '../../mixins/normalize-slot'
import { BButton } from '../button/button'
const NAME = 'BDropdown'
export const props = {
toggleText: {
// This really should be toggleLabel
type: String,
default: () => getComponentConfig(NAME, 'toggleText')
},
size: {
type: String,
default: () => getComponentConfig(NAME, 'size')
},
variant: {
type: String,
default: () => getComponentConfig(NAME, 'variant')
},
menuClass: {
type: [String, Array],
default: null
},
toggleTag: {
type: String,
default: 'button'
},
toggleClass: {
type: [String, Array],
default: null
},
noCaret: {
type: Boolean,
default: false
},
split: {
type: Boolean,
default: false
},
splitHref: {
type: String
// default: undefined
},
splitTo: {
type: [String, Object]
// default: undefined
},
splitVariant: {
type: String,
default: () => getComponentConfig(NAME, 'splitVariant')
},
splitButtonType: {
type: String,
default: 'button',
validator: value => arrayIncludes(['button', 'submit', 'reset'], value)
},
role: {
type: String,
default: 'menu'
},
boundary: {
// String: `scrollParent`, `window` or `viewport`
// HTMLElement: HTML Element reference
type: [String, HTMLElement],
default: 'scrollParent'
}
}
// @vue/component
export const BDropdown = /*#__PURE__*/ Vue.extend({
name: NAME,
mixins: [idMixin, dropdownMixin, normalizeSlotMixin],
props,
computed: {
dropdownClasses() {
return [
this.directionClass,
{
show: this.visible,
// Position `static` is needed to allow menu to "breakout" of the scrollParent boundaries
// when boundary is anything other than `scrollParent`
// See https://github.com/twbs/bootstrap/issues/24251#issuecomment-341413786
'position-static': this.boundary !== 'scrollParent' || !this.boundary
}
]
},
menuClasses() {
return [
this.menuClass,
{
'dropdown-menu-right': this.right,
show: this.visible
}
]
},
toggleClasses() {
return [
this.toggleClass,
{
'dropdown-toggle-split': this.split,
'dropdown-toggle-no-caret': this.noCaret && !this.split
}
]
}
},
render(h) {
let split = h()
const buttonContent = this.normalizeSlot('button-content') || this.html || stripTags(this.text)
if (this.split) {
const btnProps = {
disabled: this.disabled,
variant: this.splitVariant || this.variant,
size: this.size
}
// We add these as needed due to router-link issues with defined property with undefined/null values
if (this.splitTo) {
btnProps.to = this.splitTo
} else if (this.splitHref) {
btnProps.href = this.splitHref
} else if (this.splitButtonType) {
btnProps.type = this.splitButtonType
}
split = h(
BButton,
{
ref: 'button',
props: btnProps,
attrs: {
id: this.safeId('_BV_button_')
},
on: {
click: this.click
}
},
[buttonContent]
)
}
const toggle = h(
BButton,
{
ref: 'toggle',
staticClass: 'dropdown-toggle',
class: this.toggleClasses,
props: {
variant: this.variant,
size: this.size,
disabled: this.disabled,
tag: this.toggleTag
},
attrs: {
id: this.safeId('_BV_toggle_'),
'aria-haspopup': 'true',
'aria-expanded': this.visible ? 'true' : 'false'
},
on: {
click: this.toggle, // click
keydown: this.toggle // enter, space, down
}
},
[this.split ? h('span', { class: ['sr-only'] }, [this.toggleText]) : buttonContent]
)
const menu = h(
'ul',
{
ref: 'menu',
staticClass: 'dropdown-menu',
class: this.menuClasses,
attrs: {
role: this.role,
tabindex: '-1',
'aria-labelledby': this.safeId(this.split ? '_BV_button_' : '_BV_toggle_')
},
on: {
keydown: this.onKeydown // up, down, esc
}
},
!this.lazy || this.visible ? this.normalizeSlot('default', { hide: this.hide }) : [h()]
)
return h(
'div',
{
staticClass: 'dropdown btn-group b-dropdown',
class: this.dropdownClasses,
attrs: { id: this.safeId() }
},
[split, toggle, menu]
)
}
})
export default BDropdown