@inertiajs/react 2.3.17 → 3.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +986 -532
- package/dist/index.js.map +4 -4
- package/dist/server.js +4 -35
- package/dist/server.js.map +2 -2
- package/package.json +9 -11
- package/resources/boost/skills/inertia-react-development/SKILL.blade.php +128 -8
- package/types/App.d.ts +3 -2
- package/types/Deferred.d.ts +4 -1
- package/types/Form.d.ts +10 -19
- package/types/InfiniteScroll.d.ts +2 -1
- package/types/createInertiaApp.d.ts +15 -4
- package/types/index.d.ts +6 -15
- package/types/layoutProps.d.ts +8 -0
- package/types/react.d.ts +0 -1
- package/types/types.d.ts +3 -1
- package/types/useForm.d.ts +10 -12
- package/types/useFormState.d.ts +79 -0
- package/types/useHttp.d.ts +65 -0
- package/dist/index.esm.js +0 -1651
- package/dist/index.esm.js.map +0 -7
- package/dist/server.esm.js +0 -6
- package/dist/server.esm.js.map +0 -7
package/dist/index.js
CHANGED
|
@@ -1,74 +1,74 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
|
|
30
1
|
// src/index.ts
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
App: () => App,
|
|
34
|
-
Deferred: () => Deferred_default,
|
|
35
|
-
Form: () => Form_default,
|
|
36
|
-
Head: () => Head_default,
|
|
37
|
-
InfiniteScroll: () => InfiniteScroll_default,
|
|
38
|
-
Link: () => Link_default,
|
|
39
|
-
WhenVisible: () => WhenVisible_default,
|
|
40
|
-
config: () => config,
|
|
41
|
-
createInertiaApp: () => createInertiaApp,
|
|
42
|
-
progress: () => progress,
|
|
43
|
-
router: () => router3,
|
|
44
|
-
useForm: () => useForm,
|
|
45
|
-
useFormContext: () => useFormContext,
|
|
46
|
-
usePage: () => usePage,
|
|
47
|
-
usePoll: () => usePoll,
|
|
48
|
-
usePrefetch: () => usePrefetch,
|
|
49
|
-
useRemember: () => useRemember
|
|
50
|
-
});
|
|
51
|
-
module.exports = __toCommonJS(index_exports);
|
|
52
|
-
var import_core11 = require("@inertiajs/core");
|
|
2
|
+
import { config as coreConfig } from "@inertiajs/core";
|
|
3
|
+
import { http as http2, progress, router as router3 } from "@inertiajs/core";
|
|
53
4
|
|
|
54
5
|
// src/App.ts
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
6
|
+
import {
|
|
7
|
+
createHeadManager,
|
|
8
|
+
normalizeLayouts,
|
|
9
|
+
router
|
|
10
|
+
} from "@inertiajs/core";
|
|
11
|
+
import { createElement, useEffect, useMemo as useMemo2, useState } from "react";
|
|
12
|
+
import { flushSync } from "react-dom";
|
|
58
13
|
|
|
59
14
|
// src/HeadContext.ts
|
|
60
|
-
|
|
61
|
-
var headContext =
|
|
15
|
+
import { createContext } from "react";
|
|
16
|
+
var headContext = createContext(null);
|
|
62
17
|
headContext.displayName = "InertiaHeadContext";
|
|
63
18
|
var HeadContext_default = headContext;
|
|
64
19
|
|
|
20
|
+
// src/layoutProps.ts
|
|
21
|
+
import { createLayoutPropsStore, mergeLayoutProps } from "@inertiajs/core";
|
|
22
|
+
import { createContext as createContext2, useContext, useMemo, useSyncExternalStore } from "react";
|
|
23
|
+
var store = createLayoutPropsStore();
|
|
24
|
+
function setLayoutProps(props) {
|
|
25
|
+
store.set(props);
|
|
26
|
+
}
|
|
27
|
+
function setLayoutPropsFor(name, props) {
|
|
28
|
+
store.setFor(name, props);
|
|
29
|
+
}
|
|
30
|
+
function resetLayoutProps() {
|
|
31
|
+
store.reset();
|
|
32
|
+
}
|
|
33
|
+
var LayoutPropsContext = createContext2({
|
|
34
|
+
staticProps: {}
|
|
35
|
+
});
|
|
36
|
+
function useLayoutProps(defaults) {
|
|
37
|
+
const { staticProps, name } = useContext(LayoutPropsContext);
|
|
38
|
+
const { shared, named } = useSyncExternalStore(store.subscribe, store.get, store.get);
|
|
39
|
+
return useMemo(() => {
|
|
40
|
+
const dynamicProps = name ? { ...shared, ...named[name] } : shared;
|
|
41
|
+
return mergeLayoutProps(defaults, staticProps, dynamicProps);
|
|
42
|
+
}, [defaults, staticProps, name, shared, named]);
|
|
43
|
+
}
|
|
44
|
+
|
|
65
45
|
// src/PageContext.ts
|
|
66
|
-
|
|
67
|
-
var pageContext = (
|
|
46
|
+
import { createContext as createContext3 } from "react";
|
|
47
|
+
var pageContext = createContext3(null);
|
|
68
48
|
pageContext.displayName = "InertiaPageContext";
|
|
69
49
|
var PageContext_default = pageContext;
|
|
70
50
|
|
|
71
51
|
// src/App.ts
|
|
52
|
+
function isComponent(value) {
|
|
53
|
+
if (!value) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
if (typeof value === "object" && "$$typeof" in value) {
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
if (typeof value === "function") {
|
|
60
|
+
const fn = value;
|
|
61
|
+
return fn.prototype?.isReactComponent === true || fn.name !== "" || fn.displayName !== void 0;
|
|
62
|
+
}
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
function isRenderFunction(value) {
|
|
66
|
+
if (typeof value !== "function") {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
const fn = value;
|
|
70
|
+
return fn.length === 1 && typeof fn.prototype === "undefined";
|
|
71
|
+
}
|
|
72
72
|
var currentIsInitialPage = true;
|
|
73
73
|
var routerIsInitialized = false;
|
|
74
74
|
var swapComponent = async () => {
|
|
@@ -80,15 +80,16 @@ function App({
|
|
|
80
80
|
initialComponent,
|
|
81
81
|
resolveComponent,
|
|
82
82
|
titleCallback,
|
|
83
|
-
onHeadUpdate
|
|
83
|
+
onHeadUpdate,
|
|
84
|
+
defaultLayout
|
|
84
85
|
}) {
|
|
85
|
-
const [current, setCurrent] =
|
|
86
|
+
const [current, setCurrent] = useState({
|
|
86
87
|
component: initialComponent || null,
|
|
87
88
|
page: { ...initialPage, flash: initialPage.flash ?? {} },
|
|
88
89
|
key: null
|
|
89
90
|
});
|
|
90
|
-
const headManager = (
|
|
91
|
-
return
|
|
91
|
+
const headManager = useMemo2(() => {
|
|
92
|
+
return createHeadManager(
|
|
92
93
|
typeof window === "undefined",
|
|
93
94
|
titleCallback || ((title) => title),
|
|
94
95
|
onHeadUpdate || (() => {
|
|
@@ -96,7 +97,7 @@ function App({
|
|
|
96
97
|
);
|
|
97
98
|
}, []);
|
|
98
99
|
if (!routerIsInitialized) {
|
|
99
|
-
|
|
100
|
+
router.init({
|
|
100
101
|
initialPage,
|
|
101
102
|
resolveComponent,
|
|
102
103
|
swapComponent: async (args) => swapComponent(args),
|
|
@@ -109,13 +110,16 @@ function App({
|
|
|
109
110
|
});
|
|
110
111
|
routerIsInitialized = true;
|
|
111
112
|
}
|
|
112
|
-
|
|
113
|
+
useEffect(() => {
|
|
113
114
|
swapComponent = async ({ component, page, preserveState }) => {
|
|
114
115
|
if (currentIsInitialPage) {
|
|
115
116
|
currentIsInitialPage = false;
|
|
116
117
|
return;
|
|
117
118
|
}
|
|
118
|
-
(
|
|
119
|
+
if (!preserveState) {
|
|
120
|
+
resetLayoutProps();
|
|
121
|
+
}
|
|
122
|
+
flushSync(
|
|
119
123
|
() => setCurrent((current2) => ({
|
|
120
124
|
component,
|
|
121
125
|
page,
|
|
@@ -123,29 +127,37 @@ function App({
|
|
|
123
127
|
}))
|
|
124
128
|
);
|
|
125
129
|
};
|
|
126
|
-
|
|
130
|
+
router.on("navigate", () => headManager.forceUpdate());
|
|
127
131
|
}, []);
|
|
128
132
|
if (!current.component) {
|
|
129
|
-
return
|
|
133
|
+
return createElement(
|
|
130
134
|
HeadContext_default.Provider,
|
|
131
135
|
{ value: headManager },
|
|
132
|
-
|
|
136
|
+
createElement(PageContext_default.Provider, { value: current.page }, null)
|
|
133
137
|
);
|
|
134
138
|
}
|
|
135
139
|
const renderChildren = children || (({ Component, props, key }) => {
|
|
136
|
-
const child =
|
|
137
|
-
if (
|
|
140
|
+
const child = createElement(Component, { key, ...props });
|
|
141
|
+
if (Component.layout && isRenderFunction(Component.layout)) {
|
|
138
142
|
return Component.layout(child);
|
|
139
143
|
}
|
|
140
|
-
|
|
141
|
-
|
|
144
|
+
const effectiveLayout = Component.layout ?? defaultLayout?.(current.page.component, current.page);
|
|
145
|
+
const layouts = normalizeLayouts(effectiveLayout, isComponent, Component.layout ? isRenderFunction : void 0);
|
|
146
|
+
if (layouts.length > 0) {
|
|
147
|
+
return layouts.reduceRight((childNode, layout) => {
|
|
148
|
+
return createElement(
|
|
149
|
+
LayoutPropsContext.Provider,
|
|
150
|
+
{ value: { staticProps: { ...props, ...layout.props }, name: layout.name } },
|
|
151
|
+
createElement(layout.component, { ...props, ...layout.props }, childNode)
|
|
152
|
+
);
|
|
153
|
+
}, child);
|
|
142
154
|
}
|
|
143
155
|
return child;
|
|
144
156
|
});
|
|
145
|
-
return
|
|
157
|
+
return createElement(
|
|
146
158
|
HeadContext_default.Provider,
|
|
147
159
|
{ value: headManager },
|
|
148
|
-
|
|
160
|
+
createElement(
|
|
149
161
|
PageContext_default.Provider,
|
|
150
162
|
{ value: current.page },
|
|
151
163
|
renderChildren({
|
|
@@ -159,8 +171,15 @@ function App({
|
|
|
159
171
|
App.displayName = "Inertia";
|
|
160
172
|
|
|
161
173
|
// src/createInertiaApp.ts
|
|
162
|
-
|
|
163
|
-
|
|
174
|
+
import {
|
|
175
|
+
buildSSRBody,
|
|
176
|
+
getInitialPageFromDOM,
|
|
177
|
+
http as httpModule,
|
|
178
|
+
router as router2,
|
|
179
|
+
setupProgress
|
|
180
|
+
} from "@inertiajs/core";
|
|
181
|
+
import { createElement as createElement2, StrictMode } from "react";
|
|
182
|
+
import { createRoot, hydrateRoot } from "react-dom/client";
|
|
164
183
|
async function createInertiaApp({
|
|
165
184
|
id = "app",
|
|
166
185
|
resolve,
|
|
@@ -169,87 +188,105 @@ async function createInertiaApp({
|
|
|
169
188
|
progress: progress2 = {},
|
|
170
189
|
page,
|
|
171
190
|
render,
|
|
172
|
-
defaults = {}
|
|
173
|
-
|
|
191
|
+
defaults = {},
|
|
192
|
+
http: http3,
|
|
193
|
+
layout,
|
|
194
|
+
strictMode = false
|
|
195
|
+
} = {}) {
|
|
174
196
|
config.replace(defaults);
|
|
197
|
+
if (http3) {
|
|
198
|
+
httpModule.setClient(http3);
|
|
199
|
+
}
|
|
175
200
|
const isServer = typeof window === "undefined";
|
|
176
|
-
const
|
|
177
|
-
|
|
178
|
-
|
|
201
|
+
const wrapWithStrictMode = (element) => {
|
|
202
|
+
return strictMode ? createElement2(StrictMode, null, element) : element;
|
|
203
|
+
};
|
|
204
|
+
const resolveComponent = (name, page2) => Promise.resolve(resolve(name, page2)).then((module) => {
|
|
205
|
+
return module.default || module;
|
|
206
|
+
});
|
|
207
|
+
if (isServer && !page && !render) {
|
|
208
|
+
return async (page2, renderToString) => {
|
|
209
|
+
let head2 = [];
|
|
210
|
+
const initialComponent = await resolveComponent(page2.component, page2);
|
|
211
|
+
const props = {
|
|
212
|
+
initialPage: page2,
|
|
213
|
+
initialComponent,
|
|
214
|
+
resolveComponent,
|
|
215
|
+
titleCallback: title,
|
|
216
|
+
onHeadUpdate: (elements) => head2 = elements,
|
|
217
|
+
defaultLayout: layout
|
|
218
|
+
};
|
|
219
|
+
let reactApp2;
|
|
220
|
+
if (setup) {
|
|
221
|
+
reactApp2 = setup({
|
|
222
|
+
el: null,
|
|
223
|
+
App,
|
|
224
|
+
props
|
|
225
|
+
});
|
|
226
|
+
} else {
|
|
227
|
+
reactApp2 = wrapWithStrictMode(createElement2(App, props));
|
|
228
|
+
}
|
|
229
|
+
const html = renderToString(reactApp2);
|
|
230
|
+
const body = buildSSRBody(id, page2, html);
|
|
231
|
+
return { head: head2, body };
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
const initialPage = page || getInitialPageFromDOM(id);
|
|
179
235
|
let head = [];
|
|
180
236
|
const reactApp = await Promise.all([
|
|
181
|
-
resolveComponent(initialPage.component),
|
|
182
|
-
|
|
237
|
+
resolveComponent(initialPage.component, initialPage),
|
|
238
|
+
router2.decryptHistory().catch(() => {
|
|
183
239
|
})
|
|
184
240
|
]).then(([initialComponent]) => {
|
|
185
241
|
const props = {
|
|
186
242
|
initialPage,
|
|
187
243
|
initialComponent,
|
|
188
244
|
resolveComponent,
|
|
189
|
-
titleCallback: title
|
|
245
|
+
titleCallback: title,
|
|
246
|
+
onHeadUpdate: isServer ? (elements) => head = elements : void 0,
|
|
247
|
+
defaultLayout: layout
|
|
190
248
|
};
|
|
191
249
|
if (isServer) {
|
|
192
|
-
|
|
193
|
-
return ssrSetup({
|
|
250
|
+
return setup({
|
|
194
251
|
el: null,
|
|
195
252
|
App,
|
|
196
|
-
props
|
|
253
|
+
props
|
|
197
254
|
});
|
|
198
255
|
}
|
|
199
|
-
const
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
256
|
+
const el = document.getElementById(id);
|
|
257
|
+
if (setup) {
|
|
258
|
+
return setup({
|
|
259
|
+
el,
|
|
260
|
+
App,
|
|
261
|
+
props
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
const appElement = wrapWithStrictMode(createElement2(App, props));
|
|
265
|
+
if (el.hasAttribute("data-server-rendered")) {
|
|
266
|
+
hydrateRoot(el, appElement);
|
|
267
|
+
} else {
|
|
268
|
+
createRoot(el).render(appElement);
|
|
269
|
+
}
|
|
205
270
|
});
|
|
206
271
|
if (!isServer && progress2) {
|
|
207
|
-
|
|
272
|
+
setupProgress(progress2);
|
|
208
273
|
}
|
|
209
|
-
if (isServer && render) {
|
|
210
|
-
const
|
|
211
|
-
|
|
212
|
-
return (0, import_react4.createElement)(
|
|
213
|
-
"div",
|
|
214
|
-
{
|
|
215
|
-
id,
|
|
216
|
-
"data-page": JSON.stringify(initialPage)
|
|
217
|
-
},
|
|
218
|
-
reactApp
|
|
219
|
-
);
|
|
220
|
-
}
|
|
221
|
-
return (0, import_react4.createElement)(
|
|
222
|
-
import_react4.Fragment,
|
|
223
|
-
null,
|
|
224
|
-
(0, import_react4.createElement)("script", {
|
|
225
|
-
"data-page": id,
|
|
226
|
-
type: "application/json",
|
|
227
|
-
dangerouslySetInnerHTML: { __html: JSON.stringify(initialPage).replace(/\//g, "\\/") }
|
|
228
|
-
}),
|
|
229
|
-
(0, import_react4.createElement)("div", { id }, reactApp)
|
|
230
|
-
);
|
|
231
|
-
};
|
|
232
|
-
const body = await render(element());
|
|
274
|
+
if (isServer && render && reactApp) {
|
|
275
|
+
const html = render(reactApp);
|
|
276
|
+
const body = buildSSRBody(id, initialPage, html);
|
|
233
277
|
return { head, body };
|
|
234
278
|
}
|
|
235
279
|
}
|
|
236
280
|
|
|
237
281
|
// src/Deferred.ts
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
var import_react6 = __toESM(require("react"), 1);
|
|
242
|
-
|
|
243
|
-
// src/react.ts
|
|
244
|
-
var import_react5 = __toESM(require("react"), 1);
|
|
245
|
-
function useIsomorphicLayoutEffect(effect, deps) {
|
|
246
|
-
typeof window === "undefined" ? (0, import_react5.useEffect)(effect, deps) : (0, import_react5.useLayoutEffect)(effect, deps);
|
|
247
|
-
}
|
|
248
|
-
var isReact19 = typeof import_react5.default.use === "function";
|
|
282
|
+
import { isSameUrlWithoutQueryOrHash } from "@inertiajs/core";
|
|
283
|
+
import { get } from "lodash-es";
|
|
284
|
+
import { useEffect as useEffect2, useMemo as useMemo3, useRef, useState as useState2 } from "react";
|
|
249
285
|
|
|
250
286
|
// src/usePage.ts
|
|
287
|
+
import { use } from "react";
|
|
251
288
|
function usePage() {
|
|
252
|
-
const page =
|
|
289
|
+
const page = use(PageContext_default);
|
|
253
290
|
if (!page) {
|
|
254
291
|
throw new Error("usePage must be used within the Inertia component");
|
|
255
292
|
}
|
|
@@ -257,39 +294,54 @@ function usePage() {
|
|
|
257
294
|
}
|
|
258
295
|
|
|
259
296
|
// src/Deferred.ts
|
|
260
|
-
var
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
297
|
+
var keysAreBeingReloaded = (only, except, keys) => {
|
|
298
|
+
if (only.length === 0 && except.length === 0) {
|
|
299
|
+
return true;
|
|
300
|
+
}
|
|
301
|
+
if (only.length > 0) {
|
|
302
|
+
return keys.some((key) => only.includes(key));
|
|
303
|
+
}
|
|
304
|
+
return keys.some((key) => !except.includes(key));
|
|
267
305
|
};
|
|
268
306
|
var Deferred = ({ children, data, fallback }) => {
|
|
269
307
|
if (!data) {
|
|
270
308
|
throw new Error("`<Deferred>` requires a `data` prop to be a string or array of strings");
|
|
271
309
|
}
|
|
272
|
-
const [loaded, setLoaded] = (
|
|
310
|
+
const [loaded, setLoaded] = useState2(false);
|
|
311
|
+
const [reloading, setReloading] = useState2(false);
|
|
312
|
+
const activeReloads = useRef(/* @__PURE__ */ new Set());
|
|
273
313
|
const pageProps = usePage().props;
|
|
274
|
-
const keys = (
|
|
275
|
-
(
|
|
276
|
-
const
|
|
277
|
-
const
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
314
|
+
const keys = useMemo3(() => Array.isArray(data) ? data : [data], [data]);
|
|
315
|
+
useEffect2(() => {
|
|
316
|
+
const removeStartListener = router3.on("start", (e) => {
|
|
317
|
+
const visit = e.detail.visit;
|
|
318
|
+
if (visit.preserveState === true && isSameUrlWithoutQueryOrHash(visit.url, window.location) && keysAreBeingReloaded(visit.only, visit.except, keys)) {
|
|
319
|
+
activeReloads.current.add(visit);
|
|
320
|
+
setReloading(true);
|
|
321
|
+
}
|
|
322
|
+
});
|
|
323
|
+
const removeFinishListener = router3.on("finish", (e) => {
|
|
324
|
+
const visit = e.detail.visit;
|
|
325
|
+
if (activeReloads.current.has(visit)) {
|
|
326
|
+
activeReloads.current.delete(visit);
|
|
327
|
+
setReloading(activeReloads.current.size > 0);
|
|
281
328
|
}
|
|
282
329
|
});
|
|
283
330
|
return () => {
|
|
284
|
-
|
|
331
|
+
removeStartListener();
|
|
332
|
+
removeFinishListener();
|
|
333
|
+
activeReloads.current.clear();
|
|
285
334
|
};
|
|
286
|
-
}, []);
|
|
287
|
-
(
|
|
288
|
-
setLoaded(keys.every((key) => pageProps
|
|
335
|
+
}, [keys]);
|
|
336
|
+
useEffect2(() => {
|
|
337
|
+
setLoaded(keys.every((key) => get(pageProps, key) !== void 0));
|
|
289
338
|
}, [pageProps, keys]);
|
|
290
|
-
const propsAreDefined = (
|
|
339
|
+
const propsAreDefined = useMemo3(() => keys.every((key) => get(pageProps, key) !== void 0), [keys, pageProps]);
|
|
291
340
|
if (loaded && propsAreDefined) {
|
|
292
|
-
|
|
341
|
+
if (typeof children === "function") {
|
|
342
|
+
return children({ reloading });
|
|
343
|
+
}
|
|
344
|
+
return children;
|
|
293
345
|
}
|
|
294
346
|
return typeof fallback === "function" ? fallback() : fallback;
|
|
295
347
|
};
|
|
@@ -297,170 +349,93 @@ Deferred.displayName = "InertiaDeferred";
|
|
|
297
349
|
var Deferred_default = Deferred;
|
|
298
350
|
|
|
299
351
|
// src/Form.ts
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
352
|
+
import {
|
|
353
|
+
config as config2,
|
|
354
|
+
FormComponentResetSymbol,
|
|
355
|
+
formDataToObject,
|
|
356
|
+
isUrlMethodPair,
|
|
357
|
+
mergeDataIntoQueryString,
|
|
358
|
+
resetFormFields,
|
|
359
|
+
resolveUrlMethodPairComponent,
|
|
360
|
+
UseFormUtils as UseFormUtils3
|
|
361
|
+
} from "@inertiajs/core";
|
|
362
|
+
import { isEqual as isEqual2 } from "lodash-es";
|
|
363
|
+
import React, {
|
|
364
|
+
createContext as createContext4,
|
|
365
|
+
createElement as createElement3,
|
|
366
|
+
forwardRef,
|
|
367
|
+
use as use2,
|
|
368
|
+
useEffect as useEffect6,
|
|
369
|
+
useImperativeHandle,
|
|
370
|
+
useMemo as useMemo5,
|
|
371
|
+
useRef as useRef4,
|
|
372
|
+
useState as useState6
|
|
373
|
+
} from "react";
|
|
303
374
|
|
|
304
375
|
// src/useForm.ts
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
376
|
+
import {
|
|
377
|
+
router as router5,
|
|
378
|
+
UseFormUtils as UseFormUtils2
|
|
379
|
+
} from "@inertiajs/core";
|
|
380
|
+
import { cloneDeep as cloneDeep2 } from "lodash-es";
|
|
381
|
+
import { useCallback as useCallback2, useRef as useRef3, useState as useState5 } from "react";
|
|
309
382
|
|
|
310
|
-
// src/
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
const [state, setState] = (0, import_react9.useState)(() => {
|
|
315
|
-
const restored = import_core3.router.restore(key);
|
|
316
|
-
return restored !== void 0 ? restored : initialState;
|
|
317
|
-
});
|
|
318
|
-
(0, import_react9.useEffect)(() => {
|
|
319
|
-
const keys = excludeKeysRef?.current;
|
|
320
|
-
if (keys && keys.length > 0 && typeof state === "object" && state !== null) {
|
|
321
|
-
const filtered = { ...state };
|
|
322
|
-
keys.forEach((k) => delete filtered[k]);
|
|
323
|
-
import_core3.router.remember(filtered, key);
|
|
324
|
-
} else {
|
|
325
|
-
import_core3.router.remember(state, key);
|
|
326
|
-
}
|
|
327
|
-
}, [state, key]);
|
|
328
|
-
return [state, setState];
|
|
383
|
+
// src/react.ts
|
|
384
|
+
import { useEffect as useEffect3, useLayoutEffect } from "react";
|
|
385
|
+
function useIsomorphicLayoutEffect(effect, deps) {
|
|
386
|
+
typeof window === "undefined" ? useEffect3(effect, deps) : useLayoutEffect(effect, deps);
|
|
329
387
|
}
|
|
330
388
|
|
|
331
|
-
// src/
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
const
|
|
344
|
-
const
|
|
345
|
-
const
|
|
346
|
-
const
|
|
347
|
-
const
|
|
348
|
-
const
|
|
349
|
-
const [
|
|
350
|
-
const
|
|
351
|
-
const
|
|
352
|
-
const
|
|
353
|
-
const [
|
|
354
|
-
const [
|
|
355
|
-
const [
|
|
356
|
-
const
|
|
357
|
-
(0
|
|
389
|
+
// src/useFormState.ts
|
|
390
|
+
import {
|
|
391
|
+
UseFormUtils
|
|
392
|
+
} from "@inertiajs/core";
|
|
393
|
+
import {
|
|
394
|
+
createValidator,
|
|
395
|
+
resolveName,
|
|
396
|
+
toSimpleValidationErrors
|
|
397
|
+
} from "laravel-precognition";
|
|
398
|
+
import { cloneDeep, get as get2, has, isEqual, set } from "lodash-es";
|
|
399
|
+
import { useCallback, useEffect as useEffect4, useMemo as useMemo4, useRef as useRef2, useState as useState3 } from "react";
|
|
400
|
+
function useFormState(options) {
|
|
401
|
+
const { data: dataOption, useDataState, useErrorsState } = options;
|
|
402
|
+
const isDataFunction = typeof dataOption === "function";
|
|
403
|
+
const resolveData = () => isDataFunction ? dataOption() : dataOption;
|
|
404
|
+
const initialData = cloneDeep(resolveData());
|
|
405
|
+
const isMounted = useRef2(false);
|
|
406
|
+
const precognitionEndpointRef = useRef2(options.precognitionEndpoint ?? null);
|
|
407
|
+
const [defaults, setDefaultsState] = useState3(cloneDeep(initialData));
|
|
408
|
+
const [data, setData] = useDataState ? useDataState() : useState3(cloneDeep(initialData));
|
|
409
|
+
const [errors, setErrors] = useErrorsState ? useErrorsState() : useState3({});
|
|
410
|
+
const [hasErrors, setHasErrors] = useState3(false);
|
|
411
|
+
const [processing, setProcessing] = useState3(false);
|
|
412
|
+
const [progress2, setProgress] = useState3(null);
|
|
413
|
+
const [wasSuccessful, setWasSuccessful] = useState3(false);
|
|
414
|
+
const [recentlySuccessful, setRecentlySuccessful] = useState3(false);
|
|
415
|
+
const recentlySuccessfulTimeoutId = useRef2(void 0);
|
|
416
|
+
const transformRef = useRef2((data2) => data2);
|
|
417
|
+
const isDirty = useMemo4(() => !isEqual(data, defaults), [data, defaults]);
|
|
418
|
+
const defaultsCalledInOnSuccessRef = useRef2(false);
|
|
419
|
+
const validatorRef = useRef2(null);
|
|
420
|
+
const [validating, setValidating] = useState3(false);
|
|
421
|
+
const [touchedFields, setTouchedFields] = useState3([]);
|
|
422
|
+
const [validFields, setValidFields] = useState3([]);
|
|
423
|
+
const withAllErrorsRef = useRef2(null);
|
|
424
|
+
const withAllErrorsEnabled = () => withAllErrorsRef.current ?? config.get("form.withAllErrors");
|
|
425
|
+
const dataRef = useRef2(data);
|
|
426
|
+
useEffect4(() => {
|
|
427
|
+
dataRef.current = data;
|
|
428
|
+
});
|
|
429
|
+
useEffect4(() => {
|
|
358
430
|
isMounted.current = true;
|
|
359
431
|
return () => {
|
|
360
432
|
isMounted.current = false;
|
|
361
433
|
};
|
|
362
434
|
}, []);
|
|
363
|
-
const
|
|
364
|
-
const submit = (0, import_react10.useCallback)(
|
|
365
|
-
(...args2) => {
|
|
366
|
-
const { method, url, options } = import_core4.UseFormUtils.parseSubmitArguments(args2, precognitionEndpoint.current);
|
|
367
|
-
setDefaultsCalledInOnSuccess.current = false;
|
|
368
|
-
const _options = {
|
|
369
|
-
...options,
|
|
370
|
-
onCancelToken: (token) => {
|
|
371
|
-
cancelToken.current = token;
|
|
372
|
-
if (options.onCancelToken) {
|
|
373
|
-
return options.onCancelToken(token);
|
|
374
|
-
}
|
|
375
|
-
},
|
|
376
|
-
onBefore: (visit) => {
|
|
377
|
-
setWasSuccessful(false);
|
|
378
|
-
setRecentlySuccessful(false);
|
|
379
|
-
clearTimeout(recentlySuccessfulTimeoutId.current);
|
|
380
|
-
if (options.onBefore) {
|
|
381
|
-
return options.onBefore(visit);
|
|
382
|
-
}
|
|
383
|
-
},
|
|
384
|
-
onStart: (visit) => {
|
|
385
|
-
setProcessing(true);
|
|
386
|
-
if (options.onStart) {
|
|
387
|
-
return options.onStart(visit);
|
|
388
|
-
}
|
|
389
|
-
},
|
|
390
|
-
onProgress: (event) => {
|
|
391
|
-
setProgress(event || null);
|
|
392
|
-
if (options.onProgress) {
|
|
393
|
-
return options.onProgress(event);
|
|
394
|
-
}
|
|
395
|
-
},
|
|
396
|
-
onSuccess: async (page) => {
|
|
397
|
-
if (isMounted.current) {
|
|
398
|
-
setProcessing(false);
|
|
399
|
-
setProgress(null);
|
|
400
|
-
setErrors({});
|
|
401
|
-
setHasErrors(false);
|
|
402
|
-
setWasSuccessful(true);
|
|
403
|
-
setRecentlySuccessful(true);
|
|
404
|
-
recentlySuccessfulTimeoutId.current = setTimeout(() => {
|
|
405
|
-
if (isMounted.current) {
|
|
406
|
-
setRecentlySuccessful(false);
|
|
407
|
-
}
|
|
408
|
-
}, config.get("form.recentlySuccessfulDuration"));
|
|
409
|
-
}
|
|
410
|
-
const onSuccess = options.onSuccess ? await options.onSuccess(page) : null;
|
|
411
|
-
if (isMounted.current && !setDefaultsCalledInOnSuccess.current) {
|
|
412
|
-
setData((data2) => {
|
|
413
|
-
setDefaults((0, import_lodash_es.cloneDeep)(data2));
|
|
414
|
-
return data2;
|
|
415
|
-
});
|
|
416
|
-
}
|
|
417
|
-
return onSuccess;
|
|
418
|
-
},
|
|
419
|
-
onError: (errors2) => {
|
|
420
|
-
if (isMounted.current) {
|
|
421
|
-
setProcessing(false);
|
|
422
|
-
setProgress(null);
|
|
423
|
-
setErrors(errors2);
|
|
424
|
-
setHasErrors(Object.keys(errors2).length > 0);
|
|
425
|
-
validatorRef.current?.setErrors(errors2);
|
|
426
|
-
}
|
|
427
|
-
if (options.onError) {
|
|
428
|
-
return options.onError(errors2);
|
|
429
|
-
}
|
|
430
|
-
},
|
|
431
|
-
onCancel: () => {
|
|
432
|
-
if (isMounted.current) {
|
|
433
|
-
setProcessing(false);
|
|
434
|
-
setProgress(null);
|
|
435
|
-
}
|
|
436
|
-
if (options.onCancel) {
|
|
437
|
-
return options.onCancel();
|
|
438
|
-
}
|
|
439
|
-
},
|
|
440
|
-
onFinish: (visit) => {
|
|
441
|
-
if (isMounted.current) {
|
|
442
|
-
setProcessing(false);
|
|
443
|
-
setProgress(null);
|
|
444
|
-
}
|
|
445
|
-
cancelToken.current = null;
|
|
446
|
-
if (options.onFinish) {
|
|
447
|
-
return options.onFinish(visit);
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
};
|
|
451
|
-
const transformedData = transform.current(data);
|
|
452
|
-
if (method === "delete") {
|
|
453
|
-
import_core4.router.delete(url, { ..._options, data: transformedData });
|
|
454
|
-
} else {
|
|
455
|
-
import_core4.router[method](url, transformedData, _options);
|
|
456
|
-
}
|
|
457
|
-
},
|
|
458
|
-
[data, setErrors, transform]
|
|
459
|
-
);
|
|
460
|
-
const setDataFunction = (0, import_react10.useCallback)(
|
|
435
|
+
const setDataFunction = useCallback(
|
|
461
436
|
(keyOrData, maybeValue) => {
|
|
462
437
|
if (typeof keyOrData === "string") {
|
|
463
|
-
setData((data2) =>
|
|
438
|
+
setData((data2) => set(cloneDeep(data2), keyOrData, maybeValue));
|
|
464
439
|
} else if (typeof keyOrData === "function") {
|
|
465
440
|
setData((data2) => keyOrData(data2));
|
|
466
441
|
} else {
|
|
@@ -469,47 +444,49 @@ function useForm(...args) {
|
|
|
469
444
|
},
|
|
470
445
|
[setData]
|
|
471
446
|
);
|
|
472
|
-
const
|
|
473
|
-
const dataRef = (0, import_react10.useRef)(data);
|
|
474
|
-
(0, import_react10.useEffect)(() => {
|
|
475
|
-
dataRef.current = data;
|
|
476
|
-
});
|
|
477
|
-
const setDefaultsFunction = (0, import_react10.useCallback)(
|
|
447
|
+
const setDefaultsFunction = useCallback(
|
|
478
448
|
(fieldOrFields, maybeValue) => {
|
|
479
|
-
|
|
449
|
+
if (isDataFunction) {
|
|
450
|
+
throw new Error("You cannot call `defaults()` when using a function to define your form data.");
|
|
451
|
+
}
|
|
452
|
+
defaultsCalledInOnSuccessRef.current = true;
|
|
480
453
|
let newDefaults = {};
|
|
481
454
|
if (typeof fieldOrFields === "undefined") {
|
|
482
455
|
newDefaults = { ...dataRef.current };
|
|
483
|
-
|
|
484
|
-
setDataAsDefaults(true);
|
|
456
|
+
setDefaultsState(dataRef.current);
|
|
485
457
|
} else {
|
|
486
|
-
|
|
487
|
-
newDefaults = typeof fieldOrFields === "string" ?
|
|
458
|
+
setDefaultsState((defaults2) => {
|
|
459
|
+
newDefaults = typeof fieldOrFields === "string" ? set(cloneDeep(defaults2), fieldOrFields, maybeValue) : Object.assign(cloneDeep(defaults2), fieldOrFields);
|
|
488
460
|
return newDefaults;
|
|
489
461
|
});
|
|
490
462
|
}
|
|
491
463
|
validatorRef.current?.defaults(newDefaults);
|
|
492
464
|
},
|
|
493
|
-
[
|
|
465
|
+
[setDefaultsState]
|
|
494
466
|
);
|
|
495
|
-
|
|
496
|
-
if (!dataAsDefaults) {
|
|
497
|
-
return;
|
|
498
|
-
}
|
|
499
|
-
if (isDirty) {
|
|
500
|
-
setDefaults(data);
|
|
501
|
-
}
|
|
502
|
-
setDataAsDefaults(false);
|
|
503
|
-
}, [dataAsDefaults]);
|
|
504
|
-
const reset = (0, import_react10.useCallback)(
|
|
467
|
+
const reset = useCallback(
|
|
505
468
|
(...fields) => {
|
|
469
|
+
const resolvedData = isDataFunction ? cloneDeep(resolveData()) : defaults;
|
|
470
|
+
const clonedData = cloneDeep(resolvedData);
|
|
506
471
|
if (fields.length === 0) {
|
|
507
|
-
|
|
472
|
+
if (isDataFunction) {
|
|
473
|
+
setDefaultsState(clonedData);
|
|
474
|
+
}
|
|
475
|
+
setData(clonedData);
|
|
508
476
|
} else {
|
|
477
|
+
if (isDataFunction) {
|
|
478
|
+
setDefaultsState((currentDefaults) => {
|
|
479
|
+
const newDefaults = cloneDeep(currentDefaults);
|
|
480
|
+
fields.filter((key) => has(clonedData, key)).forEach((key) => {
|
|
481
|
+
set(newDefaults, key, get2(clonedData, key));
|
|
482
|
+
});
|
|
483
|
+
return newDefaults;
|
|
484
|
+
});
|
|
485
|
+
}
|
|
509
486
|
setData(
|
|
510
|
-
(data2) => fields.filter((key) =>
|
|
487
|
+
(data2) => fields.filter((key) => has(clonedData, key)).reduce(
|
|
511
488
|
(carry, key) => {
|
|
512
|
-
return
|
|
489
|
+
return set(carry, key, get2(clonedData, key));
|
|
513
490
|
},
|
|
514
491
|
{ ...data2 }
|
|
515
492
|
)
|
|
@@ -519,7 +496,7 @@ function useForm(...args) {
|
|
|
519
496
|
},
|
|
520
497
|
[setData, defaults]
|
|
521
498
|
);
|
|
522
|
-
const setError =
|
|
499
|
+
const setError = useCallback(
|
|
523
500
|
(fieldOrFields, maybeValue) => {
|
|
524
501
|
setErrors((errors2) => {
|
|
525
502
|
const newErrors = {
|
|
@@ -533,7 +510,7 @@ function useForm(...args) {
|
|
|
533
510
|
},
|
|
534
511
|
[setErrors, setHasErrors]
|
|
535
512
|
);
|
|
536
|
-
const clearErrors =
|
|
513
|
+
const clearErrors = useCallback(
|
|
537
514
|
(...fields) => {
|
|
538
515
|
setErrors((errors2) => {
|
|
539
516
|
const newErrors = Object.keys(errors2).reduce(
|
|
@@ -556,29 +533,48 @@ function useForm(...args) {
|
|
|
556
533
|
},
|
|
557
534
|
[setErrors, setHasErrors]
|
|
558
535
|
);
|
|
559
|
-
const resetAndClearErrors =
|
|
536
|
+
const resetAndClearErrors = useCallback(
|
|
560
537
|
(...fields) => {
|
|
561
538
|
reset(...fields);
|
|
562
539
|
clearErrors(...fields);
|
|
563
540
|
},
|
|
564
541
|
[reset, clearErrors]
|
|
565
542
|
);
|
|
566
|
-
const
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
543
|
+
const markAsSuccessful = useCallback(() => {
|
|
544
|
+
clearErrors();
|
|
545
|
+
setWasSuccessful(true);
|
|
546
|
+
setRecentlySuccessful(true);
|
|
547
|
+
recentlySuccessfulTimeoutId.current = window.setTimeout(() => {
|
|
548
|
+
if (isMounted.current) {
|
|
549
|
+
setRecentlySuccessful(false);
|
|
550
|
+
}
|
|
551
|
+
}, config.get("form.recentlySuccessfulDuration"));
|
|
552
|
+
}, [clearErrors, setWasSuccessful, setRecentlySuccessful]);
|
|
553
|
+
const resetBeforeSubmit = useCallback(() => {
|
|
554
|
+
setWasSuccessful(false);
|
|
555
|
+
setRecentlySuccessful(false);
|
|
556
|
+
clearTimeout(recentlySuccessfulTimeoutId.current);
|
|
557
|
+
}, [setWasSuccessful, setRecentlySuccessful]);
|
|
558
|
+
const finishProcessing = useCallback(() => {
|
|
559
|
+
setProcessing(false);
|
|
560
|
+
setProgress(null);
|
|
561
|
+
}, [setProcessing, setProgress]);
|
|
562
|
+
const transformFunction = useCallback((callback) => {
|
|
563
|
+
transformRef.current = callback;
|
|
581
564
|
}, []);
|
|
565
|
+
const tap = (value, callback) => {
|
|
566
|
+
callback(value);
|
|
567
|
+
return value;
|
|
568
|
+
};
|
|
569
|
+
const valid = useCallback(
|
|
570
|
+
(field) => validFields.includes(field),
|
|
571
|
+
[validFields]
|
|
572
|
+
);
|
|
573
|
+
const invalid = useCallback((field) => field in errors, [errors]);
|
|
574
|
+
const touched = useCallback(
|
|
575
|
+
(field) => typeof field === "string" ? touchedFields.includes(field) : touchedFields.length > 0,
|
|
576
|
+
[touchedFields]
|
|
577
|
+
);
|
|
582
578
|
const form = {
|
|
583
579
|
data,
|
|
584
580
|
setData: setDataFunction,
|
|
@@ -594,32 +590,8 @@ function useForm(...args) {
|
|
|
594
590
|
reset,
|
|
595
591
|
setError,
|
|
596
592
|
clearErrors,
|
|
597
|
-
resetAndClearErrors
|
|
598
|
-
submit,
|
|
599
|
-
get: getMethod,
|
|
600
|
-
post,
|
|
601
|
-
put,
|
|
602
|
-
patch,
|
|
603
|
-
delete: deleteMethod,
|
|
604
|
-
cancel,
|
|
605
|
-
dontRemember: (...keys) => {
|
|
606
|
-
excludeKeysRef.current = keys;
|
|
607
|
-
return form;
|
|
608
|
-
}
|
|
609
|
-
};
|
|
610
|
-
const tap = (value, callback) => {
|
|
611
|
-
callback(value);
|
|
612
|
-
return value;
|
|
593
|
+
resetAndClearErrors
|
|
613
594
|
};
|
|
614
|
-
const valid = (0, import_react10.useCallback)(
|
|
615
|
-
(field) => validFields.includes(field),
|
|
616
|
-
[validFields]
|
|
617
|
-
);
|
|
618
|
-
const invalid = (0, import_react10.useCallback)((field) => field in errors, [errors]);
|
|
619
|
-
const touched = (0, import_react10.useCallback)(
|
|
620
|
-
(field) => typeof field === "string" ? touchedFields.includes(field) : touchedFields.length > 0,
|
|
621
|
-
[touchedFields]
|
|
622
|
-
);
|
|
623
595
|
const validate = (field, config3) => {
|
|
624
596
|
if (typeof field === "object" && !("target" in field)) {
|
|
625
597
|
config3 = field;
|
|
@@ -628,22 +600,24 @@ function useForm(...args) {
|
|
|
628
600
|
if (field === void 0) {
|
|
629
601
|
validatorRef.current.validate(config3);
|
|
630
602
|
} else {
|
|
631
|
-
const fieldName =
|
|
632
|
-
const
|
|
633
|
-
|
|
634
|
-
validatorRef.current.validate(fieldName, (0, import_lodash_es.get)(transformedData, fieldName), config3);
|
|
603
|
+
const fieldName = resolveName(field);
|
|
604
|
+
const transformedData = transformRef.current(dataRef.current);
|
|
605
|
+
validatorRef.current.validate(fieldName, get2(transformedData, fieldName), config3);
|
|
635
606
|
}
|
|
636
607
|
return form;
|
|
637
608
|
};
|
|
638
|
-
const withPrecognition = (...
|
|
639
|
-
|
|
609
|
+
const withPrecognition = (...args) => {
|
|
610
|
+
precognitionEndpointRef.current = UseFormUtils.createWayfinderCallback(...args);
|
|
640
611
|
if (!validatorRef.current) {
|
|
641
|
-
const validator =
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
612
|
+
const validator = createValidator(
|
|
613
|
+
(client) => {
|
|
614
|
+
const { method, url } = precognitionEndpointRef.current();
|
|
615
|
+
const currentData = dataRef.current;
|
|
616
|
+
const transformedData = transformRef.current(currentData);
|
|
617
|
+
return client[method](url, transformedData);
|
|
618
|
+
},
|
|
619
|
+
cloneDeep(defaults)
|
|
620
|
+
);
|
|
647
621
|
validatorRef.current = validator;
|
|
648
622
|
validator.on("validatingChanged", () => {
|
|
649
623
|
setValidating(validator.validating());
|
|
@@ -652,7 +626,7 @@ function useForm(...args) {
|
|
|
652
626
|
}).on("touchedChanged", () => {
|
|
653
627
|
setTouchedFields(validator.touched());
|
|
654
628
|
}).on("errorsChanged", () => {
|
|
655
|
-
const validationErrors =
|
|
629
|
+
const validationErrors = withAllErrorsEnabled() ? validator.errors() : toSimpleValidationErrors(validator.errors());
|
|
656
630
|
setErrors(validationErrors);
|
|
657
631
|
setHasErrors(Object.keys(validationErrors).length > 0);
|
|
658
632
|
setValidFields(validator.valid());
|
|
@@ -675,29 +649,221 @@ function useForm(...args) {
|
|
|
675
649
|
}
|
|
676
650
|
return precognitiveForm;
|
|
677
651
|
},
|
|
678
|
-
withAllErrors: () => tap(precognitiveForm, () =>
|
|
652
|
+
withAllErrors: () => tap(precognitiveForm, () => withAllErrorsRef.current = true),
|
|
679
653
|
setValidationTimeout: (duration) => tap(precognitiveForm, () => validatorRef.current?.setTimeout(duration)),
|
|
680
654
|
validateFiles: () => tap(precognitiveForm, () => validatorRef.current?.validateFiles()),
|
|
681
655
|
validate,
|
|
682
656
|
setErrors: (errors2) => tap(precognitiveForm, () => form.setError(errors2)),
|
|
683
657
|
forgetError: (field) => tap(
|
|
684
658
|
precognitiveForm,
|
|
685
|
-
() => form.clearErrors(
|
|
659
|
+
() => form.clearErrors(resolveName(field))
|
|
686
660
|
)
|
|
687
661
|
});
|
|
688
662
|
return precognitiveForm;
|
|
689
663
|
};
|
|
690
664
|
form.withPrecognition = withPrecognition;
|
|
691
|
-
|
|
665
|
+
if (precognitionEndpointRef.current) {
|
|
666
|
+
form.withPrecognition(precognitionEndpointRef.current);
|
|
667
|
+
}
|
|
668
|
+
return {
|
|
669
|
+
form,
|
|
670
|
+
setDefaultsState,
|
|
671
|
+
transformRef,
|
|
672
|
+
precognitionEndpointRef,
|
|
673
|
+
dataRef,
|
|
674
|
+
isMounted,
|
|
675
|
+
setProcessing,
|
|
676
|
+
setProgress,
|
|
677
|
+
markAsSuccessful,
|
|
678
|
+
clearErrors,
|
|
679
|
+
setError,
|
|
680
|
+
defaultsCalledInOnSuccessRef,
|
|
681
|
+
resetBeforeSubmit,
|
|
682
|
+
finishProcessing,
|
|
683
|
+
withAllErrors: {
|
|
684
|
+
enabled: withAllErrorsEnabled,
|
|
685
|
+
enable: () => {
|
|
686
|
+
withAllErrorsRef.current = true;
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
};
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
// src/useRemember.ts
|
|
693
|
+
import { router as router4 } from "@inertiajs/core";
|
|
694
|
+
import { useEffect as useEffect5, useState as useState4 } from "react";
|
|
695
|
+
function useRemember(initialState, key, excludeKeysRef) {
|
|
696
|
+
const [state, setState] = useState4(() => {
|
|
697
|
+
const restored = router4.restore(key);
|
|
698
|
+
return restored !== void 0 ? restored : initialState;
|
|
699
|
+
});
|
|
700
|
+
useEffect5(() => {
|
|
701
|
+
const keys = excludeKeysRef?.current;
|
|
702
|
+
if (keys && keys.length > 0 && typeof state === "object" && state !== null) {
|
|
703
|
+
const filtered = { ...state };
|
|
704
|
+
keys.forEach((k) => delete filtered[k]);
|
|
705
|
+
router4.remember(filtered, key);
|
|
706
|
+
} else {
|
|
707
|
+
router4.remember(state, key);
|
|
708
|
+
}
|
|
709
|
+
}, [state, key]);
|
|
710
|
+
return [state, setState];
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
// src/useForm.ts
|
|
714
|
+
function useForm(...args) {
|
|
715
|
+
const { rememberKey, data, precognitionEndpoint } = UseFormUtils2.parseUseFormArguments(...args);
|
|
716
|
+
const initialDefaults = typeof data === "function" ? cloneDeep2(data()) : cloneDeep2(data);
|
|
717
|
+
const cancelToken = useRef3(null);
|
|
718
|
+
const excludeKeysRef = useRef3([]);
|
|
719
|
+
const pendingOptimisticRef = useRef3(null);
|
|
720
|
+
const useDataState = rememberKey ? () => useRemember(initialDefaults, `${rememberKey}:data`, excludeKeysRef) : void 0;
|
|
721
|
+
const useErrorsState = rememberKey ? () => useRemember({}, `${rememberKey}:errors`) : void 0;
|
|
722
|
+
const {
|
|
723
|
+
form: baseForm,
|
|
724
|
+
setDefaultsState,
|
|
725
|
+
transformRef,
|
|
726
|
+
precognitionEndpointRef,
|
|
727
|
+
isMounted,
|
|
728
|
+
setProcessing,
|
|
729
|
+
setProgress,
|
|
730
|
+
markAsSuccessful,
|
|
731
|
+
clearErrors,
|
|
732
|
+
setError,
|
|
733
|
+
defaultsCalledInOnSuccessRef,
|
|
734
|
+
resetBeforeSubmit,
|
|
735
|
+
finishProcessing
|
|
736
|
+
} = useFormState({
|
|
737
|
+
data,
|
|
738
|
+
precognitionEndpoint,
|
|
739
|
+
useDataState,
|
|
740
|
+
useErrorsState
|
|
741
|
+
});
|
|
742
|
+
const [dataAsDefaults, setDataAsDefaults] = useState5(false);
|
|
743
|
+
const originalSetDefaults = baseForm.setDefaults;
|
|
744
|
+
baseForm.setDefaults = useCallback2(
|
|
745
|
+
(fieldOrFields, maybeValue) => {
|
|
746
|
+
if (typeof fieldOrFields === "undefined") {
|
|
747
|
+
setDataAsDefaults(true);
|
|
748
|
+
}
|
|
749
|
+
return originalSetDefaults(fieldOrFields, maybeValue);
|
|
750
|
+
},
|
|
751
|
+
[originalSetDefaults]
|
|
752
|
+
);
|
|
753
|
+
useIsomorphicLayoutEffect(() => {
|
|
754
|
+
if (!dataAsDefaults) {
|
|
755
|
+
return;
|
|
756
|
+
}
|
|
757
|
+
if (baseForm.isDirty) {
|
|
758
|
+
setDefaultsState(baseForm.data);
|
|
759
|
+
}
|
|
760
|
+
setDataAsDefaults(false);
|
|
761
|
+
}, [dataAsDefaults]);
|
|
762
|
+
const submit = useCallback2(
|
|
763
|
+
(...args2) => {
|
|
764
|
+
const { method, url, options } = UseFormUtils2.parseSubmitArguments(args2, precognitionEndpointRef.current);
|
|
765
|
+
defaultsCalledInOnSuccessRef.current = false;
|
|
766
|
+
const _options = {
|
|
767
|
+
...options,
|
|
768
|
+
onCancelToken: (token) => {
|
|
769
|
+
cancelToken.current = token;
|
|
770
|
+
return options.onCancelToken?.(token);
|
|
771
|
+
},
|
|
772
|
+
onBefore: (visit) => {
|
|
773
|
+
resetBeforeSubmit();
|
|
774
|
+
return options.onBefore?.(visit);
|
|
775
|
+
},
|
|
776
|
+
onStart: (visit) => {
|
|
777
|
+
setProcessing(true);
|
|
778
|
+
return options.onStart?.(visit);
|
|
779
|
+
},
|
|
780
|
+
onProgress: (event) => {
|
|
781
|
+
setProgress(event || null);
|
|
782
|
+
return options.onProgress?.(event);
|
|
783
|
+
},
|
|
784
|
+
onSuccess: async (page) => {
|
|
785
|
+
if (isMounted.current) {
|
|
786
|
+
markAsSuccessful();
|
|
787
|
+
}
|
|
788
|
+
const onSuccess = options.onSuccess ? await options.onSuccess(page) : null;
|
|
789
|
+
if (isMounted.current && !defaultsCalledInOnSuccessRef.current) {
|
|
790
|
+
baseForm.setData((data2) => {
|
|
791
|
+
setDefaultsState(cloneDeep2(data2));
|
|
792
|
+
return data2;
|
|
793
|
+
});
|
|
794
|
+
}
|
|
795
|
+
return onSuccess;
|
|
796
|
+
},
|
|
797
|
+
onError: (errors) => {
|
|
798
|
+
if (isMounted.current) {
|
|
799
|
+
clearErrors();
|
|
800
|
+
setError(errors);
|
|
801
|
+
}
|
|
802
|
+
return options.onError?.(errors);
|
|
803
|
+
},
|
|
804
|
+
onCancel: () => {
|
|
805
|
+
return options.onCancel?.();
|
|
806
|
+
},
|
|
807
|
+
onFinish: (visit) => {
|
|
808
|
+
if (isMounted.current) {
|
|
809
|
+
finishProcessing();
|
|
810
|
+
}
|
|
811
|
+
cancelToken.current = null;
|
|
812
|
+
return options.onFinish?.(visit);
|
|
813
|
+
}
|
|
814
|
+
};
|
|
815
|
+
_options.optimistic = _options.optimistic ?? pendingOptimisticRef.current ?? void 0;
|
|
816
|
+
pendingOptimisticRef.current = null;
|
|
817
|
+
const transformedData = transformRef.current(baseForm.data);
|
|
818
|
+
if (method === "delete") {
|
|
819
|
+
router5.delete(url, { ..._options, data: transformedData });
|
|
820
|
+
} else {
|
|
821
|
+
router5[method](url, transformedData, _options);
|
|
822
|
+
}
|
|
823
|
+
},
|
|
824
|
+
[baseForm.data, clearErrors, setError, transformRef]
|
|
825
|
+
);
|
|
826
|
+
const cancel = useCallback2(() => {
|
|
827
|
+
if (cancelToken.current) {
|
|
828
|
+
cancelToken.current.cancel();
|
|
829
|
+
}
|
|
830
|
+
}, []);
|
|
831
|
+
const createSubmitMethod = (method) => (url, options = {}) => {
|
|
832
|
+
submit(method, url, options);
|
|
833
|
+
};
|
|
834
|
+
Object.assign(baseForm, {
|
|
835
|
+
submit,
|
|
836
|
+
get: createSubmitMethod("get"),
|
|
837
|
+
post: createSubmitMethod("post"),
|
|
838
|
+
put: createSubmitMethod("put"),
|
|
839
|
+
patch: createSubmitMethod("patch"),
|
|
840
|
+
delete: createSubmitMethod("delete"),
|
|
841
|
+
cancel,
|
|
842
|
+
dontRemember: (...keys) => {
|
|
843
|
+
excludeKeysRef.current = keys;
|
|
844
|
+
return form;
|
|
845
|
+
},
|
|
846
|
+
optimistic: (callback) => {
|
|
847
|
+
pendingOptimisticRef.current = callback;
|
|
848
|
+
return form;
|
|
849
|
+
}
|
|
850
|
+
});
|
|
851
|
+
const form = baseForm;
|
|
852
|
+
const originalWithPrecognition = baseForm.withPrecognition;
|
|
853
|
+
form.withPrecognition = (...args2) => {
|
|
854
|
+
originalWithPrecognition(...args2);
|
|
855
|
+
return form;
|
|
856
|
+
};
|
|
857
|
+
return precognitionEndpointRef.current ? form : form;
|
|
692
858
|
}
|
|
693
859
|
|
|
694
860
|
// src/Form.ts
|
|
695
861
|
var deferStateUpdate = (callback) => {
|
|
696
|
-
typeof
|
|
862
|
+
typeof React.startTransition === "function" ? React.startTransition(callback) : setTimeout(callback, 0);
|
|
697
863
|
};
|
|
698
864
|
var noop = () => void 0;
|
|
699
|
-
var FormContext = (
|
|
700
|
-
var Form =
|
|
865
|
+
var FormContext = createContext4(void 0);
|
|
866
|
+
var Form = forwardRef(
|
|
701
867
|
({
|
|
702
868
|
action = "",
|
|
703
869
|
method = "get",
|
|
@@ -706,6 +872,7 @@ var Form = (0, import_react12.forwardRef)(
|
|
|
706
872
|
errorBag = null,
|
|
707
873
|
showProgress = true,
|
|
708
874
|
transform = (data) => data,
|
|
875
|
+
optimistic,
|
|
709
876
|
options = {},
|
|
710
877
|
onStart = noop,
|
|
711
878
|
onProgress = noop,
|
|
@@ -724,6 +891,8 @@ var Form = (0, import_react12.forwardRef)(
|
|
|
724
891
|
validateFiles = false,
|
|
725
892
|
validationTimeout = 1500,
|
|
726
893
|
withAllErrors = null,
|
|
894
|
+
component = null,
|
|
895
|
+
instant = false,
|
|
727
896
|
children,
|
|
728
897
|
...props
|
|
729
898
|
}, ref) => {
|
|
@@ -738,39 +907,48 @@ var Form = (0, import_react12.forwardRef)(
|
|
|
738
907
|
if (validateFiles) {
|
|
739
908
|
form.validateFiles();
|
|
740
909
|
}
|
|
741
|
-
if (withAllErrors ??
|
|
910
|
+
if (withAllErrors ?? config2.get("form.withAllErrors")) {
|
|
742
911
|
form.withAllErrors();
|
|
743
912
|
}
|
|
744
913
|
form.transform(getTransformedData);
|
|
745
|
-
const formElement = (
|
|
746
|
-
const resolvedMethod = (
|
|
747
|
-
return
|
|
914
|
+
const formElement = useRef4(void 0);
|
|
915
|
+
const resolvedMethod = useMemo5(() => {
|
|
916
|
+
return isUrlMethodPair(action) ? action.method : method.toLowerCase();
|
|
748
917
|
}, [action, method]);
|
|
749
|
-
const
|
|
750
|
-
|
|
918
|
+
const resolvedComponent = useMemo5(() => {
|
|
919
|
+
if (component) {
|
|
920
|
+
return component;
|
|
921
|
+
}
|
|
922
|
+
if (instant && isUrlMethodPair(action)) {
|
|
923
|
+
return resolveUrlMethodPairComponent(action);
|
|
924
|
+
}
|
|
925
|
+
return null;
|
|
926
|
+
}, [component, instant, action]);
|
|
927
|
+
const [isDirty, setIsDirty] = useState6(false);
|
|
928
|
+
const defaultData = useRef4(new FormData());
|
|
751
929
|
const getFormData = (submitter) => new FormData(formElement.current, submitter);
|
|
752
|
-
const getData = (submitter) =>
|
|
930
|
+
const getData = (submitter) => formDataToObject(getFormData(submitter));
|
|
753
931
|
const getUrlAndData = (submitter) => {
|
|
754
|
-
return
|
|
932
|
+
return mergeDataIntoQueryString(
|
|
755
933
|
resolvedMethod,
|
|
756
|
-
|
|
934
|
+
isUrlMethodPair(action) ? action.url : action,
|
|
757
935
|
getData(submitter),
|
|
758
936
|
queryStringArrayFormat
|
|
759
937
|
);
|
|
760
938
|
};
|
|
761
939
|
const updateDirtyState = (event) => {
|
|
762
|
-
if (event.type === "reset" && event.detail?.[
|
|
940
|
+
if (event.type === "reset" && event.detail?.[FormComponentResetSymbol]) {
|
|
763
941
|
event.preventDefault();
|
|
764
942
|
}
|
|
765
943
|
deferStateUpdate(
|
|
766
|
-
() => setIsDirty(event.type === "reset" ? false : !(
|
|
944
|
+
() => setIsDirty(event.type === "reset" ? false : !isEqual2(getData(), formDataToObject(defaultData.current)))
|
|
767
945
|
);
|
|
768
946
|
};
|
|
769
947
|
const clearErrors = (...names) => {
|
|
770
948
|
form.clearErrors(...names);
|
|
771
949
|
return form;
|
|
772
950
|
};
|
|
773
|
-
(
|
|
951
|
+
useEffect6(() => {
|
|
774
952
|
defaultData.current = getFormData();
|
|
775
953
|
form.setDefaults(getData());
|
|
776
954
|
const formEvents = ["input", "change", "reset"];
|
|
@@ -779,10 +957,10 @@ var Form = (0, import_react12.forwardRef)(
|
|
|
779
957
|
formEvents.forEach((e) => formElement.current?.removeEventListener(e, updateDirtyState));
|
|
780
958
|
};
|
|
781
959
|
}, []);
|
|
782
|
-
(
|
|
960
|
+
useEffect6(() => {
|
|
783
961
|
form.setValidationTimeout(validationTimeout);
|
|
784
962
|
}, [validationTimeout]);
|
|
785
|
-
(
|
|
963
|
+
useEffect6(() => {
|
|
786
964
|
if (validateFiles) {
|
|
787
965
|
form.validateFiles();
|
|
788
966
|
} else {
|
|
@@ -791,7 +969,7 @@ var Form = (0, import_react12.forwardRef)(
|
|
|
791
969
|
}, [validateFiles]);
|
|
792
970
|
const reset = (...fields) => {
|
|
793
971
|
if (formElement.current) {
|
|
794
|
-
|
|
972
|
+
resetFormFields(formElement.current, defaultData.current, fields);
|
|
795
973
|
}
|
|
796
974
|
form.reset(...fields);
|
|
797
975
|
};
|
|
@@ -822,6 +1000,8 @@ var Form = (0, import_react12.forwardRef)(
|
|
|
822
1000
|
errorBag,
|
|
823
1001
|
showProgress,
|
|
824
1002
|
invalidateCacheTags,
|
|
1003
|
+
component: resolvedComponent,
|
|
1004
|
+
optimistic: optimistic ? (pageProps) => optimistic(pageProps, data) : void 0,
|
|
825
1005
|
onCancelToken,
|
|
826
1006
|
onBefore,
|
|
827
1007
|
onStart,
|
|
@@ -874,46 +1054,43 @@ var Form = (0, import_react12.forwardRef)(
|
|
|
874
1054
|
validating: form.validating,
|
|
875
1055
|
valid: form.valid,
|
|
876
1056
|
invalid: form.invalid,
|
|
877
|
-
validate: (field, config3) => form.validate(...
|
|
1057
|
+
validate: (field, config3) => form.validate(...UseFormUtils3.mergeHeadersForValidation(field, config3, headers)),
|
|
878
1058
|
touch: form.touch,
|
|
879
1059
|
touched: form.touched
|
|
880
1060
|
};
|
|
881
|
-
|
|
882
|
-
const formNode = (
|
|
1061
|
+
useImperativeHandle(ref, () => exposed, [form, isDirty, submit]);
|
|
1062
|
+
const formNode = createElement3(
|
|
883
1063
|
"form",
|
|
884
1064
|
{
|
|
885
1065
|
...props,
|
|
886
1066
|
ref: formElement,
|
|
887
|
-
action:
|
|
1067
|
+
action: isUrlMethodPair(action) ? action.url : action,
|
|
888
1068
|
method: resolvedMethod,
|
|
889
1069
|
onSubmit: (event) => {
|
|
890
1070
|
event.preventDefault();
|
|
891
1071
|
submit(event.nativeEvent.submitter);
|
|
892
1072
|
},
|
|
893
|
-
|
|
894
|
-
// a warning when receiving a string. Earlier versions require the string 'true'.
|
|
895
|
-
// See: https://github.com/inertiajs/inertia/pull/2536
|
|
896
|
-
inert: disableWhileProcessing && form.processing && (isReact19 ? true : "true")
|
|
1073
|
+
inert: disableWhileProcessing && form.processing
|
|
897
1074
|
},
|
|
898
1075
|
typeof children === "function" ? children(exposed) : children
|
|
899
1076
|
);
|
|
900
|
-
return (
|
|
1077
|
+
return createElement3(FormContext.Provider, { value: exposed }, formNode);
|
|
901
1078
|
}
|
|
902
1079
|
);
|
|
903
1080
|
Form.displayName = "InertiaForm";
|
|
904
1081
|
function useFormContext() {
|
|
905
|
-
return (
|
|
1082
|
+
return use2(FormContext);
|
|
906
1083
|
}
|
|
907
1084
|
var Form_default = Form;
|
|
908
1085
|
|
|
909
1086
|
// src/Head.ts
|
|
910
|
-
|
|
911
|
-
|
|
1087
|
+
import { escape } from "lodash-es";
|
|
1088
|
+
import React2, { use as use3, useEffect as useEffect7, useMemo as useMemo6 } from "react";
|
|
912
1089
|
var Head = function({ children, title }) {
|
|
913
|
-
const headManager = (
|
|
914
|
-
const provider = (
|
|
1090
|
+
const headManager = use3(HeadContext_default);
|
|
1091
|
+
const provider = useMemo6(() => headManager.createProvider(), [headManager]);
|
|
915
1092
|
const isServer = typeof window === "undefined";
|
|
916
|
-
(
|
|
1093
|
+
useEffect7(() => {
|
|
917
1094
|
provider.reconnect();
|
|
918
1095
|
provider.update(renderNodes(children));
|
|
919
1096
|
return () => {
|
|
@@ -948,7 +1125,7 @@ var Head = function({ children, title }) {
|
|
|
948
1125
|
if (value === "") {
|
|
949
1126
|
return carry + ` ${name}`;
|
|
950
1127
|
}
|
|
951
|
-
return carry + ` ${name}="${
|
|
1128
|
+
return carry + ` ${name}="${escape(value)}"`;
|
|
952
1129
|
}, "");
|
|
953
1130
|
return `<${String(node.type)}${attrs}>`;
|
|
954
1131
|
}
|
|
@@ -976,17 +1153,17 @@ var Head = function({ children, title }) {
|
|
|
976
1153
|
return html;
|
|
977
1154
|
}
|
|
978
1155
|
function ensureNodeHasInertiaProp(node) {
|
|
979
|
-
return
|
|
980
|
-
|
|
1156
|
+
return React2.cloneElement(node, {
|
|
1157
|
+
"data-inertia": node.props["head-key"] !== void 0 ? node.props["head-key"] : ""
|
|
981
1158
|
});
|
|
982
1159
|
}
|
|
983
1160
|
function renderNode(node) {
|
|
984
1161
|
return renderTag(ensureNodeHasInertiaProp(node));
|
|
985
1162
|
}
|
|
986
1163
|
function renderNodes(nodes) {
|
|
987
|
-
const elements =
|
|
1164
|
+
const elements = React2.Children.toArray(nodes).filter((node) => node).map((node) => renderNode(node));
|
|
988
1165
|
if (title && !elements.find((tag) => tag.startsWith("<title"))) {
|
|
989
|
-
elements.push(`<title
|
|
1166
|
+
elements.push(`<title data-inertia="">${title}</title>`);
|
|
990
1167
|
}
|
|
991
1168
|
return elements;
|
|
992
1169
|
}
|
|
@@ -998,8 +1175,20 @@ var Head = function({ children, title }) {
|
|
|
998
1175
|
var Head_default = Head;
|
|
999
1176
|
|
|
1000
1177
|
// src/InfiniteScroll.ts
|
|
1001
|
-
|
|
1002
|
-
|
|
1178
|
+
import {
|
|
1179
|
+
getScrollableParent,
|
|
1180
|
+
useInfiniteScroll
|
|
1181
|
+
} from "@inertiajs/core";
|
|
1182
|
+
import React3, {
|
|
1183
|
+
createElement as createElement4,
|
|
1184
|
+
forwardRef as forwardRef2,
|
|
1185
|
+
useCallback as useCallback3,
|
|
1186
|
+
useEffect as useEffect8,
|
|
1187
|
+
useImperativeHandle as useImperativeHandle2,
|
|
1188
|
+
useMemo as useMemo7,
|
|
1189
|
+
useRef as useRef5,
|
|
1190
|
+
useState as useState7
|
|
1191
|
+
} from "react";
|
|
1003
1192
|
var resolveHTMLElement = (value, fallback) => {
|
|
1004
1193
|
if (!value) {
|
|
1005
1194
|
return fallback;
|
|
@@ -1018,7 +1207,7 @@ var renderSlot = (slotContent, slotProps, fallback = null) => {
|
|
|
1018
1207
|
}
|
|
1019
1208
|
return typeof slotContent === "function" ? slotContent(slotProps) : slotContent;
|
|
1020
1209
|
};
|
|
1021
|
-
var InfiniteScroll = (
|
|
1210
|
+
var InfiniteScroll = forwardRef2(
|
|
1022
1211
|
({
|
|
1023
1212
|
data,
|
|
1024
1213
|
buffer = 0,
|
|
@@ -1035,55 +1224,58 @@ var InfiniteScroll = (0, import_react15.forwardRef)(
|
|
|
1035
1224
|
previous,
|
|
1036
1225
|
next,
|
|
1037
1226
|
loading,
|
|
1227
|
+
params = {},
|
|
1038
1228
|
onlyNext = false,
|
|
1039
1229
|
onlyPrevious = false,
|
|
1040
1230
|
...props
|
|
1041
1231
|
}, ref) => {
|
|
1042
|
-
const [startElementFromRef, setStartElementFromRef] = (
|
|
1043
|
-
const startElementRef = (
|
|
1044
|
-
const [endElementFromRef, setEndElementFromRef] = (
|
|
1045
|
-
const endElementRef = (
|
|
1046
|
-
const [itemsElementFromRef, setItemsElementFromRef] = (
|
|
1047
|
-
const itemsElementRef = (
|
|
1048
|
-
const [loadingPrevious, setLoadingPrevious] = (
|
|
1049
|
-
const [loadingNext, setLoadingNext] = (
|
|
1050
|
-
const [requestCount, setRequestCount] = (0
|
|
1051
|
-
const [hasPreviousPage, setHasPreviousPage] = (
|
|
1052
|
-
const [hasNextPage, setHasNextPage] = (
|
|
1053
|
-
const [resolvedStartElement, setResolvedStartElement] = (
|
|
1054
|
-
const [resolvedEndElement, setResolvedEndElement] = (
|
|
1055
|
-
const [resolvedItemsElement, setResolvedItemsElement] = (
|
|
1056
|
-
(
|
|
1232
|
+
const [startElementFromRef, setStartElementFromRef] = useState7(null);
|
|
1233
|
+
const startElementRef = useCallback3((node) => setStartElementFromRef(node), []);
|
|
1234
|
+
const [endElementFromRef, setEndElementFromRef] = useState7(null);
|
|
1235
|
+
const endElementRef = useCallback3((node) => setEndElementFromRef(node), []);
|
|
1236
|
+
const [itemsElementFromRef, setItemsElementFromRef] = useState7(null);
|
|
1237
|
+
const itemsElementRef = useCallback3((node) => setItemsElementFromRef(node), []);
|
|
1238
|
+
const [loadingPrevious, setLoadingPrevious] = useState7(false);
|
|
1239
|
+
const [loadingNext, setLoadingNext] = useState7(false);
|
|
1240
|
+
const [requestCount, setRequestCount] = useState7(0);
|
|
1241
|
+
const [hasPreviousPage, setHasPreviousPage] = useState7(false);
|
|
1242
|
+
const [hasNextPage, setHasNextPage] = useState7(false);
|
|
1243
|
+
const [resolvedStartElement, setResolvedStartElement] = useState7(null);
|
|
1244
|
+
const [resolvedEndElement, setResolvedEndElement] = useState7(null);
|
|
1245
|
+
const [resolvedItemsElement, setResolvedItemsElement] = useState7(null);
|
|
1246
|
+
useEffect8(() => {
|
|
1057
1247
|
const element = startElement ? resolveHTMLElement(startElement, startElementFromRef) : startElementFromRef;
|
|
1058
1248
|
setResolvedStartElement(element);
|
|
1059
1249
|
}, [startElement, startElementFromRef]);
|
|
1060
|
-
(
|
|
1250
|
+
useEffect8(() => {
|
|
1061
1251
|
const element = endElement ? resolveHTMLElement(endElement, endElementFromRef) : endElementFromRef;
|
|
1062
1252
|
setResolvedEndElement(element);
|
|
1063
1253
|
}, [endElement, endElementFromRef]);
|
|
1064
|
-
(
|
|
1254
|
+
useEffect8(() => {
|
|
1065
1255
|
const element = itemsElement ? resolveHTMLElement(itemsElement, itemsElementFromRef) : itemsElementFromRef;
|
|
1066
1256
|
setResolvedItemsElement(element);
|
|
1067
1257
|
}, [itemsElement, itemsElementFromRef]);
|
|
1068
|
-
const scrollableParent = (
|
|
1069
|
-
const callbackPropsRef = (
|
|
1258
|
+
const scrollableParent = useMemo7(() => getScrollableParent(resolvedItemsElement), [resolvedItemsElement]);
|
|
1259
|
+
const callbackPropsRef = useRef5({
|
|
1070
1260
|
buffer,
|
|
1071
1261
|
onlyNext,
|
|
1072
1262
|
onlyPrevious,
|
|
1073
1263
|
reverse,
|
|
1074
|
-
preserveUrl
|
|
1264
|
+
preserveUrl,
|
|
1265
|
+
params
|
|
1075
1266
|
});
|
|
1076
1267
|
callbackPropsRef.current = {
|
|
1077
1268
|
buffer,
|
|
1078
1269
|
onlyNext,
|
|
1079
1270
|
onlyPrevious,
|
|
1080
1271
|
reverse,
|
|
1081
|
-
preserveUrl
|
|
1272
|
+
preserveUrl,
|
|
1273
|
+
params
|
|
1082
1274
|
};
|
|
1083
|
-
const [infiniteScroll, setInfiniteScroll] = (
|
|
1084
|
-
const dataManager = (
|
|
1085
|
-
const elementManager = (
|
|
1086
|
-
const scrollToBottom = (
|
|
1275
|
+
const [infiniteScroll, setInfiniteScroll] = useState7(null);
|
|
1276
|
+
const dataManager = useMemo7(() => infiniteScroll?.dataManager, [infiniteScroll]);
|
|
1277
|
+
const elementManager = useMemo7(() => infiniteScroll?.elementManager, [infiniteScroll]);
|
|
1278
|
+
const scrollToBottom = useCallback3(() => {
|
|
1087
1279
|
if (scrollableParent) {
|
|
1088
1280
|
scrollableParent.scrollTo({
|
|
1089
1281
|
top: scrollableParent.scrollHeight,
|
|
@@ -1096,7 +1288,7 @@ var InfiniteScroll = (0, import_react15.forwardRef)(
|
|
|
1096
1288
|
});
|
|
1097
1289
|
}
|
|
1098
1290
|
}, [scrollableParent]);
|
|
1099
|
-
(
|
|
1291
|
+
useEffect8(() => {
|
|
1100
1292
|
if (!resolvedItemsElement) {
|
|
1101
1293
|
return;
|
|
1102
1294
|
}
|
|
@@ -1105,13 +1297,14 @@ var InfiniteScroll = (0, import_react15.forwardRef)(
|
|
|
1105
1297
|
setHasPreviousPage(infiniteScrollInstance.dataManager.hasPrevious());
|
|
1106
1298
|
setHasNextPage(infiniteScrollInstance.dataManager.hasNext());
|
|
1107
1299
|
}
|
|
1108
|
-
const infiniteScrollInstance =
|
|
1300
|
+
const infiniteScrollInstance = useInfiniteScroll({
|
|
1109
1301
|
// Data
|
|
1110
1302
|
getPropName: () => data,
|
|
1111
1303
|
inReverseMode: () => callbackPropsRef.current.reverse,
|
|
1112
1304
|
shouldFetchNext: () => !callbackPropsRef.current.onlyPrevious,
|
|
1113
1305
|
shouldFetchPrevious: () => !callbackPropsRef.current.onlyNext,
|
|
1114
1306
|
shouldPreserveUrl: () => callbackPropsRef.current.preserveUrl,
|
|
1307
|
+
getReloadOptions: () => callbackPropsRef.current.params,
|
|
1115
1308
|
// Elements
|
|
1116
1309
|
getTriggerMargin: () => callbackPropsRef.current.buffer,
|
|
1117
1310
|
getStartElement: () => resolvedStartElement,
|
|
@@ -1144,21 +1337,21 @@ var InfiniteScroll = (0, import_react15.forwardRef)(
|
|
|
1144
1337
|
setInfiniteScroll(null);
|
|
1145
1338
|
};
|
|
1146
1339
|
}, [data, resolvedItemsElement, resolvedStartElement, resolvedEndElement, scrollableParent]);
|
|
1147
|
-
const manualMode = (
|
|
1340
|
+
const manualMode = useMemo7(
|
|
1148
1341
|
() => manual || manualAfter > 0 && requestCount >= manualAfter,
|
|
1149
1342
|
[manual, manualAfter, requestCount]
|
|
1150
1343
|
);
|
|
1151
|
-
const autoLoad = (
|
|
1152
|
-
(
|
|
1344
|
+
const autoLoad = useMemo7(() => !manualMode, [manualMode]);
|
|
1345
|
+
useEffect8(() => {
|
|
1153
1346
|
autoLoad ? elementManager?.enableTriggers() : elementManager?.disableTriggers();
|
|
1154
1347
|
}, [autoLoad, onlyNext, onlyPrevious, resolvedStartElement, resolvedEndElement]);
|
|
1155
|
-
(
|
|
1348
|
+
useEffect8(() => {
|
|
1156
1349
|
const shouldAutoScroll = autoScroll !== void 0 ? autoScroll : reverse;
|
|
1157
1350
|
if (shouldAutoScroll) {
|
|
1158
1351
|
scrollToBottom();
|
|
1159
1352
|
}
|
|
1160
1353
|
}, [scrollableParent]);
|
|
1161
|
-
(
|
|
1354
|
+
useImperativeHandle2(
|
|
1162
1355
|
ref,
|
|
1163
1356
|
() => ({
|
|
1164
1357
|
fetchNext: dataManager?.fetchNext || (() => {
|
|
@@ -1204,7 +1397,7 @@ var InfiniteScroll = (0, import_react15.forwardRef)(
|
|
|
1204
1397
|
const renderElements = [];
|
|
1205
1398
|
if (!startElement) {
|
|
1206
1399
|
renderElements.push(
|
|
1207
|
-
(
|
|
1400
|
+
createElement4(
|
|
1208
1401
|
"div",
|
|
1209
1402
|
{ ref: startElementRef },
|
|
1210
1403
|
// Render previous slot or fallback to loading indicator
|
|
@@ -1213,7 +1406,7 @@ var InfiniteScroll = (0, import_react15.forwardRef)(
|
|
|
1213
1406
|
);
|
|
1214
1407
|
}
|
|
1215
1408
|
renderElements.push(
|
|
1216
|
-
(
|
|
1409
|
+
createElement4(
|
|
1217
1410
|
as,
|
|
1218
1411
|
{ ...props, ref: itemsElementRef },
|
|
1219
1412
|
typeof children === "function" ? children(exposedSlot) : children
|
|
@@ -1221,7 +1414,7 @@ var InfiniteScroll = (0, import_react15.forwardRef)(
|
|
|
1221
1414
|
);
|
|
1222
1415
|
if (!endElement) {
|
|
1223
1416
|
renderElements.push(
|
|
1224
|
-
(
|
|
1417
|
+
createElement4(
|
|
1225
1418
|
"div",
|
|
1226
1419
|
{ ref: endElementRef },
|
|
1227
1420
|
// Render next slot or fallback to loading indicator
|
|
@@ -1229,17 +1422,24 @@ var InfiniteScroll = (0, import_react15.forwardRef)(
|
|
|
1229
1422
|
)
|
|
1230
1423
|
);
|
|
1231
1424
|
}
|
|
1232
|
-
return (
|
|
1425
|
+
return createElement4(React3.Fragment, {}, ...reverse ? [...renderElements].reverse() : renderElements);
|
|
1233
1426
|
}
|
|
1234
1427
|
);
|
|
1235
1428
|
InfiniteScroll.displayName = "InertiaInfiniteScroll";
|
|
1236
1429
|
var InfiniteScroll_default = InfiniteScroll;
|
|
1237
1430
|
|
|
1238
1431
|
// src/Link.ts
|
|
1239
|
-
|
|
1240
|
-
|
|
1432
|
+
import {
|
|
1433
|
+
isUrlMethodPair as isUrlMethodPair2,
|
|
1434
|
+
mergeDataIntoQueryString as mergeDataIntoQueryString2,
|
|
1435
|
+
resolveUrlMethodPairComponent as resolveUrlMethodPairComponent2,
|
|
1436
|
+
router as router6,
|
|
1437
|
+
shouldIntercept,
|
|
1438
|
+
shouldNavigate
|
|
1439
|
+
} from "@inertiajs/core";
|
|
1440
|
+
import { createElement as createElement5, forwardRef as forwardRef3, useEffect as useEffect9, useMemo as useMemo8, useRef as useRef6, useState as useState8 } from "react";
|
|
1241
1441
|
var noop2 = () => void 0;
|
|
1242
|
-
var Link = (
|
|
1442
|
+
var Link = forwardRef3(
|
|
1243
1443
|
({
|
|
1244
1444
|
children,
|
|
1245
1445
|
as = "a",
|
|
@@ -1270,26 +1470,38 @@ var Link = (0, import_react16.forwardRef)(
|
|
|
1270
1470
|
cacheFor = 0,
|
|
1271
1471
|
cacheTags = [],
|
|
1272
1472
|
viewTransition = false,
|
|
1473
|
+
component = null,
|
|
1474
|
+
instant = false,
|
|
1475
|
+
pageProps = null,
|
|
1273
1476
|
...props
|
|
1274
1477
|
}, ref) => {
|
|
1275
|
-
const [inFlightCount, setInFlightCount] = (0
|
|
1276
|
-
const hoverTimeout = (
|
|
1277
|
-
const _method = (
|
|
1278
|
-
return (
|
|
1478
|
+
const [inFlightCount, setInFlightCount] = useState8(0);
|
|
1479
|
+
const hoverTimeout = useRef6(void 0);
|
|
1480
|
+
const _method = useMemo8(() => {
|
|
1481
|
+
return isUrlMethodPair2(href) ? href.method : method.toLowerCase();
|
|
1279
1482
|
}, [href, method]);
|
|
1280
|
-
const
|
|
1483
|
+
const resolvedComponent = useMemo8(() => {
|
|
1484
|
+
if (component) {
|
|
1485
|
+
return component;
|
|
1486
|
+
}
|
|
1487
|
+
if (instant && isUrlMethodPair2(href)) {
|
|
1488
|
+
return resolveUrlMethodPairComponent2(href);
|
|
1489
|
+
}
|
|
1490
|
+
return null;
|
|
1491
|
+
}, [component, instant, href]);
|
|
1492
|
+
const _as = useMemo8(() => {
|
|
1281
1493
|
if (typeof as !== "string" || as.toLowerCase() !== "a") {
|
|
1282
1494
|
return as;
|
|
1283
1495
|
}
|
|
1284
1496
|
return _method !== "get" ? "button" : as.toLowerCase();
|
|
1285
1497
|
}, [as, _method]);
|
|
1286
|
-
const mergeDataArray = (
|
|
1287
|
-
() => (
|
|
1498
|
+
const mergeDataArray = useMemo8(
|
|
1499
|
+
() => mergeDataIntoQueryString2(_method, isUrlMethodPair2(href) ? href.url : href, data, queryStringArrayFormat),
|
|
1288
1500
|
[href, _method, data, queryStringArrayFormat]
|
|
1289
1501
|
);
|
|
1290
|
-
const url = (
|
|
1291
|
-
const _data = (
|
|
1292
|
-
const baseParams = (
|
|
1502
|
+
const url = useMemo8(() => mergeDataArray[0], [mergeDataArray]);
|
|
1503
|
+
const _data = useMemo8(() => mergeDataArray[1], [mergeDataArray]);
|
|
1504
|
+
const baseParams = useMemo8(
|
|
1293
1505
|
() => ({
|
|
1294
1506
|
data: _data,
|
|
1295
1507
|
method: _method,
|
|
@@ -1300,11 +1512,26 @@ var Link = (0, import_react16.forwardRef)(
|
|
|
1300
1512
|
only,
|
|
1301
1513
|
except,
|
|
1302
1514
|
headers,
|
|
1303
|
-
async
|
|
1515
|
+
async,
|
|
1516
|
+
component: resolvedComponent,
|
|
1517
|
+
pageProps
|
|
1304
1518
|
}),
|
|
1305
|
-
[
|
|
1519
|
+
[
|
|
1520
|
+
_data,
|
|
1521
|
+
_method,
|
|
1522
|
+
preserveScroll,
|
|
1523
|
+
preserveState,
|
|
1524
|
+
preserveUrl,
|
|
1525
|
+
replace,
|
|
1526
|
+
only,
|
|
1527
|
+
except,
|
|
1528
|
+
headers,
|
|
1529
|
+
async,
|
|
1530
|
+
resolvedComponent,
|
|
1531
|
+
pageProps
|
|
1532
|
+
]
|
|
1306
1533
|
);
|
|
1307
|
-
const visitParams = (
|
|
1534
|
+
const visitParams = useMemo8(
|
|
1308
1535
|
() => ({
|
|
1309
1536
|
...baseParams,
|
|
1310
1537
|
viewTransition,
|
|
@@ -1336,7 +1563,7 @@ var Link = (0, import_react16.forwardRef)(
|
|
|
1336
1563
|
onError
|
|
1337
1564
|
]
|
|
1338
1565
|
);
|
|
1339
|
-
const prefetchModes = (
|
|
1566
|
+
const prefetchModes = useMemo8(
|
|
1340
1567
|
() => {
|
|
1341
1568
|
if (prefetch === true) {
|
|
1342
1569
|
return ["hover"];
|
|
@@ -1351,7 +1578,7 @@ var Link = (0, import_react16.forwardRef)(
|
|
|
1351
1578
|
},
|
|
1352
1579
|
Array.isArray(prefetch) ? prefetch : [prefetch]
|
|
1353
1580
|
);
|
|
1354
|
-
const cacheForValue = (
|
|
1581
|
+
const cacheForValue = useMemo8(() => {
|
|
1355
1582
|
if (cacheFor !== 0) {
|
|
1356
1583
|
return cacheFor;
|
|
1357
1584
|
}
|
|
@@ -1360,9 +1587,9 @@ var Link = (0, import_react16.forwardRef)(
|
|
|
1360
1587
|
}
|
|
1361
1588
|
return config.get("prefetch.cacheFor");
|
|
1362
1589
|
}, [cacheFor, prefetchModes]);
|
|
1363
|
-
const doPrefetch = (
|
|
1590
|
+
const doPrefetch = useMemo8(() => {
|
|
1364
1591
|
return () => {
|
|
1365
|
-
|
|
1592
|
+
router6.prefetch(
|
|
1366
1593
|
url,
|
|
1367
1594
|
{
|
|
1368
1595
|
...baseParams,
|
|
@@ -1373,12 +1600,12 @@ var Link = (0, import_react16.forwardRef)(
|
|
|
1373
1600
|
);
|
|
1374
1601
|
};
|
|
1375
1602
|
}, [url, baseParams, onPrefetching, onPrefetched, cacheForValue, cacheTags]);
|
|
1376
|
-
(
|
|
1603
|
+
useEffect9(() => {
|
|
1377
1604
|
return () => {
|
|
1378
1605
|
clearTimeout(hoverTimeout.current);
|
|
1379
1606
|
};
|
|
1380
1607
|
}, []);
|
|
1381
|
-
(
|
|
1608
|
+
useEffect9(() => {
|
|
1382
1609
|
if (prefetchModes.includes("mount")) {
|
|
1383
1610
|
setTimeout(() => doPrefetch());
|
|
1384
1611
|
}
|
|
@@ -1386,9 +1613,9 @@ var Link = (0, import_react16.forwardRef)(
|
|
|
1386
1613
|
const regularEvents = {
|
|
1387
1614
|
onClick: (event) => {
|
|
1388
1615
|
onClick(event);
|
|
1389
|
-
if (
|
|
1616
|
+
if (shouldIntercept(event)) {
|
|
1390
1617
|
event.preventDefault();
|
|
1391
|
-
|
|
1618
|
+
router6.visit(url, visitParams);
|
|
1392
1619
|
}
|
|
1393
1620
|
}
|
|
1394
1621
|
};
|
|
@@ -1405,37 +1632,37 @@ var Link = (0, import_react16.forwardRef)(
|
|
|
1405
1632
|
};
|
|
1406
1633
|
const prefetchClickEvents = {
|
|
1407
1634
|
onMouseDown: (event) => {
|
|
1408
|
-
if (
|
|
1635
|
+
if (shouldIntercept(event)) {
|
|
1409
1636
|
event.preventDefault();
|
|
1410
1637
|
doPrefetch();
|
|
1411
1638
|
}
|
|
1412
1639
|
},
|
|
1413
1640
|
onKeyDown: (event) => {
|
|
1414
|
-
if (
|
|
1641
|
+
if (shouldNavigate(event)) {
|
|
1415
1642
|
event.preventDefault();
|
|
1416
1643
|
doPrefetch();
|
|
1417
1644
|
}
|
|
1418
1645
|
},
|
|
1419
1646
|
onMouseUp: (event) => {
|
|
1420
|
-
if (
|
|
1647
|
+
if (shouldIntercept(event)) {
|
|
1421
1648
|
event.preventDefault();
|
|
1422
|
-
|
|
1649
|
+
router6.visit(url, visitParams);
|
|
1423
1650
|
}
|
|
1424
1651
|
},
|
|
1425
1652
|
onKeyUp: (event) => {
|
|
1426
|
-
if (
|
|
1653
|
+
if (shouldNavigate(event)) {
|
|
1427
1654
|
event.preventDefault();
|
|
1428
|
-
|
|
1655
|
+
router6.visit(url, visitParams);
|
|
1429
1656
|
}
|
|
1430
1657
|
},
|
|
1431
1658
|
onClick: (event) => {
|
|
1432
1659
|
onClick(event);
|
|
1433
|
-
if (
|
|
1660
|
+
if (shouldIntercept(event)) {
|
|
1434
1661
|
event.preventDefault();
|
|
1435
1662
|
}
|
|
1436
1663
|
}
|
|
1437
1664
|
};
|
|
1438
|
-
const elProps = (
|
|
1665
|
+
const elProps = useMemo8(() => {
|
|
1439
1666
|
if (_as === "button") {
|
|
1440
1667
|
return { type: "button" };
|
|
1441
1668
|
}
|
|
@@ -1444,7 +1671,7 @@ var Link = (0, import_react16.forwardRef)(
|
|
|
1444
1671
|
}
|
|
1445
1672
|
return {};
|
|
1446
1673
|
}, [_as, url]);
|
|
1447
|
-
return (
|
|
1674
|
+
return createElement5(
|
|
1448
1675
|
_as,
|
|
1449
1676
|
{
|
|
1450
1677
|
...props,
|
|
@@ -1468,20 +1695,223 @@ var Link = (0, import_react16.forwardRef)(
|
|
|
1468
1695
|
Link.displayName = "InertiaLink";
|
|
1469
1696
|
var Link_default = Link;
|
|
1470
1697
|
|
|
1698
|
+
// src/useHttp.ts
|
|
1699
|
+
import {
|
|
1700
|
+
hasFiles,
|
|
1701
|
+
http,
|
|
1702
|
+
HttpCancelledError,
|
|
1703
|
+
HttpResponseError,
|
|
1704
|
+
mergeDataIntoQueryString as mergeDataIntoQueryString3,
|
|
1705
|
+
objectToFormData,
|
|
1706
|
+
UseFormUtils as UseFormUtils4
|
|
1707
|
+
} from "@inertiajs/core";
|
|
1708
|
+
import { toSimpleValidationErrors as toSimpleValidationErrors2 } from "laravel-precognition";
|
|
1709
|
+
import { cloneDeep as cloneDeep3 } from "lodash-es";
|
|
1710
|
+
import { useCallback as useCallback4, useRef as useRef7, useState as useState9 } from "react";
|
|
1711
|
+
function useHttp(...args) {
|
|
1712
|
+
const { rememberKey, data, precognitionEndpoint } = UseFormUtils4.parseUseFormArguments(...args);
|
|
1713
|
+
const initialDefaults = typeof data === "function" ? cloneDeep3(data()) : cloneDeep3(data);
|
|
1714
|
+
const abortController = useRef7(null);
|
|
1715
|
+
const excludeKeysRef = useRef7([]);
|
|
1716
|
+
const pendingOptimisticRef = useRef7(null);
|
|
1717
|
+
const [response, setResponse] = useState9(null);
|
|
1718
|
+
const useDataState = rememberKey ? () => useRemember(initialDefaults, `${rememberKey}:data`, excludeKeysRef) : void 0;
|
|
1719
|
+
const useErrorsState = rememberKey ? () => useRemember({}, `${rememberKey}:errors`) : void 0;
|
|
1720
|
+
const {
|
|
1721
|
+
form: baseForm,
|
|
1722
|
+
setDefaultsState,
|
|
1723
|
+
transformRef,
|
|
1724
|
+
dataRef,
|
|
1725
|
+
precognitionEndpointRef,
|
|
1726
|
+
isMounted,
|
|
1727
|
+
setProcessing,
|
|
1728
|
+
setProgress,
|
|
1729
|
+
markAsSuccessful,
|
|
1730
|
+
clearErrors,
|
|
1731
|
+
setError,
|
|
1732
|
+
defaultsCalledInOnSuccessRef,
|
|
1733
|
+
resetBeforeSubmit,
|
|
1734
|
+
finishProcessing,
|
|
1735
|
+
withAllErrors
|
|
1736
|
+
} = useFormState({
|
|
1737
|
+
data,
|
|
1738
|
+
precognitionEndpoint,
|
|
1739
|
+
useDataState,
|
|
1740
|
+
useErrorsState
|
|
1741
|
+
});
|
|
1742
|
+
const submit = useCallback4(
|
|
1743
|
+
async (method, url, options) => {
|
|
1744
|
+
const onBefore = options.onBefore?.();
|
|
1745
|
+
if (onBefore === false) {
|
|
1746
|
+
return Promise.reject(new Error("Request cancelled by onBefore"));
|
|
1747
|
+
}
|
|
1748
|
+
defaultsCalledInOnSuccessRef.current = false;
|
|
1749
|
+
if (isMounted.current) {
|
|
1750
|
+
resetBeforeSubmit();
|
|
1751
|
+
}
|
|
1752
|
+
abortController.current = new AbortController();
|
|
1753
|
+
const cancelToken = {
|
|
1754
|
+
cancel: () => abortController.current?.abort()
|
|
1755
|
+
};
|
|
1756
|
+
options.onCancelToken?.(cancelToken);
|
|
1757
|
+
options.optimistic = options.optimistic ?? pendingOptimisticRef.current ?? void 0;
|
|
1758
|
+
pendingOptimisticRef.current = null;
|
|
1759
|
+
let snapshot;
|
|
1760
|
+
if (options.optimistic) {
|
|
1761
|
+
snapshot = cloneDeep3(dataRef.current);
|
|
1762
|
+
const optimisticData = options.optimistic(cloneDeep3(snapshot));
|
|
1763
|
+
baseForm.setData((current) => ({ ...current, ...optimisticData }));
|
|
1764
|
+
}
|
|
1765
|
+
if (isMounted.current) {
|
|
1766
|
+
setProcessing(true);
|
|
1767
|
+
}
|
|
1768
|
+
options.onStart?.();
|
|
1769
|
+
const transformedData = transformRef.current(dataRef.current);
|
|
1770
|
+
const useFormData = hasFiles(transformedData);
|
|
1771
|
+
let requestUrl = url;
|
|
1772
|
+
let requestData;
|
|
1773
|
+
let contentType;
|
|
1774
|
+
if (method === "get") {
|
|
1775
|
+
const [urlWithParams] = mergeDataIntoQueryString3(
|
|
1776
|
+
method,
|
|
1777
|
+
url,
|
|
1778
|
+
transformedData
|
|
1779
|
+
);
|
|
1780
|
+
requestUrl = urlWithParams;
|
|
1781
|
+
} else {
|
|
1782
|
+
if (useFormData) {
|
|
1783
|
+
requestData = objectToFormData(transformedData);
|
|
1784
|
+
} else {
|
|
1785
|
+
requestData = JSON.stringify(transformedData);
|
|
1786
|
+
contentType = "application/json";
|
|
1787
|
+
}
|
|
1788
|
+
}
|
|
1789
|
+
try {
|
|
1790
|
+
const httpResponse = await http.getClient().request({
|
|
1791
|
+
method,
|
|
1792
|
+
url: requestUrl,
|
|
1793
|
+
data: requestData,
|
|
1794
|
+
headers: {
|
|
1795
|
+
Accept: "application/json",
|
|
1796
|
+
...contentType ? { "Content-Type": contentType } : {},
|
|
1797
|
+
...options.headers
|
|
1798
|
+
},
|
|
1799
|
+
signal: abortController.current.signal,
|
|
1800
|
+
onUploadProgress: (event) => {
|
|
1801
|
+
if (isMounted.current) {
|
|
1802
|
+
setProgress(event);
|
|
1803
|
+
}
|
|
1804
|
+
options.onProgress?.(event);
|
|
1805
|
+
}
|
|
1806
|
+
});
|
|
1807
|
+
const responseData = JSON.parse(httpResponse.data);
|
|
1808
|
+
if (httpResponse.status >= 200 && httpResponse.status < 300) {
|
|
1809
|
+
if (isMounted.current) {
|
|
1810
|
+
markAsSuccessful();
|
|
1811
|
+
setResponse(responseData);
|
|
1812
|
+
}
|
|
1813
|
+
options.onSuccess?.(responseData);
|
|
1814
|
+
if (isMounted.current && !defaultsCalledInOnSuccessRef.current) {
|
|
1815
|
+
baseForm.setData((data2) => {
|
|
1816
|
+
setDefaultsState(cloneDeep3(data2));
|
|
1817
|
+
return data2;
|
|
1818
|
+
});
|
|
1819
|
+
}
|
|
1820
|
+
return responseData;
|
|
1821
|
+
}
|
|
1822
|
+
throw new HttpResponseError(`Request failed with status ${httpResponse.status}`, httpResponse);
|
|
1823
|
+
} catch (error) {
|
|
1824
|
+
if (snapshot && isMounted.current) {
|
|
1825
|
+
baseForm.setData(snapshot);
|
|
1826
|
+
}
|
|
1827
|
+
if (error instanceof HttpResponseError) {
|
|
1828
|
+
if (error.response.status === 422) {
|
|
1829
|
+
const responseData = JSON.parse(error.response.data);
|
|
1830
|
+
const validationErrors = responseData.errors || {};
|
|
1831
|
+
const processedErrors = withAllErrors.enabled() ? validationErrors : toSimpleValidationErrors2(validationErrors);
|
|
1832
|
+
if (isMounted.current) {
|
|
1833
|
+
clearErrors();
|
|
1834
|
+
setError(processedErrors);
|
|
1835
|
+
}
|
|
1836
|
+
options.onError?.(processedErrors);
|
|
1837
|
+
}
|
|
1838
|
+
throw error;
|
|
1839
|
+
}
|
|
1840
|
+
if (error instanceof HttpCancelledError || error instanceof Error && error.name === "AbortError") {
|
|
1841
|
+
options.onCancel?.();
|
|
1842
|
+
throw new HttpCancelledError("Request was cancelled", url);
|
|
1843
|
+
}
|
|
1844
|
+
throw error;
|
|
1845
|
+
} finally {
|
|
1846
|
+
if (isMounted.current) {
|
|
1847
|
+
finishProcessing();
|
|
1848
|
+
}
|
|
1849
|
+
abortController.current = null;
|
|
1850
|
+
options.onFinish?.();
|
|
1851
|
+
}
|
|
1852
|
+
},
|
|
1853
|
+
[clearErrors, setError]
|
|
1854
|
+
);
|
|
1855
|
+
const cancel = useCallback4(() => {
|
|
1856
|
+
if (abortController.current) {
|
|
1857
|
+
abortController.current.abort();
|
|
1858
|
+
}
|
|
1859
|
+
}, []);
|
|
1860
|
+
const createSubmitMethod = (method) => async (url, options = {}) => {
|
|
1861
|
+
return submit(method, url, options);
|
|
1862
|
+
};
|
|
1863
|
+
const submitWithArgs = useCallback4(
|
|
1864
|
+
(...args2) => {
|
|
1865
|
+
const parsed = UseFormUtils4.parseSubmitArguments(args2, precognitionEndpointRef.current);
|
|
1866
|
+
return submit(parsed.method, parsed.url, parsed.options);
|
|
1867
|
+
},
|
|
1868
|
+
[submit]
|
|
1869
|
+
);
|
|
1870
|
+
Object.assign(baseForm, {
|
|
1871
|
+
response,
|
|
1872
|
+
submit: submitWithArgs,
|
|
1873
|
+
get: createSubmitMethod("get"),
|
|
1874
|
+
post: createSubmitMethod("post"),
|
|
1875
|
+
put: createSubmitMethod("put"),
|
|
1876
|
+
patch: createSubmitMethod("patch"),
|
|
1877
|
+
delete: createSubmitMethod("delete"),
|
|
1878
|
+
cancel,
|
|
1879
|
+
dontRemember: (...keys) => {
|
|
1880
|
+
excludeKeysRef.current = keys;
|
|
1881
|
+
return form;
|
|
1882
|
+
},
|
|
1883
|
+
optimistic: (callback) => {
|
|
1884
|
+
pendingOptimisticRef.current = callback;
|
|
1885
|
+
return form;
|
|
1886
|
+
},
|
|
1887
|
+
withAllErrors: () => {
|
|
1888
|
+
withAllErrors.enable();
|
|
1889
|
+
return form;
|
|
1890
|
+
}
|
|
1891
|
+
});
|
|
1892
|
+
const form = baseForm;
|
|
1893
|
+
const originalWithPrecognition = baseForm.withPrecognition;
|
|
1894
|
+
form.withPrecognition = (...args2) => {
|
|
1895
|
+
originalWithPrecognition(...args2);
|
|
1896
|
+
return form;
|
|
1897
|
+
};
|
|
1898
|
+
return precognitionEndpointRef.current ? form : form;
|
|
1899
|
+
}
|
|
1900
|
+
|
|
1471
1901
|
// src/usePoll.ts
|
|
1472
|
-
|
|
1473
|
-
|
|
1902
|
+
import { router as router7 } from "@inertiajs/core";
|
|
1903
|
+
import { useEffect as useEffect10, useRef as useRef8 } from "react";
|
|
1474
1904
|
function usePoll(interval, requestOptions = {}, options = {
|
|
1475
1905
|
keepAlive: false,
|
|
1476
1906
|
autoStart: true
|
|
1477
1907
|
}) {
|
|
1478
|
-
const pollRef = (
|
|
1479
|
-
|
|
1908
|
+
const pollRef = useRef8(
|
|
1909
|
+
router7.poll(interval, requestOptions, {
|
|
1480
1910
|
...options,
|
|
1481
1911
|
autoStart: false
|
|
1482
1912
|
})
|
|
1483
1913
|
);
|
|
1484
|
-
(
|
|
1914
|
+
useEffect10(() => {
|
|
1485
1915
|
if (options.autoStart ?? true) {
|
|
1486
1916
|
pollRef.current.start();
|
|
1487
1917
|
}
|
|
@@ -1494,21 +1924,21 @@ function usePoll(interval, requestOptions = {}, options = {
|
|
|
1494
1924
|
}
|
|
1495
1925
|
|
|
1496
1926
|
// src/usePrefetch.ts
|
|
1497
|
-
|
|
1498
|
-
|
|
1927
|
+
import { router as router8 } from "@inertiajs/core";
|
|
1928
|
+
import { useEffect as useEffect11, useState as useState10 } from "react";
|
|
1499
1929
|
function usePrefetch(options = {}) {
|
|
1500
|
-
const cached = typeof window === "undefined" ? null :
|
|
1501
|
-
const inFlight = typeof window === "undefined" ? null :
|
|
1502
|
-
const [lastUpdatedAt, setLastUpdatedAt] = (
|
|
1503
|
-
const [isPrefetching, setIsPrefetching] = (
|
|
1504
|
-
const [isPrefetched, setIsPrefetched] = (
|
|
1505
|
-
(
|
|
1506
|
-
const onPrefetchingListener =
|
|
1930
|
+
const cached = typeof window === "undefined" ? null : router8.getCached(window.location.pathname, options);
|
|
1931
|
+
const inFlight = typeof window === "undefined" ? null : router8.getPrefetching(window.location.pathname, options);
|
|
1932
|
+
const [lastUpdatedAt, setLastUpdatedAt] = useState10(cached?.staleTimestamp || null);
|
|
1933
|
+
const [isPrefetching, setIsPrefetching] = useState10(inFlight !== null);
|
|
1934
|
+
const [isPrefetched, setIsPrefetched] = useState10(cached !== null);
|
|
1935
|
+
useEffect11(() => {
|
|
1936
|
+
const onPrefetchingListener = router8.on("prefetching", (e) => {
|
|
1507
1937
|
if (e.detail.visit.url.pathname === window.location.pathname) {
|
|
1508
1938
|
setIsPrefetching(true);
|
|
1509
1939
|
}
|
|
1510
1940
|
});
|
|
1511
|
-
const onPrefetchedListener =
|
|
1941
|
+
const onPrefetchedListener = router8.on("prefetched", (e) => {
|
|
1512
1942
|
if (e.detail.visit.url.pathname === window.location.pathname) {
|
|
1513
1943
|
setIsPrefetching(false);
|
|
1514
1944
|
setIsPrefetched(true);
|
|
@@ -1524,32 +1954,33 @@ function usePrefetch(options = {}) {
|
|
|
1524
1954
|
lastUpdatedAt,
|
|
1525
1955
|
isPrefetching,
|
|
1526
1956
|
isPrefetched,
|
|
1527
|
-
flush: () =>
|
|
1957
|
+
flush: () => router8.flush(window.location.pathname, options)
|
|
1528
1958
|
};
|
|
1529
1959
|
}
|
|
1530
1960
|
|
|
1531
1961
|
// src/WhenVisible.ts
|
|
1532
|
-
|
|
1533
|
-
|
|
1962
|
+
import { router as router9 } from "@inertiajs/core";
|
|
1963
|
+
import { get as get3 } from "lodash-es";
|
|
1964
|
+
import { createElement as createElement6, useCallback as useCallback5, useEffect as useEffect12, useMemo as useMemo9, useRef as useRef9, useState as useState11 } from "react";
|
|
1534
1965
|
var WhenVisible = ({ children, data, params, buffer, as, always, fallback }) => {
|
|
1535
1966
|
always = always ?? false;
|
|
1536
1967
|
as = as ?? "div";
|
|
1537
1968
|
fallback = fallback ?? null;
|
|
1538
1969
|
const pageProps = usePage().props;
|
|
1539
|
-
const keys = (
|
|
1540
|
-
const [loaded, setLoaded] = (
|
|
1541
|
-
const [isFetching, setIsFetching] = (
|
|
1542
|
-
const fetching = (
|
|
1543
|
-
const ref = (
|
|
1544
|
-
const observer = (
|
|
1545
|
-
const getReloadParamsRef = (
|
|
1546
|
-
(
|
|
1970
|
+
const keys = useMemo9(() => data ? Array.isArray(data) ? data : [data] : [], [data]);
|
|
1971
|
+
const [loaded, setLoaded] = useState11(() => keys.length > 0 && keys.every((key) => get3(pageProps, key) !== void 0));
|
|
1972
|
+
const [isFetching, setIsFetching] = useState11(false);
|
|
1973
|
+
const fetching = useRef9(false);
|
|
1974
|
+
const ref = useRef9(null);
|
|
1975
|
+
const observer = useRef9(null);
|
|
1976
|
+
const getReloadParamsRef = useRef9(() => ({}));
|
|
1977
|
+
useEffect12(() => {
|
|
1547
1978
|
if (keys.length > 0) {
|
|
1548
|
-
setLoaded(keys.every((key) => pageProps
|
|
1979
|
+
setLoaded(keys.every((key) => get3(pageProps, key) !== void 0));
|
|
1549
1980
|
}
|
|
1550
1981
|
}, [pageProps, keys]);
|
|
1551
|
-
const getReloadParams = (
|
|
1552
|
-
const reloadParams = { ...params };
|
|
1982
|
+
const getReloadParams = useCallback5(() => {
|
|
1983
|
+
const reloadParams = { preserveErrors: true, ...params };
|
|
1553
1984
|
if (data) {
|
|
1554
1985
|
reloadParams.only = Array.isArray(data) ? data : [data];
|
|
1555
1986
|
}
|
|
@@ -1572,7 +2003,7 @@ var WhenVisible = ({ children, data, params, buffer, as, always, fallback }) =>
|
|
|
1572
2003
|
fetching.current = true;
|
|
1573
2004
|
setIsFetching(true);
|
|
1574
2005
|
const reloadParams = getReloadParamsRef.current();
|
|
1575
|
-
|
|
2006
|
+
router9.reload({
|
|
1576
2007
|
...reloadParams,
|
|
1577
2008
|
onStart: (e) => {
|
|
1578
2009
|
fetching.current = true;
|
|
@@ -1596,7 +2027,7 @@ var WhenVisible = ({ children, data, params, buffer, as, always, fallback }) =>
|
|
|
1596
2027
|
);
|
|
1597
2028
|
observer.current.observe(ref.current);
|
|
1598
2029
|
};
|
|
1599
|
-
(
|
|
2030
|
+
useEffect12(() => {
|
|
1600
2031
|
if (!ref.current) {
|
|
1601
2032
|
return;
|
|
1602
2033
|
}
|
|
@@ -1611,7 +2042,7 @@ var WhenVisible = ({ children, data, params, buffer, as, always, fallback }) =>
|
|
|
1611
2042
|
const resolveChildren = () => typeof children === "function" ? children({ fetching: isFetching }) : children;
|
|
1612
2043
|
const resolveFallback = () => typeof fallback === "function" ? fallback() : fallback;
|
|
1613
2044
|
if (always || !loaded) {
|
|
1614
|
-
return (
|
|
2045
|
+
return createElement6(
|
|
1615
2046
|
as,
|
|
1616
2047
|
{
|
|
1617
2048
|
props: null,
|
|
@@ -1626,7 +2057,30 @@ WhenVisible.displayName = "InertiaWhenVisible";
|
|
|
1626
2057
|
var WhenVisible_default = WhenVisible;
|
|
1627
2058
|
|
|
1628
2059
|
// src/index.ts
|
|
1629
|
-
var
|
|
1630
|
-
|
|
1631
|
-
|
|
2060
|
+
var config = coreConfig.extend();
|
|
2061
|
+
export {
|
|
2062
|
+
App,
|
|
2063
|
+
Deferred_default as Deferred,
|
|
2064
|
+
Form_default as Form,
|
|
2065
|
+
Head_default as Head,
|
|
2066
|
+
InfiniteScroll_default as InfiniteScroll,
|
|
2067
|
+
Link_default as Link,
|
|
2068
|
+
WhenVisible_default as WhenVisible,
|
|
2069
|
+
config,
|
|
2070
|
+
createInertiaApp,
|
|
2071
|
+
http2 as http,
|
|
2072
|
+
progress,
|
|
2073
|
+
resetLayoutProps,
|
|
2074
|
+
router3 as router,
|
|
2075
|
+
setLayoutProps,
|
|
2076
|
+
setLayoutPropsFor,
|
|
2077
|
+
useForm,
|
|
2078
|
+
useFormContext,
|
|
2079
|
+
useHttp,
|
|
2080
|
+
useLayoutProps,
|
|
2081
|
+
usePage,
|
|
2082
|
+
usePoll,
|
|
2083
|
+
usePrefetch,
|
|
2084
|
+
useRemember
|
|
2085
|
+
};
|
|
1632
2086
|
//# sourceMappingURL=index.js.map
|