UNPKG

@adonisjs/inertia

Version:

Official Inertia.js adapter for AdonisJS

63 lines (62 loc) 1.84 kB
import React from "react"; import { jsx } from "react/jsx-runtime"; import { Form as Form$1, Link as Link$1, router } from "@inertiajs/react"; const TuyauContext = React.createContext(null); function TuyauProvider(props) { return /* @__PURE__ */ jsx(TuyauContext.Provider, { value: props.client, children: props.children }); } function useTuyau() { const context = React.useContext(TuyauContext); 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() }); } }; } function LinkInner(props, ref) { const tuyau = useTuyau(); if ("href" in props) return /* @__PURE__ */ jsx(Link$1, { ...props, ref }); const { route: _route, routeParams: params, ...linkProps } = props; const routeInfo = tuyau.getRoute(props.route, { params }); return /* @__PURE__ */ jsx(Link$1, { ...linkProps, href: routeInfo.url, method: routeInfo.methods[0].toLowerCase(), ref }); } const Link = React.forwardRef(LinkInner); function FormInner(props, ref) { const tuyau = useTuyau(); if ("action" in props) return /* @__PURE__ */ jsx(Form$1, { ...props, ref }); const { route: _route, routeParams: params, ...formProps } = props; const routeInfo = tuyau.getRoute(props.route, { params }); return /* @__PURE__ */ jsx(Form$1, { ...formProps, action: { url: routeInfo.url, method: routeInfo.methods[0].toLowerCase() }, ref }); } const Form = React.forwardRef(FormInner); export { Form, Link, TuyauProvider, useRouter, useTuyau };