@inertiajs/vue3 2.0.17 → 2.1.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.esm.js CHANGED
@@ -64,7 +64,9 @@ var remember = {
64
64
  var remember_default = remember;
65
65
 
66
66
  // src/useForm.ts
67
- import { router as router2 } from "@inertiajs/core";
67
+ import {
68
+ router as router2
69
+ } from "@inertiajs/core";
68
70
  import { cloneDeep as cloneDeep2, isEqual } from "es-toolkit";
69
71
  import { get, has, set } from "es-toolkit/compat";
70
72
  import { reactive, watch } from "vue";
@@ -433,10 +435,202 @@ var deferred_default = defineComponent2({
433
435
  }
434
436
  });
435
437
 
438
+ // src/form.ts
439
+ import {
440
+ formDataToObject,
441
+ mergeDataIntoQueryString,
442
+ resetFormFields
443
+ } from "@inertiajs/core";
444
+ import { isEqual as isEqual2 } from "es-toolkit";
445
+ import { computed as computed2, defineComponent as defineComponent3, h as h3, onBeforeUnmount, onMounted, ref as ref2 } from "vue";
446
+ var noop = () => void 0;
447
+ var Form = defineComponent3({
448
+ name: "Form",
449
+ props: {
450
+ action: {
451
+ type: [String, Object],
452
+ default: ""
453
+ },
454
+ method: {
455
+ type: String,
456
+ default: "get"
457
+ },
458
+ headers: {
459
+ type: Object,
460
+ default: () => ({})
461
+ },
462
+ queryStringArrayFormat: {
463
+ type: String,
464
+ default: "brackets"
465
+ },
466
+ errorBag: {
467
+ type: [String, null],
468
+ default: null
469
+ },
470
+ showProgress: {
471
+ type: Boolean,
472
+ default: true
473
+ },
474
+ transform: {
475
+ type: Function,
476
+ default: (data) => data
477
+ },
478
+ options: {
479
+ type: Object,
480
+ default: () => ({})
481
+ },
482
+ onCancelToken: {
483
+ type: Function,
484
+ default: noop
485
+ },
486
+ onBefore: {
487
+ type: Function,
488
+ default: noop
489
+ },
490
+ onStart: {
491
+ type: Function,
492
+ default: noop
493
+ },
494
+ onProgress: {
495
+ type: Function,
496
+ default: noop
497
+ },
498
+ onFinish: {
499
+ type: Function,
500
+ default: noop
501
+ },
502
+ onCancel: {
503
+ type: Function,
504
+ default: noop
505
+ },
506
+ onSuccess: {
507
+ type: Function,
508
+ default: noop
509
+ },
510
+ onError: {
511
+ type: Function,
512
+ default: noop
513
+ },
514
+ onSubmitComplete: {
515
+ type: Function,
516
+ default: noop
517
+ },
518
+ disableWhileProcessing: {
519
+ type: Boolean,
520
+ default: false
521
+ }
522
+ },
523
+ setup(props, { slots, attrs, expose }) {
524
+ const form = useForm({});
525
+ const formElement = ref2();
526
+ const method = computed2(
527
+ () => typeof props.action === "object" ? props.action.method : props.method.toLowerCase()
528
+ );
529
+ const isDirty = ref2(false);
530
+ const defaultData = ref2(new FormData());
531
+ const onFormUpdate = (event) => {
532
+ isDirty.value = event.type === "reset" ? false : !isEqual2(getData(), formDataToObject(defaultData.value));
533
+ };
534
+ const formEvents = ["input", "change", "reset"];
535
+ onMounted(() => {
536
+ defaultData.value = getFormData();
537
+ formEvents.forEach((e) => formElement.value.addEventListener(e, onFormUpdate));
538
+ });
539
+ onBeforeUnmount(() => formEvents.forEach((e) => formElement.value?.removeEventListener(e, onFormUpdate)));
540
+ const getFormData = () => new FormData(formElement.value);
541
+ const getData = () => formDataToObject(getFormData());
542
+ const submit = () => {
543
+ const [action, data] = mergeDataIntoQueryString(
544
+ method.value,
545
+ typeof props.action === "object" ? props.action.url : props.action,
546
+ getData(),
547
+ props.queryStringArrayFormat
548
+ );
549
+ const submitOptions = {
550
+ headers: props.headers,
551
+ errorBag: props.errorBag,
552
+ showProgress: props.showProgress,
553
+ onCancelToken: props.onCancelToken,
554
+ onBefore: props.onBefore,
555
+ onStart: props.onStart,
556
+ onProgress: props.onProgress,
557
+ onFinish: props.onFinish,
558
+ onCancel: props.onCancel,
559
+ onSuccess: (...args) => {
560
+ props.onSuccess(...args);
561
+ props.onSubmitComplete(exposed);
562
+ },
563
+ onError: props.onError,
564
+ ...props.options
565
+ };
566
+ form.transform(() => props.transform(data)).submit(method.value, action, submitOptions);
567
+ };
568
+ const reset = (...fields) => {
569
+ resetFormFields(formElement.value, defaultData.value, fields);
570
+ };
571
+ const resetAndClearErrors = (...fields) => {
572
+ form.clearErrors(...fields);
573
+ reset(...fields);
574
+ };
575
+ const defaults = () => {
576
+ defaultData.value = getFormData();
577
+ isDirty.value = false;
578
+ };
579
+ const exposed = {
580
+ get errors() {
581
+ return form.errors;
582
+ },
583
+ get hasErrors() {
584
+ return form.hasErrors;
585
+ },
586
+ get processing() {
587
+ return form.processing;
588
+ },
589
+ get progress() {
590
+ return form.progress;
591
+ },
592
+ get wasSuccessful() {
593
+ return form.wasSuccessful;
594
+ },
595
+ get recentlySuccessful() {
596
+ return form.recentlySuccessful;
597
+ },
598
+ clearErrors: (...fields) => form.clearErrors(...fields),
599
+ resetAndClearErrors,
600
+ setError: (fieldOrFields, maybeValue) => form.setError(typeof fieldOrFields === "string" ? { [fieldOrFields]: maybeValue } : fieldOrFields),
601
+ get isDirty() {
602
+ return isDirty.value;
603
+ },
604
+ reset,
605
+ submit,
606
+ defaults
607
+ };
608
+ expose(exposed);
609
+ return () => {
610
+ return h3(
611
+ "form",
612
+ {
613
+ ...attrs,
614
+ ref: formElement,
615
+ action: props.action,
616
+ method: method.value,
617
+ onSubmit: (event) => {
618
+ event.preventDefault();
619
+ submit();
620
+ },
621
+ inert: props.disableWhileProcessing && form.processing
622
+ },
623
+ slots.default ? slots.default(exposed) : []
624
+ );
625
+ };
626
+ }
627
+ });
628
+ var form_default = Form;
629
+
436
630
  // src/head.ts
