@inertiajs/svelte 2.1.0 → 2.1.2

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.
@@ -1,5 +1,6 @@
1
1
  <script>import {
2
2
  formDataToObject,
3
+ resetFormFields,
3
4
  mergeDataIntoQueryString
4
5
  } from "@inertiajs/core";
5
6
  import { isEqual } from "es-toolkit";
@@ -22,32 +23,71 @@ export let onFinish = noop;
22
23
  export let onCancel = noop;
23
24
  export let onSuccess = noop;
24
25
  export let onError = noop;
26
+ export let onSubmitComplete = noop;
27
+ export let disableWhileProcessing = false;
28
+ export let invalidateCacheTags = [];
29
+ export let resetOnError = false;
30
+ export let resetOnSuccess = false;
31
+ export let setDefaultsOnSuccess = false;
25
32
  const form = useForm({});
26
33
  let formElement;
27
34
  let isDirty = false;
28
- let defaultValues = {};
35
+ let defaultData = new FormData();
29
36
  $: _method = typeof action === "object" ? action.method : method.toLowerCase();
30
37
  $: _action = typeof action === "object" ? action.url : action;
38
+ function getFormData() {
39
+ return new FormData(formElement);
40
+ }
31
41
  function getData() {
32
- return formDataToObject(new FormData(formElement));
42
+ return formDataToObject(getFormData());
33
43
  }
34
44
  function updateDirtyState(event) {
35
- isDirty = event.type === "reset" ? false : !isEqual(getData(), defaultValues);
45
+ isDirty = event.type === "reset" ? false : !isEqual(getData(), formDataToObject(defaultData));
36
46
  }
