@inertiajs/react 2.0.16 → 2.1.0
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.esm.js +467 -327
- package/dist/index.esm.js.map +4 -4
- package/dist/index.js +480 -351
- package/dist/index.js.map +4 -4
- package/package.json +2 -2
- package/types/Deferred.d.ts +4 -4
- package/types/Form.d.ts +13 -0
- package/types/Link.d.ts +2 -2
- package/types/WhenVisible.d.ts +4 -4
- package/types/index.d.ts +1 -0
package/dist/index.js
CHANGED
|
@@ -30,6 +30,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
var index_exports = {};
|
|
31
31
|
__export(index_exports, {
|
|
32
32
|
Deferred: () => Deferred_default,
|
|
33
|
+
Form: () => Form_default,
|
|
33
34
|
Head: () => Head_default,
|
|
34
35
|
Link: () => Link_default,
|
|
35
36
|
WhenVisible: () => WhenVisible_default,
|
|
@@ -42,7 +43,7 @@ __export(index_exports, {
|
|
|
42
43
|
useRemember: () => useRemember
|
|
43
44
|
});
|
|
44
45
|
module.exports = __toCommonJS(index_exports);
|
|
45
|
-
var
|
|
46
|
+
var import_core10 = require("@inertiajs/core");
|
|
46
47
|
|
|
47
48
|
// src/createInertiaApp.ts
|
|
48
49
|
var import_core2 = require("@inertiajs/core");
|
|
@@ -242,333 +243,64 @@ var Deferred = ({ children, data, fallback }) => {
|
|
|
242
243
|
(0, import_react6.useEffect)(() => {
|
|
243
244
|
setLoaded(keys.every((key) => pageProps[key] !== void 0));
|
|
244
245
|
}, [pageProps, keys]);
|
|
245
|
-
|
|
246
|
+
if (loaded) {
|
|
247
|
+
return typeof children === "function" ? children() : children;
|
|
248
|
+
}
|
|
249
|
+
return typeof fallback === "function" ? fallback() : fallback;
|
|
246
250
|
};
|
|
247
251
|
Deferred.displayName = "InertiaDeferred";
|
|
248
252
|
var Deferred_default = Deferred;
|
|
249
253
|
|
|
250
|
-
// src/
|
|
251
|
-
var import_es_toolkit = require("es-toolkit");
|
|
252
|
-
var import_react7 = __toESM(require("react"), 1);
|
|
253
|
-
var Head = function({ children, title }) {
|
|
254
|
-
const headManager = (0, import_react7.useContext)(HeadContext_default);
|
|
255
|
-
const provider = (0, import_react7.useMemo)(() => headManager.createProvider(), [headManager]);
|
|
256
|
-
const isServer = typeof window === "undefined";
|
|
257
|
-
(0, import_react7.useEffect)(() => {
|
|
258
|
-
provider.reconnect();
|
|
259
|
-
provider.update(renderNodes(children));
|
|
260
|
-
return () => {
|
|
261
|
-
provider.disconnect();
|
|
262
|
-
};
|
|
263
|
-
}, [provider, children, title]);
|
|
264
|
-
function isUnaryTag(node) {
|
|
265
|
-
return [
|
|
266
|
-
"area",
|
|
267
|
-
"base",
|
|
268
|
-
"br",
|
|
269
|
-
"col",
|
|
270
|
-
"embed",
|
|
271
|
-
"hr",
|
|
272
|
-
"img",
|
|
273
|
-
"input",
|
|
274
|
-
"keygen",
|
|
275
|
-
"link",
|
|
276
|
-
"meta",
|
|
277
|
-
"param",
|
|
278
|
-
"source",
|
|
279
|
-
"track",
|
|
280
|
-
"wbr"
|
|
281
|
-
].indexOf(node.type) > -1;
|
|
282
|
-
}
|
|
283
|
-
function renderTagStart(node) {
|
|
284
|
-
const attrs = Object.keys(node.props).reduce((carry, name) => {
|
|
285
|
-
if (["head-key", "children", "dangerouslySetInnerHTML"].includes(name)) {
|
|
286
|
-
return carry;
|
|
287
|
-
}
|
|
288
|
-
const value = node.props[name];
|
|
289
|
-
if (value === "") {
|
|
290
|
-
return carry + ` ${name}`;
|
|
291
|
-
} else {
|
|
292
|
-
return carry + ` ${name}="${(0, import_es_toolkit.escape)(value)}"`;
|
|
293
|
-
}
|
|
294
|
-
}, "");
|
|
295
|
-
return `<${node.type}${attrs}>`;
|
|
296
|
-
}
|
|
297
|
-
function renderTagChildren(node) {
|
|
298
|
-
return typeof node.props.children === "string" ? node.props.children : node.props.children.reduce((html, child) => html + renderTag(child), "");
|
|
299
|
-
}
|
|
300
|
-
function renderTag(node) {
|
|
301
|
-
let html = renderTagStart(node);
|
|
302
|
-
if (node.props.children) {
|
|
303
|
-
html += renderTagChildren(node);
|
|
304
|
-
}
|
|
305
|
-
if (node.props.dangerouslySetInnerHTML) {
|
|
306
|
-
html += node.props.dangerouslySetInnerHTML.__html;
|
|
307
|
-
}
|
|
308
|
-
if (!isUnaryTag(node)) {
|
|
309
|
-
html += `</${node.type}>`;
|
|
310
|
-
}
|
|
311
|
-
return html;
|
|
312
|
-
}
|
|
313
|
-
function ensureNodeHasInertiaProp(node) {
|
|
314
|
-
return import_react7.default.cloneElement(node, {
|
|
315
|
-
inertia: node.props["head-key"] !== void 0 ? node.props["head-key"] : ""
|
|
316
|
-
});
|
|
317
|
-
}
|
|
318
|
-
function renderNode(node) {
|
|
319
|
-
return renderTag(ensureNodeHasInertiaProp(node));
|
|
320
|
-
}
|
|
321
|
-
function renderNodes(nodes) {
|
|
322
|
-
const computed = import_react7.default.Children.toArray(nodes).filter((node) => node).map((node) => renderNode(node));
|
|
323
|
-
if (title && !computed.find((tag) => tag.startsWith("<title"))) {
|
|
324
|
-
computed.push(`<title inertia>${title}</title>`);
|
|
325
|
-
}
|
|
326
|
-
return computed;
|
|
327
|
-
}
|
|
328
|
-
if (isServer) {
|
|
329
|
-
provider.update(renderNodes(children));
|
|
330
|
-
}
|
|
331
|
-
return null;
|
|
332
|
-
};
|
|
333
|
-
var Head_default = Head;
|
|
334
|
-
|
|
335
|
-
// src/Link.ts
|
|
336
|
-
var import_core3 = require("@inertiajs/core");
|
|
337
|
-
var import_react8 = require("react");
|
|
338
|
-
var noop = () => void 0;
|
|
339
|
-
var Link = (0, import_react8.forwardRef)(
|
|
340
|
-
({
|
|
341
|
-
children,
|
|
342
|
-
as = "a",
|
|
343
|
-
data = {},
|
|
344
|
-
href,
|
|
345
|
-
method = "get",
|
|
346
|
-
preserveScroll = false,
|
|
347
|
-
preserveState = null,
|
|
348
|
-
replace = false,
|
|
349
|
-
only = [],
|
|
350
|
-
except = [],
|
|
351
|
-
headers = {},
|
|
352
|
-
queryStringArrayFormat = "brackets",
|
|
353
|
-
async = false,
|
|
354
|
-
onClick = noop,
|
|
355
|
-
onCancelToken = noop,
|
|
356
|
-
onBefore = noop,
|
|
357
|
-
onStart = noop,
|
|
358
|
-
onProgress = noop,
|
|
359
|
-
onFinish = noop,
|
|
360
|
-
onCancel = noop,
|
|
361
|
-
onSuccess = noop,
|
|
362
|
-
onError = noop,
|
|
363
|
-
prefetch = false,
|
|
364
|
-
cacheFor = 0,
|
|
365
|
-
...props
|
|
366
|
-
}, ref) => {
|
|
367
|
-
const [inFlightCount, setInFlightCount] = (0, import_react8.useState)(0);
|
|
368
|
-
const hoverTimeout = (0, import_react8.useRef)(null);
|
|
369
|
-
const _method = (0, import_react8.useMemo)(() => {
|
|
370
|
-
return typeof href === "object" ? href.method : method.toLowerCase();
|
|
371
|
-
}, [href, method]);
|
|
372
|
-
const _as = (0, import_react8.useMemo)(() => {
|
|
373
|
-
as = as.toLowerCase();
|
|
374
|
-
return _method !== "get" ? "button" : as;
|
|
375
|
-
}, [as, _method]);
|
|
376
|
-
const mergeDataArray = (0, import_react8.useMemo)(
|
|
377
|
-
() => (0, import_core3.mergeDataIntoQueryString)(
|
|
378
|
-
_method,
|
|
379
|
-
typeof href === "object" ? href.url : href || "",
|
|
380
|
-
data,
|
|
381
|
-
queryStringArrayFormat
|
|
382
|
-
),
|
|
383
|
-
[href, _method, data, queryStringArrayFormat]
|
|
384
|
-
);
|
|
385
|
-
const url = (0, import_react8.useMemo)(() => mergeDataArray[0], [mergeDataArray]);
|
|
386
|
-
const _data = (0, import_react8.useMemo)(() => mergeDataArray[1], [mergeDataArray]);
|
|
387
|
-
const baseParams = (0, import_react8.useMemo)(
|
|
388
|
-
() => ({
|
|
389
|
-
data: _data,
|
|
390
|
-
method: _method,
|
|
391
|
-
preserveScroll,
|
|
392
|
-
preserveState: preserveState ?? _method !== "get",
|
|
393
|
-
replace,
|
|
394
|
-
only,
|
|
395
|
-
except,
|
|
396
|
-
headers,
|
|
397
|
-
async
|
|
398
|
-
}),
|
|
399
|
-
[_data, _method, preserveScroll, preserveState, replace, only, except, headers, async]
|
|
400
|
-
);
|
|
401
|
-
const visitParams = (0, import_react8.useMemo)(
|
|
402
|
-
() => ({
|
|
403
|
-
...baseParams,
|
|
404
|
-
onCancelToken,
|
|
405
|
-
onBefore,
|
|
406
|
-
onStart(event) {
|
|
407
|
-
setInFlightCount((count) => count + 1);
|
|
408
|
-
onStart(event);
|
|
409
|
-
},
|
|
410
|
-
onProgress,
|
|
411
|
-
onFinish(event) {
|
|
412
|
-
setInFlightCount((count) => count - 1);
|
|
413
|
-
onFinish(event);
|
|
414
|
-
},
|
|
415
|
-
onCancel,
|
|
416
|
-
onSuccess,
|
|
417
|
-
onError
|
|
418
|
-
}),
|
|
419
|
-
[baseParams, onCancelToken, onBefore, onStart, onProgress, onFinish, onCancel, onSuccess, onError]
|
|
420
|
-
);
|
|
421
|
-
const doPrefetch = () => {
|
|
422
|
-
import_core3.router.prefetch(url, baseParams, { cacheFor: cacheForValue });
|
|
423
|
-
};
|
|
424
|
-
const prefetchModes = (0, import_react8.useMemo)(
|
|
425
|
-
() => {
|
|
426
|
-
if (prefetch === true) {
|
|
427
|
-
return ["hover"];
|
|
428
|
-
}
|
|
429
|
-
if (prefetch === false) {
|
|
430
|
-
return [];
|
|
431
|
-
}
|
|
432
|
-
if (Array.isArray(prefetch)) {
|
|
433
|
-
return prefetch;
|
|
434
|
-
}
|
|
435
|
-
return [prefetch];
|
|
436
|
-
},
|
|
437
|
-
Array.isArray(prefetch) ? prefetch : [prefetch]
|
|
438
|
-
);
|
|
439
|
-
const cacheForValue = (0, import_react8.useMemo)(() => {
|
|
440
|
-
if (cacheFor !== 0) {
|
|
441
|
-
return cacheFor;
|
|
442
|
-
}
|
|
443
|
-
if (prefetchModes.length === 1 && prefetchModes[0] === "click") {
|
|
444
|
-
return 0;
|
|
445
|
-
}
|
|
446
|
-
return 3e4;
|
|
447
|
-
}, [cacheFor, prefetchModes]);
|
|
448
|
-
(0, import_react8.useEffect)(() => {
|
|
449
|
-
return () => {
|
|
450
|
-
clearTimeout(hoverTimeout.current);
|
|
451
|
-
};
|
|
452
|
-
}, []);
|
|
453
|
-
(0, import_react8.useEffect)(() => {
|
|
454
|
-
if (prefetchModes.includes("mount")) {
|
|
455
|
-
setTimeout(() => doPrefetch());
|
|
456
|
-
}
|
|
457
|
-
}, prefetchModes);
|
|
458
|
-
const regularEvents = {
|
|
459
|
-
onClick: (event) => {
|
|
460
|
-
onClick(event);
|
|
461
|
-
if ((0, import_core3.shouldIntercept)(event)) {
|
|
462
|
-
event.preventDefault();
|
|
463
|
-
import_core3.router.visit(url, visitParams);
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
};
|
|
467
|
-
const prefetchHoverEvents = {
|
|
468
|
-
onMouseEnter: () => {
|
|
469
|
-
hoverTimeout.current = window.setTimeout(() => {
|
|
470
|
-
doPrefetch();
|
|
471
|
-
}, 75);
|
|
472
|
-
},
|
|
473
|
-
onMouseLeave: () => {
|
|
474
|
-
clearTimeout(hoverTimeout.current);
|
|
475
|
-
},
|
|
476
|
-
onClick: regularEvents.onClick
|
|
477
|
-
};
|
|
478
|
-
const prefetchClickEvents = {
|
|
479
|
-
onMouseDown: (event) => {
|
|
480
|
-
if ((0, import_core3.shouldIntercept)(event)) {
|
|
481
|
-
event.preventDefault();
|
|
482
|
-
doPrefetch();
|
|
483
|
-
}
|
|
484
|
-
},
|
|
485
|
-
onMouseUp: (event) => {
|
|
486
|
-
event.preventDefault();
|
|
487
|
-
import_core3.router.visit(url, visitParams);
|
|
488
|
-
},
|
|
489
|
-
onClick: (event) => {
|
|
490
|
-
onClick(event);
|
|
491
|
-
if ((0, import_core3.shouldIntercept)(event)) {
|
|
492
|
-
event.preventDefault();
|
|
493
|
-
}
|
|
494
|
-
}
|
|
495
|
-
};
|
|
496
|
-
const elProps = (0, import_react8.useMemo)(
|
|
497
|
-
() => ({
|
|
498
|
-
a: { href: url },
|
|
499
|
-
button: { type: "button" }
|
|
500
|
-
}),
|
|
501
|
-
[url]
|
|
502
|
-
);
|
|
503
|
-
return (0, import_react8.createElement)(
|
|
504
|
-
_as,
|
|
505
|
-
{
|
|
506
|
-
...props,
|
|
507
|
-
...elProps[_as] || {},
|
|
508
|
-
ref,
|
|
509
|
-
...(() => {
|
|
510
|
-
if (prefetchModes.includes("hover")) {
|
|
511
|
-
return prefetchHoverEvents;
|
|
512
|
-
}
|
|
513
|
-
if (prefetchModes.includes("click")) {
|
|
514
|
-
return prefetchClickEvents;
|
|
515
|
-
}
|
|
516
|
-
return regularEvents;
|
|
517
|
-
})(),
|
|
518
|
-
"data-loading": inFlightCount > 0 ? "" : void 0
|
|
519
|
-
},
|
|
520
|
-
children
|
|
521
|
-
);
|
|
522
|
-
}
|
|
523
|
-
);
|
|
524
|
-
Link.displayName = "InertiaLink";
|
|
525
|
-
var Link_default = Link;
|
|
526
|
-
|
|
527
|
-
// src/useForm.ts
|
|
254
|
+
// src/Form.ts
|
|
528
255
|
var import_core5 = require("@inertiajs/core");
|
|
529
256
|
var import_es_toolkit2 = require("es-toolkit");
|
|
257
|
+
var import_react9 = require("react");
|
|
258
|
+
|
|
259
|
+
// src/useForm.ts
|
|
260
|
+
var import_core4 = require("@inertiajs/core");
|
|
261
|
+
var import_es_toolkit = require("es-toolkit");
|
|
530
262
|
var import_compat = require("es-toolkit/compat");
|
|
531
|
-
var
|
|
263
|
+
var import_react8 = require("react");
|
|
532
264
|
|
|
533
265
|
// src/useRemember.ts
|
|
534
|
-
var
|
|
535
|
-
var
|
|
266
|
+
var import_core3 = require("@inertiajs/core");
|
|
267
|
+
var import_react7 = require("react");
|
|
536
268
|
function useRemember(initialState, key) {
|
|
537
|
-
const [state, setState] = (0,
|
|
538
|
-
const restored =
|
|
269
|
+
const [state, setState] = (0, import_react7.useState)(() => {
|
|
270
|
+
const restored = import_core3.router.restore(key);
|
|
539
271
|
return restored !== void 0 ? restored : initialState;
|
|
540
272
|
});
|
|
541
|
-
(0,
|
|
542
|
-
|
|
273
|
+
(0, import_react7.useEffect)(() => {
|
|
274
|
+
import_core3.router.remember(state, key);
|
|
543
275
|
}, [state, key]);
|
|
544
276
|
return [state, setState];
|
|
545
277
|
}
|
|
546
278
|
|
|
547
279
|
// src/useForm.ts
|
|
548
280
|
function useForm(rememberKeyOrInitialValues, maybeInitialValues) {
|
|
549
|
-
const isMounted = (0,
|
|
281
|
+
const isMounted = (0, import_react8.useRef)(null);
|
|
550
282
|
const rememberKey = typeof rememberKeyOrInitialValues === "string" ? rememberKeyOrInitialValues : null;
|
|
551
|
-
const [defaults, setDefaults] = (0,
|
|
283
|
+
const [defaults, setDefaults] = (0, import_react8.useState)(
|
|
552
284
|
(typeof rememberKeyOrInitialValues === "string" ? maybeInitialValues : rememberKeyOrInitialValues) || {}
|
|
553
285
|
);
|
|
554
|
-
const cancelToken = (0,
|
|
555
|
-
const recentlySuccessfulTimeoutId = (0,
|
|
556
|
-
const [data, setData] = rememberKey ? useRemember(defaults, `${rememberKey}:data`) : (0,
|
|
557
|
-
const [errors, setErrors] = rememberKey ? useRemember({}, `${rememberKey}:errors`) : (0,
|
|
558
|
-
const [hasErrors, setHasErrors] = (0,
|
|
559
|
-
const [processing, setProcessing] = (0,
|
|
560
|
-
const [progress, setProgress] = (0,
|
|
561
|
-
const [wasSuccessful, setWasSuccessful] = (0,
|
|
562
|
-
const [recentlySuccessful, setRecentlySuccessful] = (0,
|
|
563
|
-
const transform = (0,
|
|
564
|
-
const isDirty = (0,
|
|
565
|
-
(0,
|
|
286
|
+
const cancelToken = (0, import_react8.useRef)(null);
|
|
287
|
+
const recentlySuccessfulTimeoutId = (0, import_react8.useRef)(null);
|
|
288
|
+
const [data, setData] = rememberKey ? useRemember(defaults, `${rememberKey}:data`) : (0, import_react8.useState)(defaults);
|
|
289
|
+
const [errors, setErrors] = rememberKey ? useRemember({}, `${rememberKey}:errors`) : (0, import_react8.useState)({});
|
|
290
|
+
const [hasErrors, setHasErrors] = (0, import_react8.useState)(false);
|
|
291
|
+
const [processing, setProcessing] = (0, import_react8.useState)(false);
|
|
292
|
+
const [progress, setProgress] = (0, import_react8.useState)(null);
|
|
293
|
+
const [wasSuccessful, setWasSuccessful] = (0, import_react8.useState)(false);
|
|
294
|
+
const [recentlySuccessful, setRecentlySuccessful] = (0, import_react8.useState)(false);
|
|
295
|
+
const transform = (0, import_react8.useRef)((data2) => data2);
|
|
296
|
+
const isDirty = (0, import_react8.useMemo)(() => !(0, import_es_toolkit.isEqual)(data, defaults), [data, defaults]);
|
|
297
|
+
(0, import_react8.useEffect)(() => {
|
|
566
298
|
isMounted.current = true;
|
|
567
299
|
return () => {
|
|
568
300
|
isMounted.current = false;
|
|
569
301
|
};
|
|
570
302
|
}, []);
|
|
571
|
-
const submit = (0,
|
|
303
|
+
const submit = (0, import_react8.useCallback)(
|
|
572
304
|
(...args) => {
|
|
573
305
|
const objectPassed = typeof args[0] === "object";
|
|
574
306
|
const method = objectPassed ? args[0].method : args[0];
|
|
@@ -610,7 +342,7 @@ function useForm(rememberKeyOrInitialValues, maybeInitialValues) {
|
|
|
610
342
|
setHasErrors(false);
|
|
611
343
|
setWasSuccessful(true);
|
|
612
344
|
setRecentlySuccessful(true);
|
|
613
|
-
setDefaults((0,
|
|
345
|
+
setDefaults((0, import_es_toolkit.cloneDeep)(data));
|
|
614
346
|
recentlySuccessfulTimeoutId.current = setTimeout(() => {
|
|
615
347
|
if (isMounted.current) {
|
|
616
348
|
setRecentlySuccessful(false);
|
|
@@ -653,17 +385,17 @@ function useForm(rememberKeyOrInitialValues, maybeInitialValues) {
|
|
|
653
385
|
}
|
|
654
386
|
};
|
|
655
387
|
if (method === "delete") {
|
|
656
|
-
|
|
388
|
+
import_core4.router.delete(url, { ..._options, data: transform.current(data) });
|
|
657
389
|
} else {
|
|
658
|
-
|
|
390
|
+
import_core4.router[method](url, transform.current(data), _options);
|
|
659
391
|
}
|
|
660
392
|
},
|
|
661
393
|
[data, setErrors, transform]
|
|
662
394
|
);
|
|
663
|
-
const setDataFunction = (0,
|
|
395
|
+
const setDataFunction = (0, import_react8.useCallback)(
|
|
664
396
|
(keyOrData, maybeValue) => {
|
|
665
397
|
if (typeof keyOrData === "string") {
|
|
666
|
-
setData((data2) => (0, import_compat.set)((0,
|
|
398
|
+
setData((data2) => (0, import_compat.set)((0, import_es_toolkit.cloneDeep)(data2), keyOrData, maybeValue));
|
|
667
399
|
} else if (typeof keyOrData === "function") {
|
|
668
400
|
setData((data2) => keyOrData(data2));
|
|
669
401
|
} else {
|
|
@@ -672,21 +404,21 @@ function useForm(rememberKeyOrInitialValues, maybeInitialValues) {
|
|
|
672
404
|
},
|
|
673
405
|
[setData]
|
|
674
406
|
);
|
|
675
|
-
const [dataAsDefaults, setDataAsDefaults] = (0,
|
|
676
|
-
const setDefaultsFunction = (0,
|
|
407
|
+
const [dataAsDefaults, setDataAsDefaults] = (0, import_react8.useState)(false);
|
|
408
|
+
const setDefaultsFunction = (0, import_react8.useCallback)(
|
|
677
409
|
(fieldOrFields, maybeValue) => {
|
|
678
410
|
if (typeof fieldOrFields === "undefined") {
|
|
679
411
|
setDefaults(data);
|
|
680
412
|
setDataAsDefaults(true);
|
|
681
413
|
} else {
|
|
682
414
|
setDefaults((defaults2) => {
|
|
683
|
-
return typeof fieldOrFields === "string" ? (0, import_compat.set)((0,
|
|
415
|
+
return typeof fieldOrFields === "string" ? (0, import_compat.set)((0, import_es_toolkit.cloneDeep)(defaults2), fieldOrFields, maybeValue) : Object.assign((0, import_es_toolkit.cloneDeep)(defaults2), fieldOrFields);
|
|
684
416
|
});
|
|
685
417
|
}
|
|
686
418
|
},
|
|
687
419
|
[data, setDefaults]
|
|
688
420
|
);
|
|
689
|
-
(0,
|
|
421
|
+
(0, import_react8.useLayoutEffect)(() => {
|
|
690
422
|
if (!dataAsDefaults) {
|
|
691
423
|
return;
|
|
692
424
|
}
|
|
@@ -695,7 +427,7 @@ function useForm(rememberKeyOrInitialValues, maybeInitialValues) {
|
|
|
695
427
|
}
|
|
696
428
|
setDataAsDefaults(false);
|
|
697
429
|
}, [dataAsDefaults]);
|
|
698
|
-
const reset = (0,
|
|
430
|
+
const reset = (0, import_react8.useCallback)(
|
|
699
431
|
(...fields) => {
|
|
700
432
|
if (fields.length === 0) {
|
|
701
433
|
setData(defaults);
|
|
@@ -712,7 +444,7 @@ function useForm(rememberKeyOrInitialValues, maybeInitialValues) {
|
|
|
712
444
|
},
|
|
713
445
|
[setData, defaults]
|
|
714
446
|
);
|
|
715
|
-
const setError = (0,
|
|
447
|
+
const setError = (0, import_react8.useCallback)(
|
|
716
448
|
(fieldOrFields, maybeValue) => {
|
|
717
449
|
setErrors((errors2) => {
|
|
718
450
|
const newErrors = {
|
|
@@ -725,7 +457,7 @@ function useForm(rememberKeyOrInitialValues, maybeInitialValues) {
|
|
|
725
457
|
},
|
|
726
458
|
[setErrors, setHasErrors]
|
|
727
459
|
);
|
|
728
|
-
const clearErrors = (0,
|
|
460
|
+
const clearErrors = (0, import_react8.useCallback)(
|
|
729
461
|
(...fields) => {
|
|
730
462
|
setErrors((errors2) => {
|
|
731
463
|
const newErrors = Object.keys(errors2).reduce(
|
|
@@ -741,7 +473,7 @@ function useForm(rememberKeyOrInitialValues, maybeInitialValues) {
|
|
|
741
473
|
},
|
|
742
474
|
[setErrors, setHasErrors]
|
|
743
475
|
);
|
|
744
|
-
const resetAndClearErrors = (0,
|
|
476
|
+
const resetAndClearErrors = (0, import_react8.useCallback)(
|
|
745
477
|
(...fields) => {
|
|
746
478
|
reset(...fields);
|
|
747
479
|
clearErrors(...fields);
|
|
@@ -751,17 +483,17 @@ function useForm(rememberKeyOrInitialValues, maybeInitialValues) {
|
|
|
751
483
|
const createSubmitMethod = (method) => (url, options) => {
|
|
752
484
|
submit(method, url, options);
|
|
753
485
|
};
|
|
754
|
-
const getMethod = (0,
|
|
755
|
-
const post = (0,
|
|
756
|
-
const put = (0,
|
|
757
|
-
const patch = (0,
|
|
758
|
-
const deleteMethod = (0,
|
|
759
|
-
const cancel = (0,
|
|
486
|
+
const getMethod = (0, import_react8.useCallback)(createSubmitMethod("get"), [submit]);
|
|
487
|
+
const post = (0, import_react8.useCallback)(createSubmitMethod("post"), [submit]);
|
|
488
|
+
const put = (0, import_react8.useCallback)(createSubmitMethod("put"), [submit]);
|
|
489
|
+
const patch = (0, import_react8.useCallback)(createSubmitMethod("patch"), [submit]);
|
|
490
|
+
const deleteMethod = (0, import_react8.useCallback)(createSubmitMethod("delete"), [submit]);
|
|
491
|
+
const cancel = (0, import_react8.useCallback)(() => {
|
|
760
492
|
if (cancelToken.current) {
|
|
761
493
|
cancelToken.current.cancel();
|
|
762
494
|
}
|
|
763
495
|
}, []);
|
|
764
|
-
const transformFunction = (0,
|
|
496
|
+
const transformFunction = (0, import_react8.useCallback)((callback) => {
|
|
765
497
|
transform.current = callback;
|
|
766
498
|
}, []);
|
|
767
499
|
return {
|
|
@@ -790,20 +522,415 @@ function useForm(rememberKeyOrInitialValues, maybeInitialValues) {
|
|
|
790
522
|
};
|
|
791
523
|
}
|
|
792
524
|
|
|
793
|
-
// src/
|
|
794
|
-
var
|
|
525
|
+
// src/Form.ts
|
|
526
|
+
var noop = () => void 0;
|
|
527
|
+
var Form = (0, import_react9.forwardRef)(
|
|
528
|
+
({
|
|
529
|
+
action = "",
|
|
530
|
+
method = "get",
|
|
531
|
+
headers = {},
|
|
532
|
+
queryStringArrayFormat = "brackets",
|
|
533
|
+
errorBag = null,
|
|
534
|
+
showProgress = true,
|
|
535
|
+
transform = (data) => data,
|
|
536
|
+
options = {},
|
|
537
|
+
onStart = noop,
|
|
538
|
+
onProgress = noop,
|
|
539
|
+
onFinish = noop,
|
|
540
|
+
onBefore = noop,
|
|
541
|
+
onCancel = noop,
|
|
542
|
+
onSuccess = noop,
|
|
543
|
+
onError = noop,
|
|
544
|
+
onCancelToken = noop,
|
|
545
|
+
children,
|
|
546
|
+
...props
|
|
547
|
+
}, ref) => {
|
|
548
|
+
const form = useForm({});
|
|
549
|
+
const formElement = (0, import_react9.useRef)(null);
|
|
550
|
+
const resolvedMethod = (0, import_react9.useMemo)(() => {
|
|
551
|
+
return typeof action === "object" ? action.method : method.toLowerCase();
|
|
552
|
+
}, [action, method]);
|
|
553
|
+
const [isDirty, setIsDirty] = (0, import_react9.useState)(false);
|
|
554
|
+
const defaultValues = (0, import_react9.useRef)({});
|
|
555
|
+
const getData = () => {
|
|
556
|
+
return (0, import_core5.formDataToObject)(new FormData(formElement.current));
|
|
557
|
+
};
|
|
558
|
+
const updateDirtyState = (event) => setIsDirty(event.type === "reset" ? false : !(0, import_es_toolkit2.isEqual)(getData(), defaultValues.current));
|
|
559
|
+
(0, import_react9.useEffect)(() => {
|
|
560
|
+
defaultValues.current = getData();
|
|
561
|
+
const formEvents = ["input", "change", "reset"];
|
|
562
|
+
formEvents.forEach((e) => formElement.current.addEventListener(e, updateDirtyState));
|
|
563
|
+
return () => formEvents.forEach((e) => formElement.current?.removeEventListener(e, updateDirtyState));
|
|
564
|
+
}, []);
|
|
565
|
+
const submit = () => {
|
|
566
|
+
const [url, _data] = (0, import_core5.mergeDataIntoQueryString)(
|
|
567
|
+
resolvedMethod,
|
|
568
|
+
typeof action === "object" ? action.url : action,
|
|
569
|
+
getData(),
|
|
570
|
+
queryStringArrayFormat
|
|
571
|
+
);
|
|
572
|
+
const submitOptions = {
|
|
573
|
+
headers,
|
|
574
|
+
errorBag,
|
|
575
|
+
showProgress,
|
|
576
|
+
onCancelToken,
|
|
577
|
+
onBefore,
|
|
578
|
+
onStart,
|
|
579
|
+
onProgress,
|
|
580
|
+
onFinish,
|
|
581
|
+
onCancel,
|
|
582
|
+
onSuccess,
|
|
583
|
+
onError,
|
|
584
|
+
...options
|
|
585
|
+
};
|
|
586
|
+
form.transform(() => transform(_data));
|
|
587
|
+
form.submit(resolvedMethod, url, submitOptions);
|
|
588
|
+
};
|
|
589
|
+
(0, import_react9.useImperativeHandle)(
|
|
590
|
+
ref,
|
|
591
|
+
() => ({
|
|
592
|
+
errors: form.errors,
|
|
593
|
+
hasErrors: form.hasErrors,
|
|
594
|
+
processing: form.processing,
|
|
595
|
+
progress: form.progress,
|
|
596
|
+
wasSuccessful: form.wasSuccessful,
|
|
597
|
+
recentlySuccessful: form.recentlySuccessful,
|
|
598
|
+
clearErrors: form.clearErrors,
|
|
599
|
+
resetAndClearErrors: form.resetAndClearErrors,
|
|
600
|
+
setError: form.setError,
|
|
601
|
+
isDirty,
|
|
602
|
+
reset: () => formElement.current?.reset(),
|
|
603
|
+
submit
|
|
604
|
+
}),
|
|
605
|
+
[form, isDirty, submit]
|
|
606
|
+
);
|
|
607
|
+
return (0, import_react9.createElement)(
|
|
608
|
+
"form",
|
|
609
|
+
{
|
|
610
|
+
...props,
|
|
611
|
+
ref: formElement,
|
|
612
|
+
action: typeof action === "object" ? action.url : action,
|
|
613
|
+
method: resolvedMethod,
|
|
614
|
+
onSubmit: (event) => {
|
|
615
|
+
event.preventDefault();
|
|
616
|
+
submit();
|
|
617
|
+
}
|
|
618
|
+
},
|
|
619
|
+
typeof children === "function" ? children({
|
|
620
|
+
errors: form.errors,
|
|
621
|
+
hasErrors: form.hasErrors,
|
|
622
|
+
processing: form.processing,
|
|
623
|
+
progress: form.progress,
|
|
624
|
+
wasSuccessful: form.wasSuccessful,
|
|
625
|
+
recentlySuccessful: form.recentlySuccessful,
|
|
626
|
+
setError: form.setError,
|
|
627
|
+
clearErrors: form.clearErrors,
|
|
628
|
+
resetAndClearErrors: form.resetAndClearErrors,
|
|
629
|
+
isDirty,
|
|
630
|
+
reset: () => formElement.current?.reset(),
|
|
631
|
+
submit
|
|
632
|
+
}) : children
|
|
633
|
+
);
|
|
634
|
+
}
|
|
635
|
+
);
|
|
636
|
+
Form.displayName = "InertiaForm";
|
|
637
|
+
var Form_default = Form;
|
|
638
|
+
|
|
639
|
+
// src/Head.ts
|
|
640
|
+
var import_es_toolkit3 = require("es-toolkit");
|
|
641
|
+
var import_react10 = __toESM(require("react"), 1);
|
|
642
|
+
var Head = function({ children, title }) {
|
|
643
|
+
const headManager = (0, import_react10.useContext)(HeadContext_default);
|
|
644
|
+
const provider = (0, import_react10.useMemo)(() => headManager.createProvider(), [headManager]);
|
|
645
|
+
const isServer = typeof window === "undefined";
|
|
646
|
+
(0, import_react10.useEffect)(() => {
|
|
647
|
+
provider.reconnect();
|
|
648
|
+
provider.update(renderNodes(children));
|
|
649
|
+
return () => {
|
|
650
|
+
provider.disconnect();
|
|
651
|
+
};
|
|
652
|
+
}, [provider, children, title]);
|
|
653
|
+
function isUnaryTag(node) {
|
|
654
|
+
return [
|
|
655
|
+
"area",
|
|
656
|
+
"base",
|
|
657
|
+
"br",
|
|
658
|
+
"col",
|
|
659
|
+
"embed",
|
|
660
|
+
"hr",
|
|
661
|
+
"img",
|
|
662
|
+
"input",
|
|
663
|
+
"keygen",
|
|
664
|
+
"link",
|
|
665
|
+
"meta",
|
|
666
|
+
"param",
|
|
667
|
+
"source",
|
|
668
|
+
"track",
|
|
669
|
+
"wbr"
|
|
670
|
+
].indexOf(node.type) > -1;
|
|
671
|
+
}
|
|
672
|
+
function renderTagStart(node) {
|
|
673
|
+
const attrs = Object.keys(node.props).reduce((carry, name) => {
|
|
674
|
+
if (["head-key", "children", "dangerouslySetInnerHTML"].includes(name)) {
|
|
675
|
+
return carry;
|
|
676
|
+
}
|
|
677
|
+
const value = String(node.props[name]);
|
|
678
|
+
if (value === "") {
|
|
679
|
+
return carry + ` ${name}`;
|
|
680
|
+
} else {
|
|
681
|
+
return carry + ` ${name}="${(0, import_es_toolkit3.escape)(value)}"`;
|
|
682
|
+
}
|
|
683
|
+
}, "");
|
|
684
|
+
return `<${node.type}${attrs}>`;
|
|
685
|
+
}
|
|
686
|
+
function renderTagChildren(node) {
|
|
687
|
+
return typeof node.props.children === "string" ? node.props.children : node.props.children.reduce((html, child) => html + renderTag(child), "");
|
|
688
|
+
}
|
|
689
|
+
function renderTag(node) {
|
|
690
|
+
let html = renderTagStart(node);
|
|
691
|
+
if (node.props.children) {
|
|
692
|
+
html += renderTagChildren(node);
|
|
693
|
+
}
|
|
694
|
+
if (node.props.dangerouslySetInnerHTML) {
|
|
695
|
+
html += node.props.dangerouslySetInnerHTML.__html;
|
|
696
|
+
}
|
|
697
|
+
if (!isUnaryTag(node)) {
|
|
698
|
+
html += `</${node.type}>`;
|
|
699
|
+
}
|
|
700
|
+
return html;
|
|
701
|
+
}
|
|
702
|
+
function ensureNodeHasInertiaProp(node) {
|
|
703
|
+
return import_react10.default.cloneElement(node, {
|
|
704
|
+
inertia: node.props["head-key"] !== void 0 ? node.props["head-key"] : ""
|
|
705
|
+
});
|
|
706
|
+
}
|
|
707
|
+
function renderNode(node) {
|
|
708
|
+
return renderTag(ensureNodeHasInertiaProp(node));
|
|
709
|
+
}
|
|
710
|
+
function renderNodes(nodes) {
|
|
711
|
+
const computed = import_react10.default.Children.toArray(nodes).filter((node) => node).map((node) => renderNode(node));
|
|
712
|
+
if (title && !computed.find((tag) => tag.startsWith("<title"))) {
|
|
713
|
+
computed.push(`<title inertia>${title}</title>`);
|
|
714
|
+
}
|
|
715
|
+
return computed;
|
|
716
|
+
}
|
|
717
|
+
if (isServer) {
|
|
718
|
+
provider.update(renderNodes(children));
|
|
719
|
+
}
|
|
720
|
+
return null;
|
|
721
|
+
};
|
|
722
|
+
var Head_default = Head;
|
|
723
|
+
|
|
724
|
+
// src/Link.ts
|
|
725
|
+
var import_core6 = require("@inertiajs/core");
|
|
795
726
|
var import_react11 = require("react");
|
|
727
|
+
var noop2 = () => void 0;
|
|
728
|
+
var Link = (0, import_react11.forwardRef)(
|
|
729
|
+
({
|
|
730
|
+
children,
|
|
731
|
+
as = "a",
|
|
732
|
+
data = {},
|
|
733
|
+
href,
|
|
734
|
+
method = "get",
|
|
735
|
+
preserveScroll = false,
|
|
736
|
+
preserveState = null,
|
|
737
|
+
replace = false,
|
|
738
|
+
only = [],
|
|
739
|
+
except = [],
|
|
740
|
+
headers = {},
|
|
741
|
+
queryStringArrayFormat = "brackets",
|
|
742
|
+
async = false,
|
|
743
|
+
onClick = noop2,
|
|
744
|
+
onCancelToken = noop2,
|
|
745
|
+
onBefore = noop2,
|
|
746
|
+
onStart = noop2,
|
|
747
|
+
onProgress = noop2,
|
|
748
|
+
onFinish = noop2,
|
|
749
|
+
onCancel = noop2,
|
|
750
|
+
onSuccess = noop2,
|
|
751
|
+
onError = noop2,
|
|
752
|
+
prefetch = false,
|
|
753
|
+
cacheFor = 0,
|
|
754
|
+
...props
|
|
755
|
+
}, ref) => {
|
|
756
|
+
const [inFlightCount, setInFlightCount] = (0, import_react11.useState)(0);
|
|
757
|
+
const hoverTimeout = (0, import_react11.useRef)(null);
|
|
758
|
+
const _method = (0, import_react11.useMemo)(() => {
|
|
759
|
+
return typeof href === "object" ? href.method : method.toLowerCase();
|
|
760
|
+
}, [href, method]);
|
|
761
|
+
const _as = (0, import_react11.useMemo)(() => {
|
|
762
|
+
if (typeof as !== "string") {
|
|
763
|
+
return as;
|
|
764
|
+
}
|
|
765
|
+
return _method !== "get" ? "button" : as.toLowerCase();
|
|
766
|
+
}, [as, _method]);
|
|
767
|
+
const mergeDataArray = (0, import_react11.useMemo)(
|
|
768
|
+
() => (0, import_core6.mergeDataIntoQueryString)(
|
|
769
|
+
_method,
|
|
770
|
+
typeof href === "object" ? href.url : href || "",
|
|
771
|
+
data,
|
|
772
|
+
queryStringArrayFormat
|
|
773
|
+
),
|
|
774
|
+
[href, _method, data, queryStringArrayFormat]
|
|
775
|
+
);
|
|
776
|
+
const url = (0, import_react11.useMemo)(() => mergeDataArray[0], [mergeDataArray]);
|
|
777
|
+
const _data = (0, import_react11.useMemo)(() => mergeDataArray[1], [mergeDataArray]);
|
|
778
|
+
const baseParams = (0, import_react11.useMemo)(
|
|
779
|
+
() => ({
|
|
780
|
+
data: _data,
|
|
781
|
+
method: _method,
|
|
782
|
+
preserveScroll,
|
|
783
|
+
preserveState: preserveState ?? _method !== "get",
|
|
784
|
+
replace,
|
|
785
|
+
only,
|
|
786
|
+
except,
|
|
787
|
+
headers,
|
|
788
|
+
async
|
|
789
|
+
}),
|
|
790
|
+
[_data, _method, preserveScroll, preserveState, replace, only, except, headers, async]
|
|
791
|
+
);
|
|
792
|
+
const visitParams = (0, import_react11.useMemo)(
|
|
793
|
+
() => ({
|
|
794
|
+
...baseParams,
|
|
795
|
+
onCancelToken,
|
|
796
|
+
onBefore,
|
|
797
|
+
onStart(event) {
|
|
798
|
+
setInFlightCount((count) => count + 1);
|
|
799
|
+
onStart(event);
|
|
800
|
+
},
|
|
801
|
+
onProgress,
|
|
802
|
+
onFinish(event) {
|
|
803
|
+
setInFlightCount((count) => count - 1);
|
|
804
|
+
onFinish(event);
|
|
805
|
+
},
|
|
806
|
+
onCancel,
|
|
807
|
+
onSuccess,
|
|
808
|
+
onError
|
|
809
|
+
}),
|
|
810
|
+
[baseParams, onCancelToken, onBefore, onStart, onProgress, onFinish, onCancel, onSuccess, onError]
|
|
811
|
+
);
|
|
812
|
+
const doPrefetch = () => {
|
|
813
|
+
import_core6.router.prefetch(url, baseParams, { cacheFor: cacheForValue });
|
|
814
|
+
};
|
|
815
|
+
const prefetchModes = (0, import_react11.useMemo)(
|
|
816
|
+
() => {
|
|
817
|
+
if (prefetch === true) {
|
|
818
|
+
return ["hover"];
|
|
819
|
+
}
|
|
820
|
+
if (prefetch === false) {
|
|
821
|
+
return [];
|
|
822
|
+
}
|
|
823
|
+
if (Array.isArray(prefetch)) {
|
|
824
|
+
return prefetch;
|
|
825
|
+
}
|
|
826
|
+
return [prefetch];
|
|
827
|
+
},
|
|
828
|
+
Array.isArray(prefetch) ? prefetch : [prefetch]
|
|
829
|
+
);
|
|
830
|
+
const cacheForValue = (0, import_react11.useMemo)(() => {
|
|
831
|
+
if (cacheFor !== 0) {
|
|
832
|
+
return cacheFor;
|
|
833
|
+
}
|
|
834
|
+
if (prefetchModes.length === 1 && prefetchModes[0] === "click") {
|
|
835
|
+
return 0;
|
|
836
|
+
}
|
|
837
|
+
return 3e4;
|
|
838
|
+
}, [cacheFor, prefetchModes]);
|
|
839
|
+
(0, import_react11.useEffect)(() => {
|
|
840
|
+
return () => {
|
|
841
|
+
clearTimeout(hoverTimeout.current);
|
|
842
|
+
};
|
|
843
|
+
}, []);
|
|
844
|
+
(0, import_react11.useEffect)(() => {
|
|
845
|
+
if (prefetchModes.includes("mount")) {
|
|
846
|
+
setTimeout(() => doPrefetch());
|
|
847
|
+
}
|
|
848
|
+
}, prefetchModes);
|
|
849
|
+
const regularEvents = {
|
|
850
|
+
onClick: (event) => {
|
|
851
|
+
onClick(event);
|
|
852
|
+
if ((0, import_core6.shouldIntercept)(event)) {
|
|
853
|
+
event.preventDefault();
|
|
854
|
+
import_core6.router.visit(url, visitParams);
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
};
|
|
858
|
+
const prefetchHoverEvents = {
|
|
859
|
+
onMouseEnter: () => {
|
|
860
|
+
hoverTimeout.current = window.setTimeout(() => {
|
|
861
|
+
doPrefetch();
|
|
862
|
+
}, 75);
|
|
863
|
+
},
|
|
864
|
+
onMouseLeave: () => {
|
|
865
|
+
clearTimeout(hoverTimeout.current);
|
|
866
|
+
},
|
|
867
|
+
onClick: regularEvents.onClick
|
|
868
|
+
};
|
|
869
|
+
const prefetchClickEvents = {
|
|
870
|
+
onMouseDown: (event) => {
|
|
871
|
+
if ((0, import_core6.shouldIntercept)(event)) {
|
|
872
|
+
event.preventDefault();
|
|
873
|
+
doPrefetch();
|
|
874
|
+
}
|
|
875
|
+
},
|
|
876
|
+
onMouseUp: (event) => {
|
|
877
|
+
event.preventDefault();
|
|
878
|
+
import_core6.router.visit(url, visitParams);
|
|
879
|
+
},
|
|
880
|
+
onClick: (event) => {
|
|
881
|
+
onClick(event);
|
|
882
|
+
if ((0, import_core6.shouldIntercept)(event)) {
|
|
883
|
+
event.preventDefault();
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
};
|
|
887
|
+
const elProps = (0, import_react11.useMemo)(() => {
|
|
888
|
+
if (_as === "button") {
|
|
889
|
+
return { type: "button" };
|
|
890
|
+
}
|
|
891
|
+
if (_as === "a" || typeof _as !== "string") {
|
|
892
|
+
return { href: url };
|
|
893
|
+
}
|
|
894
|
+
return {};
|
|
895
|
+
}, [_as, url]);
|
|
896
|
+
return (0, import_react11.createElement)(
|
|
897
|
+
_as,
|
|
898
|
+
{
|
|
899
|
+
...props,
|
|
900
|
+
...elProps,
|
|
901
|
+
ref,
|
|
902
|
+
...(() => {
|
|
903
|
+
if (prefetchModes.includes("hover")) {
|
|
904
|
+
return prefetchHoverEvents;
|
|
905
|
+
}
|
|
906
|
+
if (prefetchModes.includes("click")) {
|
|
907
|
+
return prefetchClickEvents;
|
|
908
|
+
}
|
|
909
|
+
return regularEvents;
|
|
910
|
+
})(),
|
|
911
|
+
"data-loading": inFlightCount > 0 ? "" : void 0
|
|
912
|
+
},
|
|
913
|
+
children
|
|
914
|
+
);
|
|
915
|
+
}
|
|
916
|
+
);
|
|
917
|
+
Link.displayName = "InertiaLink";
|
|
918
|
+
var Link_default = Link;
|
|
919
|
+
|
|
920
|
+
// src/usePoll.ts
|
|
921
|
+
var import_core7 = require("@inertiajs/core");
|
|
922
|
+
var import_react12 = require("react");
|
|
796
923
|
function usePoll(interval, requestOptions = {}, options = {
|
|
797
924
|
keepAlive: false,
|
|
798
925
|
autoStart: true
|
|
799
926
|
}) {
|
|
800
|
-
const pollRef = (0,
|
|
801
|
-
|
|
927
|
+
const pollRef = (0, import_react12.useRef)(
|
|
928
|
+
import_core7.router.poll(interval, requestOptions, {
|
|
802
929
|
...options,
|
|
803
930
|
autoStart: false
|
|
804
931
|
})
|
|
805
932
|
);
|
|
806
|
-
(0,
|
|
933
|
+
(0, import_react12.useEffect)(() => {
|
|
807
934
|
if (options.autoStart ?? true) {
|
|
808
935
|
pollRef.current.start();
|
|
809
936
|
}
|
|
@@ -816,21 +943,21 @@ function usePoll(interval, requestOptions = {}, options = {
|
|
|
816
943
|
}
|
|
817
944
|
|
|
818
945
|
// src/usePrefetch.ts
|
|
819
|
-
var
|
|
820
|
-
var
|
|
946
|
+
var import_core8 = require("@inertiajs/core");
|
|
947
|
+
var import_react13 = require("react");
|
|
821
948
|
function usePrefetch(options = {}) {
|
|
822
|
-
const cached = typeof window === "undefined" ? null :
|
|
823
|
-
const inFlight = typeof window === "undefined" ? null :
|
|
824
|
-
const [lastUpdatedAt, setLastUpdatedAt] = (0,
|
|
825
|
-
const [isPrefetching, setIsPrefetching] = (0,
|
|
826
|
-
const [isPrefetched, setIsPrefetched] = (0,
|
|
827
|
-
(0,
|
|
828
|
-
const onPrefetchingListener =
|
|
949
|
+
const cached = typeof window === "undefined" ? null : import_core8.router.getCached(window.location.pathname, options);
|
|
950
|
+
const inFlight = typeof window === "undefined" ? null : import_core8.router.getPrefetching(window.location.pathname, options);
|
|
951
|
+
const [lastUpdatedAt, setLastUpdatedAt] = (0, import_react13.useState)(cached?.staleTimestamp || null);
|
|
952
|
+
const [isPrefetching, setIsPrefetching] = (0, import_react13.useState)(inFlight !== null);
|
|
953
|
+
const [isPrefetched, setIsPrefetched] = (0, import_react13.useState)(cached !== null);
|
|
954
|
+
(0, import_react13.useEffect)(() => {
|
|
955
|
+
const onPrefetchingListener = import_core8.router.on("prefetching", (e) => {
|
|
829
956
|
if (e.detail.visit.url.pathname === window.location.pathname) {
|
|
830
957
|
setIsPrefetching(true);
|
|
831
958
|
}
|
|
832
959
|
});
|
|
833
|
-
const onPrefetchedListener =
|
|
960
|
+
const onPrefetchedListener = import_core8.router.on("prefetched", (e) => {
|
|
834
961
|
if (e.detail.visit.url.pathname === window.location.pathname) {
|
|
835
962
|
setIsPrefetching(false);
|
|
836
963
|
setIsPrefetched(true);
|
|
@@ -846,22 +973,22 @@ function usePrefetch(options = {}) {
|
|
|
846
973
|
lastUpdatedAt,
|
|
847
974
|
isPrefetching,
|
|
848
975
|
isPrefetched,
|
|
849
|
-
flush: () =>
|
|
976
|
+
flush: () => import_core8.router.flush(window.location.pathname, options)
|
|
850
977
|
};
|
|
851
978
|
}
|
|
852
979
|
|
|
853
980
|
// src/WhenVisible.ts
|
|
854
|
-
var
|
|
855
|
-
var
|
|
981
|
+
var import_core9 = require("@inertiajs/core");
|
|
982
|
+
var import_react14 = require("react");
|
|
856
983
|
var WhenVisible = ({ children, data, params, buffer, as, always, fallback }) => {
|
|
857
984
|
always = always ?? false;
|
|
858
985
|
as = as ?? "div";
|
|
859
986
|
fallback = fallback ?? null;
|
|
860
|
-
const [loaded, setLoaded] = (0,
|
|
861
|
-
const hasFetched = (0,
|
|
862
|
-
const fetching = (0,
|
|
863
|
-
const ref = (0,
|
|
864
|
-
const getReloadParams = (0,
|
|
987
|
+
const [loaded, setLoaded] = (0, import_react14.useState)(false);
|
|
988
|
+
const hasFetched = (0, import_react14.useRef)(false);
|
|
989
|
+
const fetching = (0, import_react14.useRef)(false);
|
|
990
|
+
const ref = (0, import_react14.useRef)(null);
|
|
991
|
+
const getReloadParams = (0, import_react14.useCallback)(() => {
|
|
865
992
|
if (data) {
|
|
866
993
|
return {
|
|
867
994
|
only: Array.isArray(data) ? data : [data]
|
|
@@ -872,7 +999,7 @@ var WhenVisible = ({ children, data, params, buffer, as, always, fallback }) =>
|
|
|
872
999
|
}
|
|
873
1000
|
return params;
|
|
874
1001
|
}, [params, data]);
|
|
875
|
-
(0,
|
|
1002
|
+
(0, import_react14.useEffect)(() => {
|
|
876
1003
|
if (!ref.current) {
|
|
877
1004
|
return;
|
|
878
1005
|
}
|
|
@@ -890,7 +1017,7 @@ var WhenVisible = ({ children, data, params, buffer, as, always, fallback }) =>
|
|
|
890
1017
|
hasFetched.current = true;
|
|
891
1018
|
fetching.current = true;
|
|
892
1019
|
const reloadParams = getReloadParams();
|
|
893
|
-
|
|
1020
|
+
import_core9.router.reload({
|
|
894
1021
|
...reloadParams,
|
|
895
1022
|
onStart: (e) => {
|
|
896
1023
|
fetching.current = true;
|
|
@@ -915,21 +1042,23 @@ var WhenVisible = ({ children, data, params, buffer, as, always, fallback }) =>
|
|
|
915
1042
|
observer.disconnect();
|
|
916
1043
|
};
|
|
917
1044
|
}, [ref, getReloadParams, buffer]);
|
|
1045
|
+
const resolveChildren = () => typeof children === "function" ? children() : children;
|
|
1046
|
+
const resolveFallback = () => typeof fallback === "function" ? fallback() : fallback;
|
|
918
1047
|
if (always || !loaded) {
|
|
919
|
-
return (0,
|
|
1048
|
+
return (0, import_react14.createElement)(
|
|
920
1049
|
as,
|
|
921
1050
|
{
|
|
922
1051
|
props: null,
|
|
923
1052
|
ref
|
|
924
1053
|
},
|
|
925
|
-
loaded ?
|
|
1054
|
+
loaded ? resolveChildren() : resolveFallback()
|
|
926
1055
|
);
|
|
927
1056
|
}
|
|
928
|
-
return loaded ?
|
|
1057
|
+
return loaded ? resolveChildren() : null;
|
|
929
1058
|
};
|
|
930
1059
|
WhenVisible.displayName = "InertiaWhenVisible";
|
|
931
1060
|
var WhenVisible_default = WhenVisible;
|
|
932
1061
|
|
|
933
1062
|
// src/index.ts
|
|
934
|
-
var router3 =
|
|
1063
|
+
var router3 = import_core10.router;
|
|
935
1064
|
//# sourceMappingURL=index.js.map
|