1
- import { cloneVNode , defineComponent , h , inject , onMounted , PropType , Ref , ref } from 'vue'
1
+ import { cloneVNode , computed , defineComponent , h , inject , onMounted , PropType , Ref , ref } from 'vue'
2
2
3
3
import { CButton } from '../button'
4
4
@@ -8,10 +8,6 @@ import type { Triggers } from '../../types'
8
8
const CDropdownToggle = defineComponent ( {
9
9
name : 'CDropdownToggle' ,
10
10
props : {
11
- /**
12
- * Toggle the active state for the component.
13
- */
14
- active : Boolean ,
15
11
/**
16
12
* Sets the color context of the component to one of CoreUI’s themed colors.
17
13
*
@@ -40,6 +36,15 @@ const CDropdownToggle = defineComponent({
40
36
* Toggle the disabled state for the component.
41
37
*/
42
38
disabled : Boolean ,
39
+ /**
40
+ * If a dropdown `variant` is set to `nav-item` then render the toggler as a link instead of a button.
41
+ *
42
+ * @since v5.0.0-alpha.1
43
+ */
44
+ navLink : {
45
+ type : Boolean ,
46
+ default : true ,
47
+ } ,
43
48
/**
44
49
* @values 'rounded', 'rounded-top', 'rounded-end', 'rounded-bottom', 'rounded-start', 'rounded-circle', 'rounded-pill', 'rounded-0', 'rounded-1', 'rounded-2', 'rounded-3'
45
50
*/
@@ -87,18 +92,10 @@ const CDropdownToggle = defineComponent({
87
92
const visible = inject ( 'visible' ) as Ref < boolean >
88
93
const setVisible = inject ( 'setVisible' ) as ( _visible ?: boolean ) => void
89
94
90
- const className = [
91
- {
92
- 'dropdown-toggle' : props . caret ,
93
- 'dropdown-toggle-split' : props . split ,
94
- active : props . active ,
95
- disabled : props . disabled ,
96
- } ,
97
- ]
98
-
99
95
const triggers = {
100
96
...( ( props . trigger === 'click' || props . trigger . includes ( 'click' ) ) && {
101
- onClick : ( ) => {
97
+ onClick : ( event : Event ) => {
98
+ event . preventDefault ( )
102
99
if ( props . disabled ) {
103
100
return
104
101
}
@@ -123,6 +120,20 @@ const CDropdownToggle = defineComponent({
123
120
} ) ,
124
121
}
125
122
123
+ const togglerProps = computed ( ( ) => {
124
+ return {
125
+ class : {
126
+ 'nav-link' : dropdownVariant === 'nav-item' && props . navLink ,
127
+ 'dropdown-toggle' : props . caret ,
128
+ 'dropdown-toggle-split' : props . split ,
129
+ disabled : props . disabled ,
130
+ show : visible . value
131
+ } ,
132
+ 'aria-expanded' : visible . value ,
133
+ ...( ! props . disabled && { ...triggers } ) ,
134
+ }
135
+ } )
136
+
126
137
onMounted ( ( ) => {
127
138
if ( togglerRef . value ) {
128
139
dropdownToggleRef . value = togglerRef . value . $el
@@ -140,45 +151,30 @@ const CDropdownToggle = defineComponent({
140
151
...triggers ,
141
152
} ) ,
142
153
)
143
- : dropdownVariant === 'nav-item'
154
+ : dropdownVariant === 'nav-item' && props . navLink
144
155
? h (
145
156
'a' ,
146
157
{
147
- active : props . active ,
148
- class : [
149
- 'nav-link' ,
150
- className ,
151
- {
152
- show : visible . value ,
153
- } ,
154
- ] ,
155
- disabled : props . disabled ,
156
158
href : '#' ,
159
+ ...togglerProps . value ,
160
+ role : 'button' ,
157
161
ref : dropdownToggleRef ,
158
- ...triggers ,
159
162
} ,
160
163
{ default : ( ) => slots . default && slots . default ( ) } ,
161
164
)
162
165
: h (
163
166
CButton ,
164
167
{
165
- class : [
166
- className ,
167
- {
168
- show : visible . value ,
169
- } ,
170
- ] ,
171
- active : props . active ,
168
+ ...togglerProps . value ,
172
169
color : props . color ,
170
+ component : props . component ,
173
171
disabled : props . disabled ,
174
- ref : ( el ) => {
175
- togglerRef . value = el
176
- } ,
177
172
shape : props . shape ,
178
173
size : props . size ,
179
- ...triggers ,
180
- ...( props . component === 'button' && { type : 'button' } ) ,
181
174
variant : props . variant ,
175
+ ref : ( el ) => {
176
+ togglerRef . value = el
177
+ } ,
182
178
} ,
183
179
( ) =>
184
180
props . split
0 commit comments