37
47
  export function submit() {
38
48
  const [url, _data] = mergeDataIntoQueryString(_method, _action, getData(), queryStringArrayFormat);
49
+ const maybeReset = (resetOption) => {
50
+ if (!resetOption) {
51
+ return;
52
+ }
53
+ if (resetOption === true) {
54
+ reset();
55
+ } else if (resetOption.length > 0) {
56
+ reset(...resetOption);
57
+ }
58
+ };
39
59
  const submitOptions = {
40
60
  headers,
41
61
  errorBag,
42
62
  showProgress,
63
+ invalidateCacheTags,
43
64
  onCancelToken,
44
65
  onBefore,
45
66
  onStart,
46
67
  onProgress,
47
68
  onFinish,
48
69
  onCancel,
49
- onSuccess,
50
- onError,
70
+ onSuccess: (...args) => {
71
+ if (onSuccess) {
72
+ onSuccess(...args);
73
+ }
74
+ if (onSubmitComplete) {
75
+ onSubmitComplete({
76
+ reset,
77
+ defaults
78
+ });
79
+ }
80
+ maybeReset(resetOnSuccess);
81
+ if (setDefaultsOnSuccess === true) {
82
+ defaults();
83
+ }
84
+ },
85
+ onError: (...args) => {
86
+ if (onError) {
87
+ onError(...args);
88
+ }
89
+ maybeReset(resetOnError);
90
+ },
51
91
  ...options
52
92
  };
53
93
  $form.transform(() => transform(_data)).submit(_method, url, submitOptions);
@@ -56,14 +96,15 @@ function handleSubmit(event) {
56
96
  event.preventDefault();
57
97
  submit();
58
98
  }
59
- export function reset() {
60
- formElement.reset();
99
+ export function reset(...fields) {
100
+ resetFormFields(formElement, defaultData, fields);
61
101
  }
62
102
  export function clearErrors(...fields) {
63
103
  $form.clearErrors(...fields);
64
104
  }
65
105
  export function resetAndClearErrors(...fields) {
66
- $form.resetAndClearErrors(...fields);
106
+ $form.clearErrors(...fields);
107
+ reset(...fields);
67
108
  }
68
109
  export function setError(field, value) {
69
110
  if (typeof field === "string") {
@@ -72,14 +113,19 @@ export function setError(field, value) {
72
113
  $form.setError(field);
73
114
  }
74
115
  }
116
+ export function defaults() {
117
+ defaultData = getFormData();
118
+ isDirty = false;
119
+ }
75
120
  onMount(() => {
76
- defaultValues = getData();
121
+ defaultData = getFormData();
77
122
  const formEvents = ["input", "change", "reset"];
78
123
  formEvents.forEach((e) => formElement.addEventListener(e, updateDirtyState));
79
124
  return () => {
80
125
  formEvents.forEach((e) => formElement?.removeEventListener(e, updateDirtyState));
81
126
  };
82
127
  });
128
+ $: slotErrors = $form.errors;
83
129
  </script>
84
130
 
85
131
  <form
@@ -87,9 +133,10 @@ onMount(() => {
87
133
  action={_action}
88
134
  method={_method}
89
135
  on:submit={handleSubmit} {...$$restProps}
136
+ inert={disableWhileProcessing && $form.processing ? true : undefined}
90
137
  >
91
138
  <slot
92
- errors={$form.errors}
139
+ errors={slotErrors}
93
140
  hasErrors={$form.hasErrors}
94
141
  processing={$form.processing}
95
142
  progress={$form.progress}
@@ -99,7 +146,7 @@ onMount(() => {
99
146
  {resetAndClearErrors}
100
147
  {setError}
101
148
  {isDirty}
102
- {reset}
103
149
  {submit}
150
+ {defaults}
104
151
  />
105
152
  </form>
@@ -1,13 +1,10 @@
1
1
  import { SvelteComponent } from "svelte";
2
- import { type FormDataConvertible } from '@inertiajs/core';
2
+ import { type Errors, type FormDataConvertible } from '@inertiajs/core';
3
3
  declare const __propDef: {
4
4
  props: {
5
5
  [x: string]: any;
6
- action?: string | {
7
- url: string;
8
- method: import("@inertiajs/core").Method;
9
- } | undefined;
10
- method?: import("@inertiajs/core").Method | undefined;
6
+ action?: string | import("@inertiajs/core").UrlMethodPair | undefined;
7
+ method?: import("@inertiajs/core").Method | "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | undefined;
11
8
  headers?: Record<string, string> | undefined;
12
9
  queryStringArrayFormat?: "indices" | "brackets" | undefined;
13
10
  errorBag?: string | null | undefined;
@@ -17,25 +14,32 @@ declare const __propDef: {
17
14
  onCancelToken?: (({ cancel }: {
18
15
  cancel: VoidFunction;
19
16
  }) => void) | undefined;
20
- onBefore?: import("@inertiajs/core").GlobalEventCallback<"before"> | undefined;
21
- onStart?: import("@inertiajs/core").GlobalEventCallback<"start"> | undefined;
22
- onProgress?: import("@inertiajs/core").GlobalEventCallback<"progress"> | undefined;
23
- onFinish?: import("@inertiajs/core").GlobalEventCallback<"finish"> | undefined;
24
- onCancel?: import("@inertiajs/core").GlobalEventCallback<"cancel"> | undefined;
25
- onSuccess?: import("@inertiajs/core").GlobalEventCallback<"success"> | undefined;
26
- onError?: import("@inertiajs/core").GlobalEventCallback<"error"> | undefined;
17
+ onBefore?: import("@inertiajs/core").GlobalEventCallback<"before", import("@inertiajs/core").RequestPayload> | undefined;
18
+ onStart?: import("@inertiajs/core").GlobalEventCallback<"start", import("@inertiajs/core").RequestPayload> | undefined;
19
+ onProgress?: import("@inertiajs/core").GlobalEventCallback<"progress", import("@inertiajs/core").RequestPayload> | undefined;
20
+ onFinish?: import("@inertiajs/core").GlobalEventCallback<"finish", import("@inertiajs/core").RequestPayload> | undefined;
21
+ onCancel?: import("@inertiajs/core").GlobalEventCallback<"cancel", import("@inertiajs/core").RequestPayload> | undefined;
22
+ onSuccess?: import("@inertiajs/core").GlobalEventCallback<"success", import("@inertiajs/core").RequestPayload> | undefined;
23
+ onError?: import("@inertiajs/core").GlobalEventCallback<"error", import("@inertiajs/core").RequestPayload> | undefined;
24
+ onSubmitComplete?: ((props: import("@inertiajs/core").FormComponentonSubmitCompleteArguments) => void) | undefined;
25
+ disableWhileProcessing?: boolean | undefined;
26
+ invalidateCacheTags?: string | string[] | undefined;
27
+ resetOnError?: boolean | string[] | undefined;
28
+ resetOnSuccess?: boolean | string[] | undefined;
29
+ setDefaultsOnSuccess?: boolean | undefined;
27
30
  submit?: (() => void) | undefined;
28
- reset?: (() => void) | undefined;
31
+ reset?: ((...fields: string[]) => void) | undefined;
29
32
  clearErrors?: ((...fields: string[]) => void) | undefined;
30
33
  resetAndClearErrors?: ((...fields: string[]) => void) | undefined;
31
34
  setError?: ((field: string | object, value?: string) => void) | undefined;
35
+ defaults?: (() => void) | undefined;
32
36
  };
33
37
  events: {
34
38
  [evt: string]: CustomEvent<any>;
35
39
  };
36
40
  slots: {
37
41
  default: {
38
- errors: Partial<Record<never, string>>;
42
+ errors: Errors;
39
43
  hasErrors: boolean;
40
44
  processing: boolean;
41
45
  progress: import("axios").AxiosProgressEvent | null;
@@ -45,8 +49,8 @@ declare const __propDef: {
45
49
  resetAndClearErrors: (...fields: string[]) => void;
46
50
  setError: (field: string | object, value?: string) => void;
47
51
  isDirty: boolean;
48
- reset: () => void;
49
52
  submit: () => void;
53
+ defaults: () => void;
50
54
  };
51
55
  };
52
56
  exports?: undefined;
@@ -57,9 +61,10 @@ export type FormEvents = typeof __propDef.events;
57
61
  export type FormSlots = typeof __propDef.slots;
58
62
  export default class Form extends SvelteComponent<FormProps, FormEvents, FormSlots> {
59
63
  get submit(): () => void;
60
- get reset(): () => void;
64
+ get reset(): (...fields: string[]) => void;
61
65
  get clearErrors(): (...fields: string[]) => void;
62
66
  get resetAndClearErrors(): (...fields: string[]) => void;
63
67
  get setError(): (field: string | object, value?: string) => void;
68
+ get defaults(): () => void;
64
69
  }
65
70
  export {};
@@ -13,6 +13,7 @@ export let queryStringArrayFormat = "brackets";
13
13
  export let async = false;
14
14
  export let prefetch = false;
15
15
  export let cacheFor = 0;
16
+ export let cacheTags = [];
16
17
  $: _method = typeof href === "object" ? href.method : method;
17
18
  $: _href = typeof href === "object" ? href.url : href;
18
19
  $: asProp = _method !== "get" ? "button" : as.toLowerCase();
@@ -39,6 +40,7 @@ $: elProps = {
39
40
  async,
40
41
  prefetch,
41
42
  cacheFor,
43
+ cacheTags,
42
44
  }}
43
45
  {...$$restProps}
44
46
  {...elProps}
@@ -59,6 +61,8 @@ $: elProps = {
59
61
  on:cancel
60
62
  on:success
61
63
  on:error
64
+ on:prefetching
65
+ on:prefetched
62
66
  >
63
67
  <slot />
64
68
  </svelte:element>
@@ -1,12 +1,9 @@
1
1
  import { SvelteComponent } from "svelte";
2
- import type { CacheForOption, FormDataConvertible, LinkPrefetchOption, Method, PreserveStateOption } from '@inertiajs/core';
2
+ import type { CacheForOption, FormDataConvertible, LinkPrefetchOption, Method, PreserveStateOption, UrlMethodPair } from '@inertiajs/core';
3
3
  declare const __propDef: {
4
4
  props: {
5
5
  [x: string]: any;
6
- href?: string | {
7
- url: string;
8
- method: Method;
9
- } | undefined;
6
+ href?: (string | UrlMethodPair) | undefined;
10
7
  as?: keyof HTMLElementTagNameMap | undefined;
11
8
  data?: Record<string, FormDataConvertible> | undefined;
12
9
  method?: Method | undefined;
@@ -20,6 +17,7 @@ declare const __propDef: {
20
17
  async?: boolean | undefined;
21
18
  prefetch?: (boolean | LinkPrefetchOption | LinkPrefetchOption[]) | undefined;
22
19
  cacheFor?: (CacheForOption | CacheForOption[]) | undefined;
20
+ cacheTags?: string | string[] | undefined;
23
21
  };
24
22
  events: {
25
23
  focus: FocusEvent;
@@ -39,6 +37,8 @@ declare const __propDef: {
39
37
  cancel: Event;
40
38
  success: Event | ProgressEvent<EventTarget> | SubmitEvent | ErrorEvent | UIEvent | AnimationEvent | MouseEvent | InputEvent | FocusEvent | CompositionEvent | ClipboardEvent | DragEvent | FormDataEvent | PointerEvent | KeyboardEvent | SecurityPolicyViolationEvent | TouchEvent | TransitionEvent | WheelEvent;
41
39
  error: ErrorEvent;
40
+ prefetching: Event | ProgressEvent<EventTarget> | SubmitEvent | ErrorEvent | UIEvent | AnimationEvent | MouseEvent | InputEvent | FocusEvent | CompositionEvent | ClipboardEvent | DragEvent | FormDataEvent | PointerEvent | KeyboardEvent | SecurityPolicyViolationEvent | TouchEvent | TransitionEvent | WheelEvent;
41
+ prefetched: Event | ProgressEvent<EventTarget> | SubmitEvent | ErrorEvent | UIEvent | AnimationEvent | MouseEvent | InputEvent | FocusEvent | CompositionEvent | ClipboardEvent | DragEvent | FormDataEvent | PointerEvent | KeyboardEvent | SecurityPolicyViolationEvent | TouchEvent | TransitionEvent | WheelEvent;
42
42
  } & {
43
43
  [evt: string]: CustomEvent<any>;
44
44
  };
@@ -1,6 +1,6 @@
1
1
  import { router, setupProgress } from '@inertiajs/core';
2
2
  import escape from 'html-escape';
3
- import App from './components/App.svelte';
3
+ import App, {} from './components/App.svelte';
4
4
  export default async function createInertiaApp({ id = 'app', resolve, setup, progress = {}, page, }) {
5
5
  const isServer = typeof window === 'undefined';
6
6
  const el = isServer ? null : document.getElementById(id);
package/dist/index.js CHANGED
@@ -6,6 +6,7 @@ export { default as WhenVisible } from './components/WhenVisible.svelte';
6
6
  export { default as createInertiaApp } from './createInertiaApp';
7
7
  export { default as inertia } from './link';
8
8
  export { default as page } from './page';
9
+ export {} from './types';
9
10
  export { default as useForm } from './useForm';
10
11
  export { default as usePoll } from './usePoll';
11
12
  export { default as usePrefetch } from './usePrefetch';
package/dist/link.d.ts CHANGED
@@ -1,19 +1,11 @@
1
- import { type CacheForOption, type FormDataConvertible, type GlobalEventsMap, type LinkPrefetchOption, type Method, type VisitOptions } from '@inertiajs/core';
1
+ import { type GlobalEventsMap, type LinkComponentBaseProps, type VisitOptions } from '@inertiajs/core';
2
2
  import type { CancelTokenSource } from 'axios';
3
3
  import type { ActionReturn } from 'svelte/action';
4
4
  interface ActionElement extends HTMLElement {
5
5
  href?: string;
6
6
  }
7
- type ActionParameters = Omit<VisitOptions, 'data' | 'prefetch'> & {
8
- href?: string | {
9
- url: string;
10
- method: Method;
11
- };
12
- data?: Record<string, FormDataConvertible>;
13
- prefetch?: boolean | LinkPrefetchOption | LinkPrefetchOption[];
14
- cacheFor?: CacheForOption | CacheForOption[];
15
- };
16
- type SelectedEventKeys = 'start' | 'progress' | 'finish' | 'before' | 'cancel' | 'success' | 'error';
7
+ type ActionParameters = Omit<LinkComponentBaseProps, 'onCancelToken'> & Omit<VisitOptions, keyof LinkComponentBaseProps>;
8
+ type SelectedEventKeys = 'start' | 'progress' | 'finish' | 'before' | 'cancel' | 'success' | 'error' | 'prefetching' | 'prefetched';
17
9
  type SelectedGlobalEventsMap = Pick<GlobalEventsMap, SelectedEventKeys>;
18
10
  type ActionAttributes = {
19
11
  [K in keyof SelectedGlobalEventsMap as `on:${K}` | `on${K}`]?: (event: CustomEvent<SelectedGlobalEventsMap[K]['details']>) => void;
package/dist/link.js CHANGED
@@ -4,6 +4,7 @@ function link(node, initialParams = {}) {
4
4
  let hoverTimeout;
5
5
  let prefetchModes = [];
6
6
  let cacheForValue;
7
+ let cacheTags = [];
7
8
  let method;
8
9
  let href;
9
10
  let data;
@@ -39,7 +40,7 @@ function link(node, initialParams = {}) {
39
40
  }
40
41
  },
41
42
  };
42
- function update({ cacheFor = 0, prefetch = false, ...params }) {
43
+ function update({ cacheFor = 0, prefetch = false, cacheTags: cacheTagValues = [], ...params }) {
43
44
  prefetchModes = (() => {
44
45
  if (prefetch === true) {
45
46
  return ['hover'];
@@ -58,6 +59,7 @@ function link(node, initialParams = {}) {
58
59
  }
59
60
  return 30_000;
60
61
  })();
62
+ cacheTags = cacheTagValues;
61
63
  method = typeof params.href === 'object' ? params.href.method : (params.method?.toLowerCase() || 'get');
62
64
  [href, data] = hrefAndData(method, params);
63
65
  if (node.tagName === 'A') {
@@ -92,6 +94,8 @@ function link(node, initialParams = {}) {
92
94
  onSuccess: (page) => dispatchEvent('success', { detail: { page } }),
93
95
  onError: (errors) => dispatchEvent('error', { detail: { errors } }),
94
96
  onCancelToken: (token) => dispatchEvent('cancel-token', { detail: { token } }),
97
+ onPrefetching: (visit) => dispatchEvent('prefetching', { detail: { visit } }),
98
+ onPrefetched: (response, visit) => dispatchEvent('prefetched', { detail: { response, visit } }),
95
99
  };
96
100
  updateEventListeners();
97
101
  }
@@ -102,7 +106,11 @@ function link(node, initialParams = {}) {
102
106
  return mergeDataIntoQueryString(method, typeof params.href === 'object' ? params.href.url : node.href || params.href || '', params.data || {}, params.queryStringArrayFormat || 'brackets');
103
107
  }
104
108
  function prefetch() {
105
- router.prefetch(href, baseParams, { cacheFor: cacheForValue });
109
+ router.prefetch(href, {
110
+ ...baseParams,
111
+ onPrefetching: (visit) => dispatchEvent('prefetching', { detail: { visit } }),
112
+ onPrefetched: (response, visit) => dispatchEvent('prefetched', { detail: { response, visit } }),
113
+ }, { cacheFor: cacheForValue, cacheTags });
106
114
  }
107
115
  function updateNodeAttributes() {
108
116
  if (inFlightCount > 0) {
package/dist/page.js CHANGED
@@ -1,3 +1,4 @@
1
+ import {} from '@inertiajs/core';
1
2
  import { writable } from 'svelte/store';
2
3
  const { set, subscribe } = writable();
3
4
  export const setPage = set;
package/dist/useForm.d.ts CHANGED
@@ -1,31 +1,28 @@
1
- import type { Errors, FormDataConvertible, FormDataKeys, Method, Progress, VisitOptions } from '@inertiajs/core';
1
+ import type { ErrorValue, FormDataErrors, FormDataKeys, FormDataType, FormDataValues, Method, Progress, UrlMethodPair, VisitOptions } from '@inertiajs/core';
2
2
  import { type Writable } from 'svelte/store';
3
- type FormDataType = Record<string, FormDataConvertible>;
3
+ type InertiaFormStore<TForm extends object> = Writable<InertiaForm<TForm>> & InertiaForm<TForm>;
4
4
  type FormOptions = Omit<VisitOptions, 'data'>;
5
- export interface InertiaFormProps<TForm extends FormDataType> {
5
+ export interface InertiaFormProps<TForm extends object> {
6
6
  isDirty: boolean;
7
- errors: Partial<Record<FormDataKeys<TForm>, string>>;
7
+ errors: FormDataErrors<TForm>;
8
8
  hasErrors: boolean;
9
9
  progress: Progress | null;
10
10
  wasSuccessful: boolean;
11
11
  recentlySuccessful: boolean;
12
12
  processing: boolean;
13
13
  setStore(data: TForm): void;
14
- setStore(key: FormDataKeys<TForm>, value?: FormDataConvertible): void;
14
+ setStore<T extends FormDataKeys<TForm>>(key: T, value: FormDataValues<TForm, T>): void;
15
15
  data(): TForm;
16
16
  transform(callback: (data: TForm) => object): this;
17
17
  defaults(): this;
18
18
  defaults(fields: Partial<TForm>): this;
19
- defaults(field?: FormDataKeys<TForm>, value?: FormDataConvertible): this;
20
- reset(...fields: FormDataKeys<TForm>[]): this;
21
- clearErrors(...fields: FormDataKeys<TForm>[]): this;
22
- resetAndClearErrors(...fields: FormDataKeys<TForm>[]): this;
23
- setError(field: FormDataKeys<TForm>, value: string): this;
24
- setError(errors: Errors): this;
25
- submit: (...args: [Method, string, FormOptions?] | [{
26
- url: string;
27
- method: Method;
28
- }, FormOptions?]) => void;
19
+ defaults<T extends FormDataKeys<TForm>>(field: T, value: FormDataValues<TForm, T>): this;
20
+ reset<K extends FormDataKeys<TForm>>(...fields: K[]): this;
21
+ clearErrors<K extends FormDataKeys<TForm>>(...fields: K[]): this;
22
+ resetAndClearErrors<K extends FormDataKeys<TForm>>(...fields: K[]): this;
23
+ setError<K extends FormDataKeys<TForm>>(field: K, value: ErrorValue): this;
24
+ setError(errors: FormDataErrors<TForm>): this;
25
+ submit: (...args: [Method, string, FormOptions?] | [UrlMethodPair, FormOptions?]) => void;
29
26
  get(url: string, options?: FormOptions): void;
30
27
  post(url: string, options?: FormOptions): void;
31
28
  put(url: string, options?: FormOptions): void;
@@ -33,7 +30,7 @@ export interface InertiaFormProps<TForm extends FormDataType> {
33
30
  delete(url: string, options?: FormOptions): void;
34
31
  cancel(): void;
35
32
  }
36
- export type InertiaForm<TForm extends FormDataType> = InertiaFormProps<TForm> & TForm;
37
- export default function useForm<TForm extends FormDataType>(data: TForm | (() => TForm)): Writable<InertiaForm<TForm>>;
38
- export default function useForm<TForm extends FormDataType>(rememberKey: string, data: TForm | (() => TForm)): Writable<InertiaForm<TForm>>;
33
+ export type InertiaForm<TForm extends object> = InertiaFormProps<TForm> & TForm;
34
+ export default function useForm<TForm extends FormDataType<TForm>>(data: TForm | (() => TForm)): InertiaFormStore<TForm>;
35
+ export default function useForm<TForm extends FormDataType<TForm>>(rememberKey: string, data: TForm | (() => TForm)): InertiaFormStore<TForm>;
39
36
  export {};
package/dist/useForm.js CHANGED
@@ -16,7 +16,7 @@ export default function useForm(rememberKeyOrData, maybeData) {
16
16
  const store = writable({
17
17
  ...(restored ? restored.data : data),
18
18
  isDirty: false,
19
- errors: restored ? restored.errors : {},
19
+ errors: (restored ? restored.errors : {}),
20
20
  hasErrors: false,
21
21
  progress: null,
22
22
  wasSuccessful: false,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inertiajs/svelte",
3
- "version": "2.1.0",
3
+ "version": "2.1.2",
4
4
  "license": "MIT",
5
5
  "description": "The Svelte adapter for Inertia.js",
6
6
  "contributors": [
@@ -37,7 +37,7 @@
37
37
  "dependencies": {
38
38
  "es-toolkit": "^1.33.0",
39
39
  "html-escape": "^2.0.0",
40
- "@inertiajs/core": "2.1.0"
40
+ "@inertiajs/core": "2.1.2"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@sveltejs/adapter-auto": "^3.2.0",