From 46151eb7c7bcfb12905488e23f81c0cb19a2d666 Mon Sep 17 00:00:00 2001 From: Urata Daiki <7nohe@users.noreply.github.com> Date: Wed, 19 Feb 2025 10:00:32 +0900 Subject: [PATCH 1/3] feat: add useRouter hook for manual route visits in React and Vue --- docs/content/docs/inertia.md | 30 +++++++++++++++++++ packages/inertia/src/react/index.tsx | 24 ++++++++++++++- packages/inertia/src/vue/index.tsx | 24 ++++++++++++++- .../inertia/pages/posts/create.tsx | 14 +++++++-- .../inertia/pages/posts/create.vue | 15 ++++++++-- 5 files changed, 101 insertions(+), 6 deletions(-) diff --git a/docs/content/docs/inertia.md b/docs/content/docs/inertia.md index 96560bc..a1e3291 100644 --- a/docs/content/docs/inertia.md +++ b/docs/content/docs/inertia.md @@ -48,6 +48,19 @@ import { Link } from '@tuyau/inertia/react' Go to post ``` +### Manual visits + +The `useRouter` hook is useful to manually visit a route in your application. It returns an object with a `visit` method that you can use to visit a route. + +```tsx +import { useRouter } from '@tuyau/inertia/react' + +const router = useRouter() + +router.visit('users.posts.show', { id: 1, postId: 2 }) +``` + + ## Vue usage To use the Inertia helpers in your Vue x Inertia project, you must install the Tuyau plugin : @@ -85,3 +98,20 @@ import { Link } from '@tuyau/inertia/vue' Go to post ``` + +### Manual visits + +The `useRouter` hook is useful to manually visit a route in your application. It returns an object with a `visit` method that you can use to visit a route. + + +```vue + + + +``` \ No newline at end of file diff --git a/packages/inertia/src/react/index.tsx b/packages/inertia/src/react/index.tsx index cbbef05..995265e 100644 --- a/packages/inertia/src/react/index.tsx +++ b/packages/inertia/src/react/index.tsx @@ -1,6 +1,6 @@ import React from 'react' -import { Link as InertiaLink } from '@inertiajs/react' import type { TuyauClient, RouteName, GeneratedRoutes } from '@tuyau/client' +import { Link as InertiaLink, router as InertiaRouter } from '@inertiajs/react' import type { ValidatedApi, LinkParams } from '../types.js' @@ -50,3 +50,25 @@ export const Link: >( }, // @ts-expect-error TODO: fix this ) => ReturnType = React.forwardRef(LinkInner) as any + +export function useRouter() { + const tuyau = useTuyau() + if (!tuyau) throw new Error('You must wrap your app in a TuyauProvider') + + const router = { + visit: >( + props: LinkProps, + options?: Parameters[1], + ) => { + const route = tuyau.$route(props.route, (props as any).params) + const url = tuyau.$url(props.route, { params: props.params } as any) + + return InertiaRouter.visit(url, { + ...options, + method: route.method[0], + }) + }, + } + + return router +} diff --git a/packages/inertia/src/vue/index.tsx b/packages/inertia/src/vue/index.tsx index 1af3f10..6b697d9 100644 --- a/packages/inertia/src/vue/index.tsx +++ b/packages/inertia/src/vue/index.tsx @@ -1,8 +1,8 @@ import { defineComponent, h, inject } from 'vue' import type { DefineSetupFnComponent } from 'vue' -import { Link as InertiaLink } from '@inertiajs/vue3' import type { InertiaLinkProps } from '@inertiajs/vue3' import type { RouteName, TuyauClient } from '@tuyau/client' +import { Link as InertiaLink, router as InertiaRouter } from '@inertiajs/vue3' import type { ValidatedApi, LinkParams } from '../types.js' @@ -54,3 +54,25 @@ export const Link: >( }, { props: ['route', 'params'] }, ) + +export function useRouter() { + const tuyau = inject | null>(getClientKey()) + if (!tuyau) throw new Error('You must install the TuyauPlugin before using useRouter') + + const router = { + visit: >( + props: LinkParams & Omit, + options?: Parameters[1], + ) => { + const route = tuyau.$route(props.route, (props as any).params) + const url = tuyau.$url(props.route, { params: props.params } as any) + + return InertiaRouter.visit(url, { + ...options, + method: route.method[0], + }) + }, + } + + return router +} diff --git a/playgrounds/inertia-react/inertia/pages/posts/create.tsx b/playgrounds/inertia-react/inertia/pages/posts/create.tsx index fe46cd5..ced4a0c 100644 --- a/playgrounds/inertia-react/inertia/pages/posts/create.tsx +++ b/playgrounds/inertia-react/inertia/pages/posts/create.tsx @@ -1,12 +1,22 @@ import { Head } from '@inertiajs/react' -import { Link } from '@tuyau/inertia/react' +import { Link, useRouter } from '@tuyau/inertia/react' export default function Home() { + const router = useRouter() + return ( <> -
+

Create post

diff --git a/playgrounds/inertia-vue/inertia/pages/posts/create.vue b/playgrounds/inertia-vue/inertia/pages/posts/create.vue index f2dfb69..ca6b9ed 100644 --- a/playgrounds/inertia-vue/inertia/pages/posts/create.vue +++ b/playgrounds/inertia-vue/inertia/pages/posts/create.vue @@ -1,17 +1,28 @@