]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix(link): let vue merge attrs
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 26 Mar 2021 17:02:54 +0000 (18:02 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Fri, 26 Mar 2021 17:02:54 +0000 (18:02 +0100)
Fix #846

__tests__/RouterLink.spec.ts
src/RouterLink.ts

index 79bec2be238c394ac922acf3864cb0f95ece826b..34b65c428b757aa80fbbb0b75c39e0f9444def92 100644 (file)
@@ -800,6 +800,32 @@ describe('RouterLink', () => {
     expect(router.push).toHaveBeenCalledTimes(1)
   })
 
+  it('allows adding more click listeners', async () => {
+    const onClick = jest.fn()
+    const { router, wrapper } = await factory(
+      START_LOCATION_NORMALIZED,
+      { to: locations.basic.string, onClick },
+      locations.basic.normalized
+    )
+    wrapper.find('a')!.trigger('click')
+    expect(router.push).toHaveBeenCalledTimes(1)
+    expect(onClick).toHaveBeenCalledTimes(1)
+  })
+
+  it('allows adding custom classes', async () => {
+    const { wrapper } = await factory(
+      locations.basic.normalized,
+      { to: locations.basic.string, class: 'custom class' },
+      locations.basic.normalized
+    )
+    expect(wrapper.find('a')!.classes()).toEqual([
+      'router-link-active',
+      'router-link-exact-active',
+      'custom',
+      'class',
+    ])
+  })
+
   it('calls router.replace when clicked with replace prop', async () => {
     const { router, wrapper } = await factory(
       START_LOCATION_NORMALIZED,
index bea7365b7c1dbb0225f4c392b62c488a6f6e398a..efe99b6eea90c132351ca857b0d5edd266c2a78a 100644 (file)
@@ -16,7 +16,6 @@ import { RouteLocationRaw, VueUseOptions, RouteLocation } from './types'
 import { isSameRouteLocationParams, isSameRouteRecord } from './location'
 import { routerKey, routeLocationKey } from './injectionSymbols'
 import { RouteRecord } from './matcher/types'
-import { assign } from './utils'
 import { NavigationFailure } from './errors'
 
 export interface RouterLinkOptions {
@@ -145,7 +144,7 @@ export const RouterLinkImpl = /*#__PURE__*/ defineComponent({
     },
   },
 
-  setup(props, { slots, attrs }) {
+  setup(props, { slots }) {
     const link = reactive(useLink(props))
     const { options } = inject(routerKey)!
 
@@ -192,19 +191,16 @@ export const RouterLinkImpl = /*#__PURE__*/ defineComponent({
         ? children
         : h(
             'a',
-            assign(
-              {
-                'aria-current': link.isExactActive
-                  ? props.ariaCurrentValue
-                  : null,
-                onClick: link.navigate,
-                href: link.href,
-              },
-              attrs,
-              {
-                class: elClass.value,
-              }
-            ),
+            {
+              'aria-current': link.isExactActive
+                ? props.ariaCurrentValue
+                : null,
+              href: link.href,
+              // this would override user added attrs but Vue will still add
+              // the listener so we end up triggering both
+              onClick: link.navigate,
+              class: elClass.value,
+            },
             children
           )
     }