@r2digisolutions/components 0.21.30 → 0.21.31
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.
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
>
|
|
5
5
|
import type { Snippet } from 'svelte';
|
|
6
6
|
import { setContext, untrack } from 'svelte';
|
|
7
|
-
import type {
|
|
7
|
+
import type { ClassValue } from 'svelte/elements';
|
|
8
|
+
import type { RemoteFormInput, RemoteQueryUpdate } from '@sveltejs/kit';
|
|
8
9
|
import Alert from '../Alert/Alert.svelte';
|
|
9
10
|
import {
|
|
10
11
|
FORM_CONTEXT_KEY,
|
|
@@ -35,6 +36,24 @@
|
|
|
35
36
|
* @default true
|
|
36
37
|
*/
|
|
37
38
|
syncRemoteIssues?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* When `false`, skip Kit's default `invalidateAll()` after a successful remote form
|
|
41
|
+
* (via `submit().updates()`). Prefer {@link updates} to refresh specific queries.
|
|
42
|
+
* @default true
|
|
43
|
+
*/
|
|
44
|
+
invalidateAll?: boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Single-flight query refreshes. Passing this (even `[]`) opts out of `invalidateAll`.
|
|
47
|
+
* @see https://svelte.dev/docs/kit/remote-functions#Single-flight-mutations
|
|
48
|
+
*/
|
|
49
|
+
updates?: RemoteQueryUpdate[];
|
|
50
|
+
onSuccess?: (result?: any) => void | Promise<void>;
|
|
51
|
+
onError?: (error: string) => void;
|
|
52
|
+
beforeSubmit?: () => void | Promise<void>;
|
|
53
|
+
/** Bind the underlying `<form>` (e.g. Dialog footer `formRef.requestSubmit()`). */
|
|
54
|
+
formRef?: HTMLFormElement | null;
|
|
55
|
+
id?: string;
|
|
56
|
+
enctype?: 'application/x-www-form-urlencoded' | 'multipart/form-data' | 'text/plain';
|
|
38
57
|
/** Optional bag for submit/remote result exposed in context */
|
|
39
58
|
result?: TOutput;
|
|
40
59
|
title?: string;
|
|
@@ -43,7 +62,7 @@
|
|
|
43
62
|
showErrorSummary?: boolean;
|
|
44
63
|
errorSummaryTitle?: string;
|
|
45
64
|
gap?: 'sm' | 'md' | 'lg';
|
|
46
|
-
class?:
|
|
65
|
+
class?: ClassValue;
|
|
47
66
|
children?: Snippet;
|
|
48
67
|
header?: Snippet;
|
|
49
68
|
footer?: Snippet;
|
|
@@ -54,10 +73,18 @@
|
|
|
54
73
|
let {
|
|
55
74
|
data = $bindable<TData>({} as TData),
|
|
56
75
|
errors = $bindable<FormErrors>({}),
|
|
57
|
-
loading = false,
|
|
76
|
+
loading = $bindable(false),
|
|
58
77
|
disabled = false,
|
|
59
78
|
remote = null,
|
|
60
79
|
syncRemoteIssues = true,
|
|
80
|
+
invalidateAll = true,
|
|
81
|
+
updates,
|
|
82
|
+
onSuccess,
|
|
83
|
+
onError,
|
|
84
|
+
beforeSubmit,
|
|
85
|
+
formRef = $bindable<HTMLFormElement | null>(null),
|
|
86
|
+
id,
|
|
87
|
+
enctype,
|
|
61
88
|
result = $bindable<TOutput | undefined>(undefined),
|
|
62
89
|
title,
|
|
63
90
|
description,
|
|
@@ -93,6 +120,9 @@
|
|
|
93
120
|
lg: 'space-y-6'
|
|
94
121
|
} as const;
|
|
95
122
|
|
|
123
|
+
/** Client-side ceiling so a hung remote cannot leave Save spinning forever. */
|
|
124
|
+
const SUBMIT_TIMEOUT_MS = 90_000;
|
|
125
|
+
|
|
96
126
|
function setError(name: string, message: string) {
|
|
97
127
|
errors = { ...errors, [name]: message };
|
|
98
128
|
}
|
|
@@ -153,10 +183,12 @@
|
|
|
153
183
|
getData
|
|
154
184
|
} satisfies FormContext<TData, TOutput>);
|
|
155
185
|
|
|
156
|
-
|
|
186
|
+
$effect(() => {
|
|
187
|
+
const form = kitForm;
|
|
188
|
+
if (!form || didInitRemoteFormId) return;
|
|
157
189
|
didInitRemoteFormId = true;
|
|
158
|
-
remoteFormId = untrack(() => getRemoteFormId(
|
|
159
|
-
}
|
|
190
|
+
remoteFormId = untrack(() => getRemoteFormId(form.action));
|
|
191
|
+
});
|
|
160
192
|
|
|
161
193
|
function syncRemoteIssuesNow() {
|
|
162
194
|
if (!kitForm || !syncRemoteIssues) return;
|
|
@@ -173,6 +205,10 @@
|
|
|
173
205
|
|
|
174
206
|
let formEl = $state<HTMLFormElement | null>(null);
|
|
175
207
|
|
|
208
|
+
$effect(() => {
|
|
209
|
+
formRef = formEl;
|
|
210
|
+
});
|
|
211
|
+
|
|
176
212
|
$effect(() => {
|
|
177
213
|
const el = formEl;
|
|
178
214
|
if (!el || !isRemote) return;
|
|
@@ -192,6 +228,130 @@
|
|
|
192
228
|
el.addEventListener('submit', onSubmit);
|
|
193
229
|
return () => el.removeEventListener('submit', onSubmit);
|
|
194
230
|
});
|
|
231
|
+
|
|
232
|
+
function isKitRedirect(err: unknown): boolean {
|
|
233
|
+
return Boolean(err && typeof err === 'object' && 'location' in err && 'status' in err);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function formatValidationIssues(): string {
|
|
237
|
+
if (!kitForm) return '';
|
|
238
|
+
const issues = kitForm.fields.allIssues?.();
|
|
239
|
+
if (!issues?.length) return '';
|
|
240
|
+
return issues
|
|
241
|
+
.map((issue) => {
|
|
242
|
+
const path = issue.path?.length ? issue.path.join('.') : 'formulario';
|
|
243
|
+
return `${path}: ${issue.message}`;
|
|
244
|
+
})
|
|
245
|
+
.join(' · ');
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function withTimeout<T>(promise: Promise<T>, ms: number, message: string): Promise<T> {
|
|
249
|
+
return new Promise<T>((resolve, reject) => {
|
|
250
|
+
const timer = setTimeout(() => reject(new Error(message)), ms);
|
|
251
|
+
promise.then(
|
|
252
|
+
(value) => {
|
|
253
|
+
clearTimeout(timer);
|
|
254
|
+
resolve(value);
|
|
255
|
+
},
|
|
256
|
+
(err) => {
|
|
257
|
+
clearTimeout(timer);
|
|
258
|
+
reject(err);
|
|
259
|
+
}
|
|
260
|
+
);
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** Kit: `submit().updates(...)` (even empty) prevents the default invalidateAll waterfall. */
|
|
265
|
+
function runSubmit(
|
|
266
|
+
submit: () => Promise<boolean> & {
|
|
267
|
+
updates: (...args: RemoteQueryUpdate[]) => Promise<boolean>;
|
|
268
|
+
}
|
|
269
|
+
) {
|
|
270
|
+
if (updates) return submit().updates(...updates);
|
|
271
|
+
if (!invalidateAll) return submit().updates();
|
|
272
|
+
return submit();
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function reportError(message: string) {
|
|
276
|
+
submitted = true;
|
|
277
|
+
setError('_form', message);
|
|
278
|
+
onError?.(message);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
async function handleRemoteEnhance(submit: () => Promise<boolean> & {
|
|
282
|
+
updates: (...args: RemoteQueryUpdate[]) => Promise<boolean>;
|
|
283
|
+
}) {
|
|
284
|
+
if (!kitForm) return;
|
|
285
|
+
try {
|
|
286
|
+
loading = true;
|
|
287
|
+
clearErrors();
|
|
288
|
+
await beforeSubmit?.();
|
|
289
|
+
const ok = await withTimeout(
|
|
290
|
+
runSubmit(submit),
|
|
291
|
+
SUBMIT_TIMEOUT_MS,
|
|
292
|
+
'La operación ha tardado demasiado. Comprueba la conexión e inténtalo de nuevo.'
|
|
293
|
+
);
|
|
294
|
+
const results = kitForm.result;
|
|
295
|
+
result = results as TOutput | undefined;
|
|
296
|
+
if (!ok) {
|
|
297
|
+
syncRemoteIssuesNow();
|
|
298
|
+
const issueSummary = formatValidationIssues();
|
|
299
|
+
const message = issueSummary || 'Revisa los errores del formulario.';
|
|
300
|
+
submitted = true;
|
|
301
|
+
// Field issues already land in `errors`; only use `_form` when none were mapped.
|
|
302
|
+
if (!issueSummary) setError('_form', message);
|
|
303
|
+
onError?.(message);
|
|
304
|
+
} else if (results && typeof results === 'object' && 'error' in results && results.error) {
|
|
305
|
+
const err = results.error;
|
|
306
|
+
const message =
|
|
307
|
+
typeof err === 'string'
|
|
308
|
+
? err
|
|
309
|
+
: err instanceof Error
|
|
310
|
+
? err.message
|
|
311
|
+
: typeof err === 'object' && err !== null && 'message' in err
|
|
312
|
+
? String((err as { message?: unknown }).message)
|
|
313
|
+
: 'Ha ocurrido un error al guardar.';
|
|
314
|
+
reportError(message);
|
|
315
|
+
} else {
|
|
316
|
+
// Clear busy UI before onSuccess — invalidateAll/refresh must not leave Save spinning.
|
|
317
|
+
loading = false;
|
|
318
|
+
try {
|
|
319
|
+
await onSuccess?.(results);
|
|
320
|
+
} catch (successErr) {
|
|
321
|
+
console.error('[Form] onSuccess failed', successErr);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
} catch (e) {
|
|
325
|
+
if (isKitRedirect(e)) throw e;
|
|
326
|
+
const raw = e instanceof Error ? e.message : '';
|
|
327
|
+
let message: string;
|
|
328
|
+
if (/413|Payload Too Large|BODY_SIZE_LIMIT|exceeds limit|Content-length/i.test(raw)) {
|
|
329
|
+
message = 'El archivo es demasiado grande. Prueba con una imagen de menos de 8 MB.';
|
|
330
|
+
} else if (/502|504|Bad Gateway|Gateway Timeout|Failed to fetch|NetworkError/i.test(raw)) {
|
|
331
|
+
message =
|
|
332
|
+
'No se pudo completar la subida (error de red o servidor). Prueba de nuevo en unos segundos.';
|
|
333
|
+
} else if (!raw || /unexpected error/i.test(raw)) {
|
|
334
|
+
message = 'Ha ocurrido un error inesperado. Inténtalo de nuevo.';
|
|
335
|
+
} else {
|
|
336
|
+
message = raw;
|
|
337
|
+
}
|
|
338
|
+
reportError(message);
|
|
339
|
+
} finally {
|
|
340
|
+
loading = false;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Full Kit `RemoteForm` → own `.enhance` (invalidateAll / updates / callbacks).
|
|
346
|
+
* Already-enhanced spread → caller owns enhance; pass through unchanged.
|
|
347
|
+
*/
|
|
348
|
+
function remoteFormAttrs(): object {
|
|
349
|
+
if (!remote) return {};
|
|
350
|
+
if (!kitForm) return remote as object;
|
|
351
|
+
return kitForm.enhance(async ({ submit }) => {
|
|
352
|
+
await handleRemoteEnhance(submit);
|
|
353
|
+
}) as object;
|
|
354
|
+
}
|
|
195
355
|
</script>
|
|
196
356
|
|
|
197
357
|
{#snippet fields()}
|
|
@@ -236,7 +396,9 @@
|
|
|
236
396
|
{#if isRemote}
|
|
237
397
|
<form
|
|
238
398
|
bind:this={formEl}
|
|
239
|
-
{...(
|
|
399
|
+
{...remoteFormAttrs()}
|
|
400
|
+
{id}
|
|
401
|
+
{enctype}
|
|
240
402
|
class={['w-full', gaps[gap], className]}
|
|
241
403
|
novalidate
|
|
242
404
|
aria-busy={busy || undefined}
|
|
@@ -245,6 +407,9 @@
|
|
|
245
407
|
</form>
|
|
246
408
|
{:else}
|
|
247
409
|
<form
|
|
410
|
+
bind:this={formEl}
|
|
411
|
+
{id}
|
|
412
|
+
{enctype}
|
|
248
413
|
class={['w-full', gaps[gap], className]}
|
|
249
414
|
onsubmit={handleSubmit}
|
|
250
415
|
novalidate
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Snippet } from 'svelte';
|
|
2
|
-
import type {
|
|
2
|
+
import type { ClassValue } from 'svelte/elements';
|
|
3
|
+
import type { RemoteFormInput, RemoteQueryUpdate } from '@sveltejs/kit';
|
|
3
4
|
import { type FormDataValues, type FormErrors, type FormRemote } from '../../../utils/formContext.js';
|
|
4
5
|
declare function $$render<TData extends FormDataValues = FormDataValues, TInput extends RemoteFormInput | void = RemoteFormInput, TOutput = unknown>(): {
|
|
5
6
|
props: {
|
|
@@ -19,6 +20,24 @@ declare function $$render<TData extends FormDataValues = FormDataValues, TInput
|
|
|
19
20
|
* @default true
|
|
20
21
|
*/
|
|
21
22
|
syncRemoteIssues?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* When `false`, skip Kit's default `invalidateAll()` after a successful remote form
|
|
25
|
+
* (via `submit().updates()`). Prefer {@link updates} to refresh specific queries.
|
|
26
|
+
* @default true
|
|
27
|
+
*/
|
|
28
|
+
invalidateAll?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Single-flight query refreshes. Passing this (even `[]`) opts out of `invalidateAll`.
|
|
31
|
+
* @see https://svelte.dev/docs/kit/remote-functions#Single-flight-mutations
|
|
32
|
+
*/
|
|
33
|
+
updates?: RemoteQueryUpdate[];
|
|
34
|
+
onSuccess?: (result?: any) => void | Promise<void>;
|
|
35
|
+
onError?: (error: string) => void;
|
|
36
|
+
beforeSubmit?: () => void | Promise<void>;
|
|
37
|
+
/** Bind the underlying `<form>` (e.g. Dialog footer `formRef.requestSubmit()`). */
|
|
38
|
+
formRef?: HTMLFormElement | null;
|
|
39
|
+
id?: string;
|
|
40
|
+
enctype?: "application/x-www-form-urlencoded" | "multipart/form-data" | "text/plain";
|
|
22
41
|
/** Optional bag for submit/remote result exposed in context */
|
|
23
42
|
result?: TOutput;
|
|
24
43
|
title?: string;
|
|
@@ -27,7 +46,7 @@ declare function $$render<TData extends FormDataValues = FormDataValues, TInput
|
|
|
27
46
|
showErrorSummary?: boolean;
|
|
28
47
|
errorSummaryTitle?: string;
|
|
29
48
|
gap?: "sm" | "md" | "lg";
|
|
30
|
-
class?:
|
|
49
|
+
class?: ClassValue;
|
|
31
50
|
children?: Snippet;
|
|
32
51
|
header?: Snippet;
|
|
33
52
|
footer?: Snippet;
|
|
@@ -35,7 +54,7 @@ declare function $$render<TData extends FormDataValues = FormDataValues, TInput
|
|
|
35
54
|
onsubmit?: (e: SubmitEvent) => void;
|
|
36
55
|
};
|
|
37
56
|
exports: {};
|
|
38
|
-
bindings: "data" | "errors" | "result";
|
|
57
|
+
bindings: "loading" | "data" | "errors" | "formRef" | "result";
|
|
39
58
|
slots: {};
|
|
40
59
|
events: {};
|
|
41
60
|
};
|
|
@@ -43,7 +62,7 @@ declare class __sveltets_Render<TData extends FormDataValues = FormDataValues, T
|
|
|
43
62
|
props(): ReturnType<typeof $$render<TData, TInput, TOutput>>['props'];
|
|
44
63
|
events(): ReturnType<typeof $$render<TData, TInput, TOutput>>['events'];
|
|
45
64
|
slots(): ReturnType<typeof $$render<TData, TInput, TOutput>>['slots'];
|
|
46
|
-
bindings(): "data" | "errors" | "result";
|
|
65
|
+
bindings(): "loading" | "data" | "errors" | "formRef" | "result";
|
|
47
66
|
exports(): {};
|
|
48
67
|
}
|
|
49
68
|
interface $$IsomorphicComponent {
|