UNPKG

@adonisjs/inertia

Version:

Official Inertia.js adapter for AdonisJS

104 lines (103 loc) 2.49 kB
import { defineComponent, h, inject, provide } from "vue"; import { Form as Form$1, Link as Link$1, router } from "@inertiajs/vue3"; const TuyauContext = Symbol("Tuyau"); const TuyauProvider = defineComponent({ name: "TuyauProvider", props: { client: { type: Object, required: true } }, setup(props, { slots }) { provide(TuyauContext, props.client); return () => slots.default?.(); } }); function useTuyau() { const context = inject(TuyauContext, null); if (!context) throw new Error("You must wrap your app in a TuyauProvider"); return context; } function useRouter() { const tuyau = useTuyau(); return { visit: (props, options) => { if ("href" in props) return router.visit(props.href, options); const routeInfo = tuyau.getRoute(props.route, { params: props.routeParams }); const url = routeInfo.url; return router.visit(url, { ...options, method: routeInfo.methods[0].toLowerCase() }); } }; } const Link = defineComponent({ name: "TuyauLink", inheritAttrs: false, props: { route: { type: String, required: false }, params: { type: [Array, Object], required: false }, href: { type: String, required: false } }, setup(props, { attrs, slots }) { const tuyau = useTuyau(); return () => { if (props.href) return h(Link$1, { ...attrs, href: props.href }, slots); if (!props.route) throw new Error("Either route or href prop is required for Link component"); const routeInfo = tuyau.getRoute(props.route, { params: props.params }); return h(Link$1, { ...attrs, href: routeInfo.url, method: routeInfo.methods[0].toLowerCase() }, slots); }; } }); const Form = defineComponent({ name: "TuyauForm", inheritAttrs: false, slots: Object, props: { route: { type: String, required: false }, params: { type: [Array, Object], required: false }, action: { type: Object, required: false } }, setup(props, { attrs, slots }) { const tuyau = useTuyau(); return () => { if (props.action) return h(Form$1, { ...attrs, action: props.action }, slots); if (!props.route) throw new Error("Either route or action prop is required for Form component"); const routeInfo = tuyau.getRoute(props.route, { params: props.params }); return h(Form$1, { ...attrs, action: { url: routeInfo.url, method: routeInfo.methods[0].toLowerCase() } }, slots); }; } }); export { Form, Link, TuyauProvider, useRouter, useTuyau };