-
Notifications
You must be signed in to change notification settings - Fork 469
/
Copy pathLink.ts
executable file
·144 lines (134 loc) · 3.37 KB
/
Link.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import {
FormDataConvertible,
mergeDataIntoQueryString,
Method,
PreserveStateOption,
Progress,
router,
shouldIntercept,
} from '@inertiajs/core'
import { createElement, forwardRef, useCallback } from 'react'
const noop = () => undefined
interface BaseInertiaLinkProps {
as?: string
data?: Record<string, FormDataConvertible>
href: string
method?: Method
headers?: Record<string, string>
onClick?: (event: React.MouseEvent<HTMLAnchorElement>) => void
preserveScroll?: PreserveStateOption
preserveState?: PreserveStateOption
replace?: boolean
only?: string[]
except?: string[]
onCancelToken?: (cancelToken: import('axios').CancelTokenSource) => void
onBefore?: () => void
onStart?: () => void
onProgress?: (progress: Progress) => void
onFinish?: () => void
onCancel?: () => void
onSuccess?: () => void
onError?: () => void
queryStringArrayFormat?: 'indices' | 'brackets'
}
export type InertiaLinkProps = BaseInertiaLinkProps &
Omit<React.HTMLAttributes<HTMLElement>, keyof BaseInertiaLinkProps> &
Omit<React.AllHTMLAttributes<HTMLElement>, keyof BaseInertiaLinkProps>
const Link = forwardRef<unknown, InertiaLinkProps>(
(
{
children,
as = 'a',
data = {},
href,
method = 'get',
preserveScroll = false,
preserveState = null,
replace = false,
only = [],
except = [],
headers = {},
queryStringArrayFormat = 'brackets',
onClick = noop,
onCancelToken = noop,
onBefore = noop,
onStart = noop,
onProgress = noop,
onFinish = noop,
onCancel = noop,
onSuccess = noop,
onError = noop,
...props
},
ref,
) => {
const visit = useCallback(
(event) => {
onClick(event)
if (shouldIntercept(event)) {
event.preventDefault()
router.visit(href, {
data,
method,
preserveScroll,
preserveState: preserveState ?? method !== 'get',
replace,
only,
except,
headers,
onCancelToken,
onBefore,
onStart,
onProgress,
onFinish,
onCancel,
onSuccess,
onError,
})
}
},
[
data,
href,
method,
preserveScroll,
preserveState,
replace,
only,
except,
headers,
onClick,
onCancelToken,
onBefore,
onStart,
onProgress,
onFinish,
onCancel,
onSuccess,
onError,
],
)
as = as.toLowerCase()
method = method.toLowerCase() as Method
const [_href, _data] = mergeDataIntoQueryString(method, href || '', data, queryStringArrayFormat)
href = _href
data = _data
if (as === '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 createElement(
as,
{
...props,
...(as === 'a' ? { href } : {}),
ref,
onClick: visit,
},
children,
)
},
)
Link.displayName = 'InertiaLink'
export default Link