@inertiajs/svelte 2.3.17 → 3.0.0-beta.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.
Files changed (47) hide show
  1. package/dist/components/App.svelte +120 -44
  2. package/dist/components/App.svelte.d.ts +10 -19
  3. package/dist/components/Deferred.svelte +68 -9
  4. package/dist/components/Deferred.svelte.d.ts +9 -20
  5. package/dist/components/Form.svelte +358 -242
  6. package/dist/components/Form.svelte.d.ts +51 -96
  7. package/dist/components/InfiniteScroll.svelte +227 -167
  8. package/dist/components/InfiniteScroll.svelte.d.ts +29 -116
  9. package/dist/components/Link.svelte +96 -48
  10. package/dist/components/Link.svelte.d.ts +49 -56
  11. package/dist/components/Render.svelte +62 -21
  12. package/dist/components/Render.svelte.d.ts +9 -25
  13. package/dist/components/WhenVisible.svelte +83 -66
  14. package/dist/components/WhenVisible.svelte.d.ts +11 -26
  15. package/dist/components/createForm.d.ts +8 -0
  16. package/dist/components/createForm.js +4 -0
  17. package/dist/components/formContext.d.ts +3 -3
  18. package/dist/components/formContext.js +9 -3
  19. package/dist/createInertiaApp.d.ts +12 -7
  20. package/dist/createInertiaApp.js +63 -24
  21. package/dist/index.d.ts +8 -5
  22. package/dist/index.js +8 -5
  23. package/dist/layoutProps.svelte.d.ts +6 -0
  24. package/dist/layoutProps.svelte.js +25 -0
  25. package/dist/link.js +13 -2
  26. package/dist/page.svelte.d.ts +10 -0
  27. package/dist/page.svelte.js +14 -0
  28. package/dist/types.d.ts +11 -4
  29. package/dist/types.js +1 -1
  30. package/dist/{useForm.d.ts → useForm.svelte.d.ts} +4 -4
  31. package/dist/useForm.svelte.js +116 -0
  32. package/dist/useFormState.svelte.d.ts +84 -0
  33. package/dist/useFormState.svelte.js +290 -0
  34. package/dist/useHttp.svelte.d.ts +61 -0
  35. package/dist/useHttp.svelte.js +154 -0
  36. package/dist/usePrefetch.svelte.d.ts +7 -0
  37. package/dist/{usePrefetch.js → usePrefetch.svelte.js} +18 -13
  38. package/dist/useRemember.svelte.d.ts +1 -0
  39. package/dist/useRemember.svelte.js +10 -0
  40. package/package.json +11 -11
  41. package/resources/boost/skills/inertia-svelte-development/SKILL.blade.php +241 -107
  42. package/dist/page.d.ts +0 -13
  43. package/dist/page.js +0 -8
  44. package/dist/useForm.js +0 -356
  45. package/dist/usePrefetch.d.ts +0 -7
  46. package/dist/useRemember.d.ts +0 -1
  47. package/dist/useRemember.js +0 -11