437
631
  import { escape } from "es-toolkit";
438
- import { defineComponent as defineComponent3 } from "vue";
439
- var Head = defineComponent3({
632
+ import { defineComponent as defineComponent4 } from "vue";
633
+ var Head = defineComponent4({
440
634
  props: {
441
635
  title: {
442
636
  type: String,
@@ -557,16 +751,18 @@ var head_default = Head;
557
751
 
558
752
  // src/link.ts
559
753
  import {
560
- mergeDataIntoQueryString,
754
+ mergeDataIntoQueryString as mergeDataIntoQueryString2,
561
755
  router as router5,
562
756
  shouldIntercept
563
757
  } from "@inertiajs/core";
564
- import { computed as computed2, defineComponent as defineComponent4, h as h3, onMounted, onUnmounted, ref as ref2 } from "vue";
565
- var Link = defineComponent4({
758
+ import { computed as computed3, defineComponent as defineComponent5, h as h4, onMounted as onMounted2, onUnmounted, ref as ref3 } from "vue";
759
+ var noop2 = () => {
760
+ };
761
+ var Link = defineComponent5({
566
762
  name: "Link",
567
763
  props: {
568
764
  as: {
569
- type: String,
765
+ type: [String, Object],
570
766
  default: "a"
571
767
  },
572
768
  data: {
@@ -575,7 +771,7 @@ var Link = defineComponent4({
575
771
  },
576
772
  href: {
577
773
  type: [String, Object],
578
- required: true
774
+ default: ""
579
775
  },
580
776
  method: {
581
777
  type: String,
@@ -623,49 +819,49 @@ var Link = defineComponent4({
623
819
  },
624
820
  onStart: {
625
821
  type: Function,
626
- default: (_visit) => {
627
- }
822
+ default: noop2
628
823
  },
629
824
  onProgress: {
630
825
  type: Function,
631
- default: () => {
632
- }
826
+ default: noop2
633
827
  },
634
828
  onFinish: {
635
829
  type: Function,
636
- default: () => {
637
- }
830
+ default: noop2
638
831
  },
639
832
  onBefore: {
640
833
  type: Function,
641
- default: () => {
642
- }
834
+ default: noop2
643
835
  },
644
836
  onCancel: {
645
837
  type: Function,
646
- default: () => {
647
- }
838
+ default: noop2
648
839
  },
649
840
  onSuccess: {
650
841
  type: Function,
651
- default: () => {
652
- }
842
+ default: noop2
653
843
  },
654
844
  onError: {
655
845
  type: Function,
656
- default: () => {
657
- }
846
+ default: noop2
658
847
  },
659
848
  onCancelToken: {
660
849
  type: Function,
661
- default: () => {
662
- }
850
+ default: noop2
851
+ },
852
+ onPrefetching: {
853
+ type: Function,
854
+ default: noop2
855
+ },
856
+ onPrefetched: {
857
+ type: Function,
858
+ default: noop2
663
859
  }
664
860
  },
665
861
  setup(props, { slots, attrs }) {
666
- const inFlightCount = ref2(0);
667
- const hoverTimeout = ref2(null);
668
- const prefetchModes = computed2(() => {
862
+ const inFlightCount = ref3(0);
863
+ const hoverTimeout = ref3(null);
864
+ const prefetchModes = computed3(() => {
669
865
  if (props.prefetch === true) {
670
866
  return ["hover"];
671
867
  }
@@ -677,7 +873,7 @@ var Link = defineComponent4({
677
873
  }
678
874
  return [props.prefetch];
679
875
  });
680
- const cacheForValue = computed2(() => {
876
+ const cacheForValue = computed3(() => {
681
877
  if (props.cacheFor !== 0) {
682
878
  return props.cacheFor;
683
879
  }
@@ -686,7 +882,7 @@ var Link = defineComponent4({
686
882
  }
687
883
  return 3e4;
688
884
  });
689
- onMounted(() => {
885
+ onMounted2(() => {
690
886
  if (prefetchModes.value.includes("mount")) {
691
887
  prefetch();
692
888
  }
@@ -694,25 +890,35 @@ var Link = defineComponent4({
694
890
  onUnmounted(() => {
695
891
  clearTimeout(hoverTimeout.value);
696
892
  });
697
- const method = computed2(
893
+ const method = computed3(
698
894
  () => typeof props.href === "object" ? props.href.method : props.method.toLowerCase()
699
895
  );
700
- const as = computed2(() => method.value !== "get" ? "button" : props.as.toLowerCase());
701
- const mergeDataArray = computed2(
702
- () => mergeDataIntoQueryString(
896
+ const as = computed3(() => {
897
+ if (typeof props.as !== "string") {
898
+ return props.as;
899
+ }
900
+ return method.value !== "get" ? "button" : props.as.toLowerCase();
901
+ });
902
+ const mergeDataArray = computed3(
903
+ () => mergeDataIntoQueryString2(
703
904
  method.value,
704
- typeof props.href === "object" ? props.href.url : props.href || "",
905
+ typeof props.href === "object" ? props.href.url : props.href,
705
906
  props.data,
706
907
  props.queryStringArrayFormat
707
908
  )
708
909
  );
709
- const href = computed2(() => mergeDataArray.value[0]);
710
- const data = computed2(() => mergeDataArray.value[1]);
711
- const elProps = computed2(() => ({
712
- a: { href: href.value },
713
- button: { type: "button" }
714
- }));
715
- const baseParams = computed2(() => ({
910
+ const href = computed3(() => mergeDataArray.value[0]);
911
+ const data = computed3(() => mergeDataArray.value[1]);
912
+ const elProps = computed3(() => {
913
+ if (as.value === "button") {
914
+ return { type: "button" };
915
+ }
916
+ if (as.value === "a" || typeof as.value !== "string") {
917
+ return { href: href.value };
918
+ }
919
+ return {};
920
+ });
921
+ const baseParams = computed3(() => ({
716
922
  data: data.value,
717
923
  method: method.value,
718
924
  replace: props.replace,
@@ -723,25 +929,35 @@ var Link = defineComponent4({
723
929
  headers: props.headers,
724
930
  async: props.async
725
931
  }));
726
- const visitParams = computed2(() => ({
932
+ const visitParams = computed3(() => ({
727
933
  ...baseParams.value,
728
934
  onCancelToken: props.onCancelToken,
729
935
  onBefore: props.onBefore,
730
- onStart: (event) => {
936
+ onStart: (visit) => {
731
937
  inFlightCount.value++;
732
- props.onStart(event);
938
+ props.onStart(visit);
733
939
  },
734
940
  onProgress: props.onProgress,
735
- onFinish: (event) => {
941
+ onFinish: (visit) => {
736
942
  inFlightCount.value--;
737
- props.onFinish(event);
943
+ props.onFinish(visit);
738
944
  },
739
945
  onCancel: props.onCancel,
740
946
  onSuccess: props.onSuccess,
741
947
  onError: props.onError
742
948
  }));
743
949
  const prefetch = () => {
744
- router5.prefetch(href.value, baseParams.value, { cacheFor: cacheForValue.value });
950
+ router5.prefetch(
951
+ href.value,
952
+ {
953
+ ...baseParams.value,
954
+ onPrefetching: props.onPrefetching,
955
+ onPrefetched: props.onPrefetched
956
+ },
957
+ {
958
+ cacheFor: cacheForValue.value
959
+ }
960
+ );
745
961
  };
746
962
  const regularEvents = {
747
963
  onClick: (event) => {
@@ -780,11 +996,11 @@ var Link = defineComponent4({
780
996
  }
781
997
  };
782
998
  return () => {
783
- return h3(
999
+ return h4(
784
1000
  as.value,
785
1001
  {
786
1002
  ...attrs,
787
- ...elProps.value[as.value] || {},
1003
+ ...elProps.value,
788
1004
  "data-loading": inFlightCount.value > 0 ? "" : void 0,
789
1005
  ...(() => {
790
1006
  if (prefetchModes.value.includes("hover")) {
@@ -805,7 +1021,7 @@ var link_default = Link;
805
1021
 
806
1022
  // src/usePoll.ts
807
1023
  import { router as router6 } from "@inertiajs/core";
808
- import { onMounted as onMounted2, onUnmounted as onUnmounted2 } from "vue";
1024
+ import { onMounted as onMounted3, onUnmounted as onUnmounted2 } from "vue";
809
1025
  function usePoll(interval, requestOptions = {}, options = {
810
1026
  keepAlive: false,
811
1027
  autoStart: true
@@ -814,7 +1030,7 @@ function usePoll(interval, requestOptions = {}, options = {
814
1030
  ...options,
815
1031
  autoStart: false
816
1032
  });
817
- onMounted2(() => {
1033
+ onMounted3(() => {
818
1034
  if (options.autoStart ?? true) {
819
1035
  start();
820
1036
  }
@@ -830,11 +1046,11 @@ function usePoll(interval, requestOptions = {}, options = {
830
1046
 
831
1047
  // src/usePrefetch.ts
832
1048
  import { router as router7 } from "@inertiajs/core";
833
- import { onMounted as onMounted3, onUnmounted as onUnmounted3, ref as ref3 } from "vue";
1049
+ import { onMounted as onMounted4, onUnmounted as onUnmounted3, ref as ref4 } from "vue";
834
1050
  function usePrefetch(options = {}) {
835
- const isPrefetching = ref3(false);
836
- const lastUpdatedAt = ref3(null);
837
- const isPrefetched = ref3(false);
1051
+ const isPrefetching = ref4(false);
1052
+ const lastUpdatedAt = ref4(null);
1053
+ const isPrefetched = ref4(false);
838
1054
  const cached = typeof window === "undefined" ? null : router7.getCached(window.location.pathname, options);
839
1055
  const inFlight = typeof window === "undefined" ? null : router7.getPrefetching(window.location.pathname, options);
840
1056
  lastUpdatedAt.value = cached?.staleTimestamp || null;
@@ -842,7 +1058,7 @@ function usePrefetch(options = {}) {
842
1058
  isPrefetched.value = cached !== null;
843
1059
  let onPrefetchedListener;
844
1060
  let onPrefetchingListener;
845
- onMounted3(() => {
1061
+ onMounted4(() => {
846
1062
  onPrefetchingListener = router7.on("prefetching", (e) => {
847
1063
  if (e.detail.visit.url.pathname === window.location.pathname) {
848
1064
  isPrefetching.value = true;
@@ -870,13 +1086,13 @@ function usePrefetch(options = {}) {
870
1086
  // src/useRemember.ts
871
1087
  import { router as router8 } from "@inertiajs/core";
872
1088
  import { cloneDeep as cloneDeep3 } from "es-toolkit";
873
- import { isReactive, reactive as reactive3, ref as ref4, watch as watch2 } from "vue";
1089
+ import { isReactive, reactive as reactive3, ref as ref5, watch as watch2 } from "vue";
874
1090
  function useRemember(data, key2) {
875
1091
  if (typeof data === "object" && data !== null && data.__rememberable === false) {
876
1092
  return data;
877
1093
  }
878
1094
  const restored = router8.restore(key2);
879
- const type = isReactive(data) ? reactive3 : ref4;
1095
+ const type = isReactive(data) ? reactive3 : ref5;
880
1096
  const hasCallbacks = typeof data.__remember === "function" && typeof data.__restore === "function";
881
1097
  const remembered = type(restored === void 0 ? data : hasCallbacks ? data.__restore(restored) : restored);
882
1098
  watch2(
@@ -891,8 +1107,8 @@ function useRemember(data, key2) {
891
1107
 
892
1108
  // src/whenVisible.ts
893
1109
  import { router as router9 } from "@inertiajs/core";
894
- import { defineComponent as defineComponent5, h as h4 } from "vue";
895
- var whenVisible_default = defineComponent5({
1110
+ import { defineComponent as defineComponent6, h as h5 } from "vue";
1111
+ var whenVisible_default = defineComponent6({
896
1112
  name: "WhenVisible",
897
1113
  props: {
898
1114
  data: {
@@ -973,7 +1189,7 @@ var whenVisible_default = defineComponent5({
973
1189
  render() {
974
1190
  const els = [];
975
1191
  if (this.$props.always || !this.loaded) {
976
- els.push(h4(this.$props.as));
1192
+ els.push(h5(this.$props.as));
977
1193
  }
978
1194
  if (!this.loaded) {
979
1195
  els.push(this.$slots.fallback ? this.$slots.fallback() : null);
@@ -985,6 +1201,7 @@ var whenVisible_default = defineComponent5({
985
1201
  });
986
1202
  export {
987
1203
  deferred_default as Deferred,
1204
+ form_default as Form,
988
1205
  head_default as Head,
989
1206
  link_default as Link,
990
1207
  whenVisible_default as WhenVisible,