From: Eduardo San Martin Morote Date: Fri, 8 May 2020 10:09:31 +0000 (+0200) Subject: feat: support jsx and tsx for RouterLink and RouterView X-Git-Tag: v4.0.0-alpha.11~19 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1d3dce3106af700fc95a403f1c229644fe8d85b8;p=thirdparty%2Fvuejs%2Frouter.git feat: support jsx and tsx for RouterLink and RouterView Close #226 --- diff --git a/__tests__/mount.ts b/__tests__/mount.ts index 8e460a4b..1ad19cf2 100644 --- a/__tests__/mount.ts +++ b/__tests__/mount.ts @@ -12,6 +12,7 @@ import { App, VNode, shallowRef, + ComponentOptions, } from 'vue' import { compile } from '@vue/compiler-dom' import * as runtimeDom from '@vue/runtime-dom' @@ -21,7 +22,7 @@ import { routeLocationKey } from '../src/injectionSymbols' export interface MountOptions { propsData: Record provide: Record - components: Record + components: ComponentOptions['components'] slots: Record } diff --git a/src/RouterLink.ts b/src/RouterLink.ts index 65550a4b..43acfc7b 100644 --- a/src/RouterLink.ts +++ b/src/RouterLink.ts @@ -6,20 +6,20 @@ import { computed, reactive, unref, - Component, + VNodeProps, } from 'vue' import { RouteLocationRaw, VueUseOptions, RouteLocation } from './types' import { isSameLocationObject, isSameRouteRecord } from './location' import { routerKey, routeLocationKey } from './injectionSymbols' import { RouteRecord } from './matcher/types' -interface LinkProps { +export interface RouterLinkProps { to: RouteLocationRaw - // TODO: refactor using extra options allowed in router.push + // TODO: refactor using extra options allowed in router.push. Needs RFC replace?: boolean } -type UseLinkOptions = VueUseOptions +type UseLinkOptions = VueUseOptions // TODO: we could allow currentRoute as a prop to expose `isActive` and // `isExactActive` behavior should go through an RFC @@ -85,7 +85,7 @@ export function useLink(props: UseLinkOptions) { } } -export const RouterLink = (defineComponent({ +export const RouterLinkImpl = defineComponent({ name: 'RouterLink', props: { to: { @@ -137,7 +137,15 @@ export const RouterLink = (defineComponent({ ) } }, -}) as unknown) as Component +}) + +// export the public type for h/tsx inference +// also to avoid inline import() in generated d.ts files +export const RouterLink = (RouterLinkImpl as any) as { + new (): { + $props: VNodeProps & RouterLinkProps + } +} function guardEvent(e: MouseEvent) { // don't redirect with control keys diff --git a/src/RouterView.ts b/src/RouterView.ts index d622ed41..91ddcb09 100644 --- a/src/RouterView.ts +++ b/src/RouterView.ts @@ -7,16 +7,22 @@ import { computed, ref, ComponentPublicInstance, - Component, + VNodeProps, } from 'vue' -import { RouteLocationNormalizedLoaded } from './types' +import { RouteLocationNormalizedLoaded, RouteLocationNormalized } from './types' import { matchedRouteKey, viewDepthKey, routeLocationKey, } from './injectionSymbols' -export const RouterView = (defineComponent({ +export interface RouterViewProps { + name?: string + // allow looser type for user facing api + route?: RouteLocationNormalized +} + +export const RouterViewImpl = defineComponent({ name: 'RouterView', props: { name: { @@ -97,4 +103,12 @@ export const RouterView = (defineComponent({ : null } }, -}) as unknown) as Component +}) + +// export the public type for h/tsx inference +// also to avoid inline import() in generated d.ts files +export const RouterView = (RouterViewImpl as any) as { + new (): { + $props: VNodeProps & RouterViewProps + } +} diff --git a/src/index.ts b/src/index.ts index 3f71bb70..00f5554f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -44,7 +44,7 @@ export { export { NavigationFailureType, NavigationFailure } from './errors' export { onBeforeRouteLeave, onBeforeRouteUpdate } from './navigationGuards' -export { RouterLink, useLink } from './RouterLink' -export { RouterView } from './RouterView' +export { RouterLink, useLink, RouterLinkProps } from './RouterLink' +export { RouterView, RouterViewProps } from './RouterView' export * from './useApi' diff --git a/test-dts/components.test-d.tsx b/test-dts/components.test-d.tsx new file mode 100644 index 00000000..c8f58b01 --- /dev/null +++ b/test-dts/components.test-d.tsx @@ -0,0 +1,23 @@ +import { expectError, expectType } from 'tsd' +import { + RouterLink, + RouterView, + createRouter, + createMemoryHistory, +} from './index' + +let router = createRouter({ + history: createMemoryHistory(), + routes: [], +}) + +// RouterLink +expectError() +expectType() +expectType() +expectType() + +// RouterView +expectType() +expectType() +expectType()