package/dist/useForm.js DELETED
@@ -1,356 +0,0 @@
1
- import { router, UseFormUtils } from '@inertiajs/core';
2
- import { createValidator, resolveName, toSimpleValidationErrors } from 'laravel-precognition';
3
- import { cloneDeep, get, has, isEqual, set } from 'lodash-es';
4
- import { get as getStore, writable } from 'svelte/store';
5
- import { config } from '.';
6
- let reservedFormKeys = null;
7
- let bootstrapping = false;
8
- function validateFormDataKeys(data) {
9
- if (bootstrapping) {
10
- return;
11
- }
12
- if (reservedFormKeys === null) {
13
- bootstrapping = true;
14
- const store = useForm({});
15
- reservedFormKeys = new Set(Object.keys(getStore(store)));
16
- bootstrapping = false;
17
- }
18
- const conflicts = Object.keys(data).filter((key) => reservedFormKeys.has(key));
19
- if (conflicts.length > 0) {
20
- console.error(`[Inertia] useForm() data contains field(s) that conflict with form properties: ${conflicts.map((k) => `"${k}"`).join(', ')}. ` +
21
- `These fields will be overwritten by form methods/properties. Please rename these fields.`);
22
- }
23
- }
24
- export default function useForm(...args) {
25
- const parsedArgs = UseFormUtils.parseUseFormArguments(...args);
26
- const { rememberKey, data: initialData } = parsedArgs;
27
- let precognitionEndpoint = parsedArgs.precognitionEndpoint;
28
- const data = typeof initialData === 'function' ? initialData() : initialData;
29
- const restored = rememberKey
30
- ? router.restore(rememberKey)
31
- : null;
32
- let defaults = cloneDeep(data);
33
- validateFormDataKeys(defaults);
34
- let cancelToken = null;
35
- let recentlySuccessfulTimeoutId = null;
36
- let transform = (data) => data;
37
- let rememberExcludeKeys = [];
38
- let defaultsCalledInOnSuccess = false;
39
- let validatorRef = null;
40
- let setFormState;
41
- const withPrecognition = (...args) => {
42
- precognitionEndpoint = UseFormUtils.createWayfinderCallback(...args);
43
- const formWithPrecognition = () => getStore(store);
44
- let withAllErrors = null;
45
- if (!validatorRef) {
46
- const validator = createValidator((client) => {
47
- const { method, url } = precognitionEndpoint();
48
- const form = formWithPrecognition();
49
- const transformedData = cloneDeep(transform(form.data()));
50
- return client[method](url, transformedData);
51
- }, cloneDeep(defaults));
52
- validatorRef = validator;
53
- validator
54
- .on('validatingChanged', () => {
55
- setFormState('validating', validator.validating());
56
- })
57
- .on('validatedChanged', () => {
58
- setFormState('__valid', validator.valid());
59
- })
60
- .on('touchedChanged', () => {
61
- setFormState('__touched', validator.touched());
62
- })
63
- .on('errorsChanged', () => {
64
- const validationErrors = (withAllErrors ?? config.get('form.withAllErrors'))
65
- ? validator.errors()
66
- : toSimpleValidationErrors(validator.errors());
67
- setFormState('errors', {});
68
- formWithPrecognition().setError(validationErrors);
69
- setFormState('__valid', validator.valid());
70
- });
71
- }
72
- const tap = (value, callback) => {
73
- callback(value);
74
- return value;
75
- };
76
- if (validatorRef) {
77
- Object.assign(store, {});
78
- }
79
- store.update((form) => {
80
- return Object.assign(store, {
81
- ...form,
82
- __touched: [],
83
- __valid: [],
84
- validating: false,
85
- validator: () => validatorRef,
86
- validate: (field, config) => {
87
- const form = formWithPrecognition();
88
- if (typeof field === 'object' && !('target' in field)) {
89
- config = field;
90
- field = undefined;
91
- }
92
- if (field === undefined) {
93
- validatorRef.validate(config);
94
- }
95
- else {
96
- field = resolveName(field);
97
- const transformedData = transform(form.data());
98
- validatorRef.validate(field, get(transformedData, field), config);
99
- }
100
- return form;
101
- },
102
- touch: (field, ...fields) => {
103
- const form = formWithPrecognition();
104
- if (Array.isArray(field)) {
105
- validatorRef?.touch(field);
106
- }
107
- else if (typeof field === 'string') {
108
- validatorRef?.touch([field, ...fields]);
109
- }
110
- else {
111
- validatorRef?.touch(field);
112
- }
113
- return form;
114
- },
115
- validateFiles: () => tap(formWithPrecognition(), () => validatorRef?.validateFiles()),
116
- setValidationTimeout: (duration) => tap(formWithPrecognition(), () => validatorRef.setTimeout(duration)),
117
- withAllErrors: () => tap(formWithPrecognition(), () => (withAllErrors = true)),
118
- withoutFileValidation: () => tap(formWithPrecognition(), () => validatorRef?.withoutFileValidation()),
119
- valid: (field) => formWithPrecognition().__valid.includes(field),
120
- invalid: (field) => field in formWithPrecognition().errors,
121
- touched: (field) => {
122
- const touched = formWithPrecognition().__touched;
123
- return typeof field === 'string' ? touched.includes(field) : touched.length > 0;
124
- },
125
- setErrors: (errors) => tap(formWithPrecognition(), () => {
126
- const form = formWithPrecognition();
127
- form.setError(errors);
128
- }),
129
- forgetError: (field) => tap(formWithPrecognition(), () => {
130
- const form = formWithPrecognition();
131
- form.clearErrors(resolveName(field));
132
- }),
133
- });
134
- });
135
- return store;
136
- };
137
- const store = writable({
138
- ...(restored ? restored.data : data),
139
- isDirty: false,
140
- errors: (restored ? restored.errors : {}),
141
- hasErrors: false,
142
- progress: null,
143
- wasSuccessful: false,
144
- recentlySuccessful: false,
145
- processing: false,
146
- setStore(keyOrData, maybeValue = undefined) {
147
- store.update((store) => {
148
- return typeof keyOrData === 'string' ? set(store, keyOrData, maybeValue) : Object.assign(store, keyOrData);
149
- });
150
- },
151
- data() {
152
- return Object.keys(data).reduce((carry, key) => {
153
- return set(carry, key, get(this, key));
154
- }, {});
155
- },
156
- transform(callback) {
157
- transform = callback;
158
- return this;
159
- },
160
- defaults(fieldOrFields, maybeValue) {
161
- defaultsCalledInOnSuccess = true;
162
- if (typeof fieldOrFields === 'undefined') {
163
- defaults = cloneDeep(this.data());
164
- }
165
- else {
166
- defaults =
167
- typeof fieldOrFields === 'string'
168
- ? set(cloneDeep(defaults), fieldOrFields, maybeValue)
169
- : Object.assign(cloneDeep(defaults), fieldOrFields);
170
- }
171
- validatorRef?.defaults(defaults);
172
- return this;
173
- },
174
- reset(...fields) {
175
- const clonedData = cloneDeep(defaults);
176
- if (fields.length === 0) {
177
- this.setStore(clonedData);
178
- }
179
- else {
180
- this.setStore(fields
181
- .filter((key) => has(clonedData, key))
182
- .reduce((carry, key) => {
183
- return set(carry, key, get(clonedData, key));
184
- }, {}));
185
- }
186
- validatorRef?.reset(...fields);
187
- return this;
188
- },
189
- setError(fieldOrFields, maybeValue) {
190
- const errors = typeof fieldOrFields === 'string' ? { [fieldOrFields]: maybeValue } : fieldOrFields;
191
- setFormState('errors', {
192
- ...this.errors,
193
- ...errors,
194
- });
195
- validatorRef?.setErrors(errors);
196
- return this;
197
- },
198
- clearErrors(...fields) {
199
- setFormState('errors', Object.keys(this.errors).reduce((carry, field) => ({
200
- ...carry,
201
- ...(fields.length > 0 && !fields.includes(field) ? { [field]: this.errors[field] } : {}),
202
- }), {}));
203
- if (validatorRef) {
204
- if (fields.length === 0) {
205
- validatorRef.setErrors({});
206
- }
207
- else {
208
- fields.forEach(validatorRef.forgetError);
209
- }
210
- }
211
- return this;
212
- },
213
- resetAndClearErrors(...fields) {
214
- this.reset(...fields);
215
- this.clearErrors(...fields);
216
- return this;
217
- },
218
- submit(...args) {
219
- const { method, url, options } = UseFormUtils.parseSubmitArguments(args, precognitionEndpoint);
220
- defaultsCalledInOnSuccess = false;
221
- const data = transform(this.data());
222
- const _options = {
223
- ...options,
224
- onCancelToken: (token) => {
225
- cancelToken = token;
226
- if (options.onCancelToken) {
227
- return options.onCancelToken(token);
228
- }
229
- },
230
- onBefore: (visit) => {
231
- setFormState('wasSuccessful', false);
232
- setFormState('recentlySuccessful', false);
233
- if (recentlySuccessfulTimeoutId) {
234
- clearTimeout(recentlySuccessfulTimeoutId);
235
- }
236
- if (options.onBefore) {
237
- return options.onBefore(visit);
238
- }
239
- },
240
- onStart: (visit) => {
241
- setFormState('processing', true);
242
- if (options.onStart) {
243
- return options.onStart(visit);
244
- }
245
- },
246
- onProgress: (event) => {
247
- setFormState('progress', event || null);
248
- if (options.onProgress) {
249
- return options.onProgress(event);
250
- }
251
- },
252
- onSuccess: async (page) => {
253
- setFormState('processing', false);
254
- setFormState('progress', null);
255
- this.clearErrors();
256
- setFormState('wasSuccessful', true);
257
- setFormState('recentlySuccessful', true);
258
- recentlySuccessfulTimeoutId = setTimeout(() => setFormState('recentlySuccessful', false), config.get('form.recentlySuccessfulDuration'));
259
- const onSuccess = options.onSuccess ? await options.onSuccess(page) : null;
260
- if (!defaultsCalledInOnSuccess) {
261
- this.defaults(cloneDeep(getStore(store).data()));
262
- }
263
- return onSuccess;
264
- },
265
- onError: (errors) => {
266
- setFormState('processing', false);
267
- setFormState('progress', null);
268
- setFormState('errors', errors);
269
- validatorRef?.setErrors(errors);
270
- if (options.onError) {
271
- return options.onError(errors);
272
- }
273
- },
274
- onCancel: () => {
275
- setFormState('processing', false);
276
- setFormState('progress', null);
277
- if (options.onCancel) {
278
- return options.onCancel();
279
- }
280
- },
281
- onFinish: (visit) => {
282
- setFormState('processing', false);
283
- setFormState('progress', null);
284
- cancelToken = null;
285
- if (options.onFinish) {
286
- return options.onFinish(visit);
287
- }
288
- },
289
- };
290
- if (method === 'delete') {
291
- router.delete(url, { ..._options, data });
292
- }
293
- else {
294
- router[method](url, data, _options);
295
- }
296
- },
297
- get(url, options) {
298
- this.submit('get', url, options);
299
- },
300
- post(url, options) {
301
- this.submit('post', url, options);
302
- },
303
- put(url, options) {
304
- this.submit('put', url, options);
305
- },
306
- patch(url, options) {
307
- this.submit('patch', url, options);
308
- },
309
- delete(url, options) {
310
- this.submit('delete', url, options);
311
- },
312
- cancel() {
313
- cancelToken?.cancel();
314
- },
315
- __remember() {
316
- const data = this.data();
317
- if (rememberExcludeKeys.length > 0) {
318
- const filtered = { ...data };
319
- rememberExcludeKeys.forEach((k) => delete filtered[k]);
320
- return { data: filtered, errors: this.errors };
321
- }
322
- return { data, errors: this.errors };
323
- },
324
- withPrecognition,
325
- });
326
- const dontRememberMethod = (...keys) => {
327
- rememberExcludeKeys = keys;
328
- return store;
329
- };
330
- Object.assign(store, {
331
- withPrecognition,
332
- dontRemember: dontRememberMethod,
333
- });
334
- setFormState = (key, value) => {
335
- store.update((form) => ({ ...form, [key]: value }));
336
- };
337
- store.subscribe((form) => {
338
- if (form.isDirty === isEqual(form.data(), defaults)) {
339
- setFormState('isDirty', !form.isDirty);
340
- }
341
- const hasErrors = Object.keys(form.errors).length > 0;
342
- if (form.hasErrors !== hasErrors) {
343
- setFormState('hasErrors', !form.hasErrors);
344
- }
345
- if (rememberKey) {
346
- const storedData = router.restore(rememberKey);
347
- const newData = form.__remember();
348
- if (!isEqual(storedData, newData)) {
349
- router.remember(newData, rememberKey);
350
- }
351
- }
352
- });
353
- return precognitionEndpoint
354
- ? store.withPrecognition(precognitionEndpoint)
355
- : store;
356
- }
@@ -1,7 +0,0 @@
1
- import { type VisitOptions } from '@inertiajs/core';
2
- export default function usePrefetch(options?: VisitOptions): {
3
- isPrefetched: import("svelte/store").Readable<boolean>;
4
- isPrefetching: import("svelte/store").Readable<boolean>;
5
- lastUpdatedAt: import("svelte/store").Readable<number | null>;
6
- flush(): void;
7
- };
@@ -1 +0,0 @@
1
- export default function useRemember<State>(initialState: State, key?: string): import("svelte/store").Writable<State>;
@@ -1,11 +0,0 @@
1
- import { router } from '@inertiajs/core';
2
- import { cloneDeep } from 'lodash-es';
3
- import { onDestroy } from 'svelte';
4
- import { writable } from 'svelte/store';
5
- export default function useRemember(initialState, key) {
6
- const restored = router.restore(key);
7
- const store = writable(restored !== undefined ? cloneDeep(restored) : initialState);
8
- const unsubscribe = store.subscribe((state) => router.remember(cloneDeep(state), key));
9
- onDestroy(unsubscribe);
10
- return store;
11
- }