@inertiajs/core 2.2.21 → 2.3.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.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,
@@ -2525,6 +2526,108 @@ var Router = class {
2525
2526
  }
2526
2527
  };
2527
2528
 
2529
+ // src/useFormUtils.ts
2530
+ var UseFormUtils = class {
2531
+ /**
2532
+ * Creates a callback that returns a UrlMethodPair.
2533
+ *
2534
+ * createWayfinderCallback(urlMethodPair)
2535
+ * createWayfinderCallback(method, url)
2536
+ * createWayfinderCallback(() => urlMethodPair)
2537
+ * createWayfinderCallback(() => method, () => url)
2538
+ */
2539
+ static createWayfinderCallback(...args) {
2540
+ return () => {
2541
+ if (args.length === 1) {
2542
+ return isUrlMethodPair(args[0]) ? args[0] : args[0]();
2543
+ }
2544
+ return {
2545
+ method: typeof args[0] === "function" ? args[0]() : args[0],
2546
+ url: typeof args[1] === "function" ? args[1]() : args[1]
2547
+ };
2548
+ };
2549
+ }
2550
+ /**
2551
+ * Parses all useForm() arguments into { rememberKey, data, precognitionEndpoint }.
2552
+ *
2553
+ * useForm(data)
2554
+ * useForm(rememberKey, data)
2555
+ * useForm(method, url, data)
2556
+ * useForm(urlMethodPair, data)
2557
+ *
2558
+ */
2559
+ static parseUseFormArguments(...args) {
2560
+ if (args.length === 1) {
2561
+ return {
2562
+ rememberKey: null,
2563
+ data: args[0],
2564
+ precognitionEndpoint: null
2565
+ };
2566
+ }
2567
+ if (args.length === 2) {
2568
+ if (typeof args[0] === "string") {
2569
+ return {
2570
+ rememberKey: args[0],
2571
+ data: args[1],
2572
+ precognitionEndpoint: null
2573
+ };
2574
+ }
2575
+ return {
2576
+ rememberKey: null,
2577
+ data: args[1],
2578
+ precognitionEndpoint: this.createWayfinderCallback(args[0])
2579
+ };
2580
+ }
2581
+ return {
2582
+ rememberKey: null,
2583
+ data: args[2],
2584
+ precognitionEndpoint: this.createWayfinderCallback(args[0], args[1])
2585
+ };
2586
+ }
2587
+ /**
2588
+ * Parses all submission arguments into { method, url, options }.
2589
+ * It uses the Precognition endpoint if no explicit method/url are provided.
2590
+ *
2591
+ * form.submit(method, url)
2592
+ * form.submit(method, url, options)
2593
+ * form.submit(urlMethodPair)
2594
+ * form.submit(urlMethodPair, options)
2595
+ * form.submit()
2596
+ * form.submit(options)
2597
+ */
2598
+ static parseSubmitArguments(args, precognitionEndpoint) {
2599
+ if (args.length === 3 || args.length === 2 && typeof args[0] === "string") {
2600
+ return { method: args[0], url: args[1], options: args[2] ?? {} };
2601
+ }
2602
+ if (isUrlMethodPair(args[0])) {
2603
+ return { ...args[0], options: args[1] ?? {} };
2604
+ }
2605
+ return { ...precognitionEndpoint(), options: args[0] ?? {} };
2606
+ }
2607
+ /**
2608
+ * Merges headers into the Precognition validate() arguments.
2609
+ */
2610
+ static mergeHeadersForValidation(field, config2, headers) {
2611
+ const merge = (config3) => {
2612
+ config3.headers = {
2613
+ ...headers ?? {},
2614
+ ...config3.headers ?? {}
2615
+ };
2616
+ return config3;
2617
+ };
2618
+ if (field && typeof field === "object" && !("target" in field)) {
2619
+ field = merge(field);
2620
+ } else if (config2 && typeof config2 === "object") {
2621
+ config2 = merge(config2);
2622
+ } else if (typeof field === "string") {
2623
+ config2 = merge(config2 ?? {});
2624
+ } else {
2625
+ field = merge(field ?? {});
2626
+ }
2627
+ return [field, config2];
2628
+ }
2629
+ };
2630
+
2528
2631
  // src/domUtils.ts
2529
2632
  var elementInViewport = (el) => {
2530
2633
  if (el.offsetParent === null) {
@@ -3833,7 +3936,8 @@ function resetFormFields(formElement, defaults, fieldNames) {
3833
3936
  if (!formElement) {
3834
3937
  return;
3835
3938
  }
3836
- if (!fieldNames || fieldNames.length === 0) {
3939
+ const resetEntireForm = !fieldNames || fieldNames.length === 0;
3940
+ if (resetEntireForm) {
3837
3941
  const formData = new FormData(formElement);
3838
3942
  const formElementNames = Array.from(formElement.elements).map((el) => isFormElement(el) ? el.name : "").filter(Boolean);
3839
3943
  fieldNames = [.../* @__PURE__ */ new Set([...defaults.keys(), ...formData.keys(), ...formElementNames])];
@@ -3847,7 +3951,7 @@ function resetFormFields(formElement, defaults, fieldNames) {
3847
3951
  }
3848
3952
  }
3849
3953
  });
3850
- if (hasChanged) {
3954
+ if (hasChanged && resetEntireForm) {
3851
3955
  formElement.dispatchEvent(new Event("reset", { bubbles: true }));
3852
3956
  }
3853
3957
  }