Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow components and global identifiers to be passed #1724

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions packages/vue3/src/link.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { mergeDataIntoQueryString, Method, PageProps, Progress, router, shouldIntercept } from '@inertiajs/core'
import { defineComponent, DefineComponent, h, PropType } from 'vue'
import {
Component,
computed,
ComputedRef,
defineComponent,
DefineComponent,
h,
PropType,
resolveDynamicComponent,
} from 'vue'

export interface InertiaLinkProps {
as?: string
as?: string | Component
data?: object
href: string
method?: Method
Expand All @@ -28,7 +37,7 @@ const Link: InertiaLink = defineComponent({
name: 'Link',
props: {
as: {
type: String,
type: [String, Object] as PropType<string | Component>,
default: 'a',
},
data: {
Expand Down Expand Up @@ -70,21 +79,30 @@ const Link: InertiaLink = defineComponent({
},
setup(props, { slots, attrs }) {
return () => {
const as = props.as.toLowerCase()
const resolvedAs: ComputedRef<String | Component> = computed(() => {
if (typeof props.as === 'object') return props.as
else {
const tryResolve = resolveDynamicComponent(props.as)
return typeof tryResolve === 'string' ? props.as.toLowerCase() : (tryResolve as Component)
}
})
const method = props.method.toLowerCase() as Method
const [href, data] = mergeDataIntoQueryString(method, props.href || '', props.data, props.queryStringArrayFormat)
const resolvedProps: ComputedRef<object> = computed(() =>
typeof resolvedAs.value === 'string' && resolvedAs.value !== 'a' ? {} : { href },
)

if (as === 'a' && method !== 'get') {
if (resolvedAs.value === 'a' && method !== 'get') {
console.warn(
`Creating POST/PUT/PATCH/DELETE <a> links is discouraged as it causes "Open Link in New Tab/Window" accessibility issues.\n\nPlease specify a more appropriate element using the "as" attribute. For example:\n\n<Link href="${href}" method="${method}" as="button">...</Link>`,
)
}

return h(
props.as,
resolvedAs.value,
{
...attrs,
...(as === 'a' ? { href } : {}),
...resolvedProps.value,
onClick: (event) => {
if (shouldIntercept(event)) {
event.preventDefault()
Expand Down