@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.esm.js CHANGED
@@ -2469,6 +2469,108 @@ var Router = class {
2469
2469
  }
2470
2470
  };
2471
2471
 
2472
+ // src/useFormUtils.ts
2473
+ var UseFormUtils = class {
2474
+ /**
2475
+ * Creates a callback that returns a UrlMethodPair.
2476
+ *
2477
+ * createWayfinderCallback(urlMethodPair)
2478
+ * createWayfinderCallback(method, url)
2479
+ * createWayfinderCallback(() => urlMethodPair)
2480
+ * createWayfinderCallback(() => method, () => url)
2481
+ */
2482
+ static createWayfinderCallback(...args) {
2483
+ return () => {
2484
+ if (args.length === 1) {
2485
+ return isUrlMethodPair(args[0]) ? args[0] : args[0]();
2486
+ }
2487
+ return {
2488
+ method: typeof args[0] === "function" ? args[0]() : args[0],
2489
+ url: typeof args[1] === "function" ? args[1]() : args[1]
2490
+ };
2491
+ };
2492
+ }
2493
+ /**
2494
+ * Parses all useForm() arguments into { rememberKey, data, precognitionEndpoint }.
2495
+ *
2496
+ * useForm(data)
2497
+ * useForm(rememberKey, data)
2498
+ * useForm(method, url, data)
2499
+ * useForm(urlMethodPair, data)
2500
+ *
2501
+ */
2502
+ static parseUseFormArguments(...args) {
2503
+ if (args.length === 1) {
2504
+ return {
2505
+ rememberKey: null,
2506
+ data: args[0],
2507
+ precognitionEndpoint: null
2508
+ };
2509
+ }
2510
+ if (args.length === 2) {
2511
+ if (typeof args[0] === "string") {
2512
+ return {
2513
+ rememberKey: args[0],
2514
+ data: args[1],
2515
+ precognitionEndpoint: null
2516
+ };
2517
+ }
2518
+ return {
2519
+ rememberKey: null,
2520
+ data: args[1],
2521
+ precognitionEndpoint: this.createWayfinderCallback(args[0])
2522
+ };
2523
+ }
2524
+ return {
2525
+ rememberKey: null,
2526
+ data: args[2],
2527
+ precognitionEndpoint: this.createWayfinderCallback(args[0], args[1])
2528
+ };
2529
+ }
2530
+ /**
2531
+ * Parses all submission arguments into { method, url, options }.
2532
+ * It uses the Precognition endpoint if no explicit method/url are provided.
2533
+ *
2534
+ * form.submit(method, url)
2535
+ * form.submit(method, url, options)
2536
+ * form.submit(urlMethodPair)
2537
+ * form.submit(urlMethodPair, options)
2538
+ * form.submit()
2539
+ * form.submit(options)
2540
+ */
2541
+ static parseSubmitArguments(args, precognitionEndpoint) {
2542
+ if (args.length === 3 || args.length === 2 && typeof args[0] === "string") {
2543
+ return { method: args[0], url: args[1], options: args[2] ?? {} };
2544
+ }
2545
+ if (isUrlMethodPair(args[0])) {
2546
+ return { ...args[0], options: args[1] ?? {} };
2547
+ }
2548
+ return { ...precognitionEndpoint(), options: args[0] ?? {} };
2549
+ }
2550
+ /**
2551
+ * Merges headers into the Precognition validate() arguments.
2552
+ */
2553
+ static mergeHeadersForValidation(field, config2, headers) {
2554
+ const merge = (config3) => {
2555
+ config3.headers = {
2556
+ ...headers ?? {},
2557
+ ...config3.headers ?? {}
2558
+ };
2559
+ return config3;
2560
+ };
2561
+ if (field && typeof field === "object" && !("target" in field)) {
2562
+ field = merge(field);
2563
+ } else if (config2 && typeof config2 === "object") {
2564
+ config2 = merge(config2);
2565
+ } else if (typeof field === "string") {
2566
+ config2 = merge(config2 ?? {});
2567
+ } else {
2568
+ field = merge(field ?? {});
2569
+ }
2570
+ return [field, config2];
2571
+ }
2572
+ };
2573
+
2472
2574
  // src/domUtils.ts
2473
2575
  var elementInViewport = (el) => {
2474
2576
  if (el.offsetParent === null) {
@@ -3777,7 +3879,8 @@ function resetFormFields(formElement, defaults, fieldNames) {
3777
3879
  if (!formElement) {
3778
3880
  return;
3779
3881
  }
3780
- if (!fieldNames || fieldNames.length === 0) {
3882
+ const resetEntireForm = !fieldNames || fieldNames.length === 0;
3883
+ if (resetEntireForm) {
3781
3884
  const formData = new FormData(formElement);
3782
3885
  const formElementNames = Array.from(formElement.elements).map((el) => isFormElement(el) ? el.name : "").filter(Boolean);
3783
3886
  fieldNames = [.../* @__PURE__ */ new Set([...defaults.keys(), ...formData.keys(), ...formElementNames])];
@@ -3791,7 +3894,7 @@ function resetFormFields(formElement, defaults, fieldNames) {
3791
3894
  }
3792
3895
  }
3793
3896
  });
3794
- if (hasChanged) {
3897
+ if (hasChanged && resetEntireForm) {
3795
3898
  formElement.dispatchEvent(new Event("reset", { bubbles: true }));
3796
3899
  }
3797
3900
  }
@@ -3799,6 +3902,7 @@ function resetFormFields(formElement, defaults, fieldNames) {
3799
3902
  // src/index.ts
3800
3903
  var router = new Router();
3801
3904
  export {
3905
+ UseFormUtils,
3802
3906
  config,
3803
3907
  createHeadManager,
3804
3908
  formDataToObject,