@inertiajs/core 2.2.21 → 2.3.1

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 CHANGED
@@ -30,6 +30,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
+ UseFormUtils: () => UseFormUtils,
33
34
  config: () => config,
34
35
  createHeadManager: () => createHeadManager,
35
36
  formDataToObject: () => formDataToObject,
@@ -630,12 +631,13 @@ var prefetchedRequests = new PrefetchedRequests();
630
631
  // src/scroll.ts
631
632
  var Scroll = class {
632
633
  static save() {
633
- history.saveScrollPositions(
634
- Array.from(this.regions()).map((region) => ({
635
- top: region.scrollTop,
636
- left: region.scrollLeft
637
- }))
638
- );
634
+ history.saveScrollPositions(this.getScrollRegions());
635
+ }
636
+ static getScrollRegions() {
637
+ return Array.from(this.regions()).map((region) => ({
638
+ top: region.scrollTop,
639
+ left: region.scrollLeft
640
+ }));
639
641
  }
640
642
  static regions() {
641
643
  return document.querySelectorAll("[scroll-region]");
@@ -874,7 +876,7 @@ var CurrentPage = class {
874
876
  page2.rememberedState ?? (page2.rememberedState = {});
875
877
  const isServer2 = typeof window === "undefined";
876
878
  const location = !isServer2 ? window.location : new URL(page2.url);
877
- const scrollRegions = !isServer2 && preserveScroll ? history.getScrollRegions() : [];
879
+ const scrollRegions = !isServer2 && preserveScroll ? Scroll.getScrollRegions() : [];
878
880
  replace = replace || isSameUrlWithoutHash(hrefToUrl(page2.url), location);
879
881
  return new Promise((resolve) => {
880
882
  replace ? history.replaceState(page2, () => resolve(null)) : history.pushState(page2, () => resolve(null));
@@ -2525,6 +2527,108 @@ var Router = class {
2525
2527
  }
2526
2528
  };
2527
2529
 
2530
+ // src/useFormUtils.ts
2531
+ var UseFormUtils = class {
2532
+ /**
2533
+ * Creates a callback that returns a UrlMethodPair.
2534
+ *
2535
+ * createWayfinderCallback(urlMethodPair)
2536
+ * createWayfinderCallback(method, url)
2537
+ * createWayfinderCallback(() => urlMethodPair)
2538
+ * createWayfinderCallback(() => method, () => url)
2539
+ */
2540
+ static createWayfinderCallback(...args) {
2541
+ return () => {
2542
+ if (args.length === 1) {
2543
+ return isUrlMethodPair(args[0]) ? args[0] : args[0]();
2544
+ }
2545
+ return {
2546
+ method: typeof args[0] === "function" ? args[0]() : args[0],
2547
+ url: typeof args[1] === "function" ? args[1]() : args[1]
2548
+ };
2549
+ };
2550
+ }
2551
+ /**
2552
+ * Parses all useForm() arguments into { rememberKey, data, precognitionEndpoint }.
2553
+ *
2554
+ * useForm(data)
2555
+ * useForm(rememberKey, data)
2556
+ * useForm(method, url, data)
2557
+ * useForm(urlMethodPair, data)
2558
+ *
2559
+ */
2560
+ static parseUseFormArguments(...args) {
2561
+ if (args.length === 1) {
2562
+ return {
2563
+ rememberKey: null,
2564
+ data: args[0],
2565
+ precognitionEndpoint: null
2566
+ };
2567
+ }
2568
+ if (args.length === 2) {
2569
+ if (typeof args[0] === "string") {
2570
+ return {
2571
+ rememberKey: args[0],
2572
+ data: args[1],
2573
+ precognitionEndpoint: null
2574
+ };
2575
+ }
2576
+ return {
2577
+ rememberKey: null,
2578
+ data: args[1],
2579
+ precognitionEndpoint: this.createWayfinderCallback(args[0])
2580
+ };
2581
+ }
2582
+ return {
2583
+ rememberKey: null,
2584
+ data: args[2],
2585
+ precognitionEndpoint: this.createWayfinderCallback(args[0], args[1])
2586
+ };
2587
+ }
2588
+ /**
2589
+ * Parses all submission arguments into { method, url, options }.
2590
+ * It uses the Precognition endpoint if no explicit method/url are provided.
2591
+ *
2592
+ * form.submit(method, url)
2593
+ * form.submit(method, url, options)
2594
+ * form.submit(urlMethodPair)
2595
+ * form.submit(urlMethodPair, options)
2596
+ * form.submit()
2597
+ * form.submit(options)
2598
+ */
2599
+ static parseSubmitArguments(args, precognitionEndpoint) {
2600
+ if (args.length === 3 || args.length === 2 && typeof args[0] === "string") {
2601
+ return { method: args[0], url: args[1], options: args[2] ?? {} };
2602
+ }
2603
+ if (isUrlMethodPair(args[0])) {
2604
+ return { ...args[0], options: args[1] ?? {} };
2605
+ }
2606
+ return { ...precognitionEndpoint(), options: args[0] ?? {} };
2607
+ }
2608
+ /**
2609
+ * Merges headers into the Precognition validate() arguments.
2610
+ */
2611
+ static mergeHeadersForValidation(field, config2, headers) {
2612
+ const merge = (config3) => {
2613
+ config3.headers = {
2614
+ ...headers ?? {},
2615
+ ...config3.headers ?? {}
2616
+ };
2617
+ return config3;
2618
+ };
2619
+ if (field && typeof field === "object" && !("target" in field)) {
2620
+ field = merge(field);
2621
+ } else if (config2 && typeof config2 === "object") {
2622
+ config2 = merge(config2);
2623
+ } else if (typeof field === "string") {
2624
+ config2 = merge(config2 ?? {});
2625
+ } else {
2626
+ field = merge(field ?? {});
2627
+ }
2628
+ return [field, config2];
2629
+ }
2630
+ };
2631
+
2528
2632
  // src/domUtils.ts
2529
2633
  var elementInViewport = (el) => {
2530
2634
  if (el.offsetParent === null) {
@@ -3833,7 +3937,8 @@ function resetFormFields(formElement, defaults, fieldNames) {
3833
3937
  if (!formElement) {
3834
3938
  return;
3835
3939
  }
3836
- if (!fieldNames || fieldNames.length === 0) {
3940
+ const resetEntireForm = !fieldNames || fieldNames.length === 0;
3941
+ if (resetEntireForm) {
3837
3942
  const formData = new FormData(formElement);
3838
3943
  const formElementNames = Array.from(formElement.elements).map((el) => isFormElement(el) ? el.name : "").filter(Boolean);
3839
3944
  fieldNames = [.../* @__PURE__ */ new Set([...defaults.keys(), ...formData.keys(), ...formElementNames])];
@@ -3847,7 +3952,7 @@ function resetFormFields(formElement, defaults, fieldNames) {
3847
3952
  }
3848
3953
  }
3849
3954
  });
3850
- if (hasChanged) {
3955
+ if (hasChanged && resetEntireForm) {
3851
3956
  formElement.dispatchEvent(new Event("reset", { bubbles: true }));
3852
3957
  }
3853
3958
  }