@functionalcms/svelte-components 5.24.1 → 5.24.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.
|
@@ -12,12 +12,8 @@
|
|
|
12
12
|
type HiddenField,
|
|
13
13
|
type DatePickerField,
|
|
14
14
|
SubmitResult,
|
|
15
|
-
|
|
16
15
|
type MarkdownEditorField,
|
|
17
|
-
|
|
18
16
|
type HtmlEditorField
|
|
19
|
-
|
|
20
|
-
|
|
21
17
|
} from './form.js';
|
|
22
18
|
import {
|
|
23
19
|
Button,
|
|
@@ -42,6 +38,7 @@
|
|
|
42
38
|
class?: string;
|
|
43
39
|
successMessage?: string;
|
|
44
40
|
fields?: Array<Field>;
|
|
41
|
+
errors?: Map<string, string>;
|
|
45
42
|
onMessageSubmitted?: (result: SubmitResult) => void;
|
|
46
43
|
buttons: Array<ButtonConfig>;
|
|
47
44
|
}
|
|
@@ -49,6 +46,7 @@
|
|
|
49
46
|
let {
|
|
50
47
|
action,
|
|
51
48
|
fields = [],
|
|
49
|
+
errors = new Map(),
|
|
52
50
|
class: css = '',
|
|
53
51
|
successMessage,
|
|
54
52
|
onMessageSubmitted,
|
|
@@ -63,8 +61,6 @@
|
|
|
63
61
|
|
|
64
62
|
let form: HTMLFormElement;
|
|
65
63
|
let showMessage = $state(false);
|
|
66
|
-
let isSendingMessage = $state(false);
|
|
67
|
-
let loading = $state(false);
|
|
68
64
|
|
|
69
65
|
const isValid = (event: Event) => {
|
|
70
66
|
debugger;
|
|
@@ -78,36 +74,16 @@
|
|
|
78
74
|
return allValid;
|
|
79
75
|
};
|
|
80
76
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
loading = true;
|
|
85
|
-
debugger;
|
|
86
|
-
try {
|
|
87
|
-
if (isValid(event) && !isSendingMessage) {
|
|
88
|
-
isSendingMessage = true;
|
|
89
|
-
const formData = new FormData(form);
|
|
90
|
-
const response = await fetch(action, {
|
|
91
|
-
method: 'POST',
|
|
92
|
-
body: formData
|
|
93
|
-
});
|
|
94
|
-
showMessage = response.status === 200;
|
|
95
|
-
onMessageSubmitted?.(showMessage ? SubmitResult.Success : SubmitResult.Error);
|
|
96
|
-
setTimeout(() => {
|
|
97
|
-
showMessage = false;
|
|
98
|
-
}, 3000);
|
|
99
|
-
}else {
|
|
100
|
-
console.warn('Form is invalid or already sending message');
|
|
101
|
-
}
|
|
102
|
-
} catch (error) {
|
|
103
|
-
console.error('Error submitting form:', error);
|
|
104
|
-
} finally {
|
|
105
|
-
isSendingMessage = false;
|
|
106
|
-
}
|
|
107
|
-
loading = false;
|
|
108
|
-
};
|
|
77
|
+
function hasErrors(field: any, errors: Map<string, string>) {
|
|
78
|
+
return errors.has(fields?.id) ? 'error' : '';
|
|
79
|
+
}
|
|
109
80
|
</script>
|
|
110
81
|
|
|
82
|
+
{#snippet showErrors(fields: InputField, errors: Map<string, string>)}
|
|
83
|
+
{#if errors.has(fields.id)}
|
|
84
|
+
<div>errors.get(fields.id)</div>
|
|
85
|
+
{/if}
|
|
86
|
+
{/snippet}
|
|
111
87
|
{#snippet renderInput(field: InputField)}
|
|
112
88
|
{#if field.isFloat}
|
|
113
89
|
<FloatingLabelInput
|
|
@@ -255,53 +231,44 @@
|
|
|
255
231
|
<form method="POST" {action} class={css} bind:this={form}>
|
|
256
232
|
<fieldset>
|
|
257
233
|
{#each fields as field}
|
|
258
|
-
{
|
|
259
|
-
{
|
|
260
|
-
|
|
261
|
-
{
|
|
262
|
-
|
|
263
|
-
{
|
|
264
|
-
|
|
265
|
-
{
|
|
266
|
-
|
|
267
|
-
{
|
|
268
|
-
|
|
269
|
-
{
|
|
270
|
-
|
|
271
|
-
{
|
|
272
|
-
|
|
273
|
-
{
|
|
274
|
-
|
|
275
|
-
{
|
|
276
|
-
|
|
277
|
-
{
|
|
278
|
-
|
|
279
|
-
{
|
|
280
|
-
|
|
234
|
+
<div class={hasErrors(field, errors)}>
|
|
235
|
+
{#if field.typeOfField === 'input'}
|
|
236
|
+
{@render renderInput(field)}
|
|
237
|
+
{:else if field.typeOfField === 'textarea'}
|
|
238
|
+
{@render renderTextarea(field)}
|
|
239
|
+
{:else if field.typeOfField === 'select'}
|
|
240
|
+
{@render renderSelect(field)}
|
|
241
|
+
{:else if field.typeOfField === 'collection'}
|
|
242
|
+
{@render renderCollection(field)}
|
|
243
|
+
{:else if field.typeOfField === 'file'}
|
|
244
|
+
{@render renderFile(field)}
|
|
245
|
+
{:else if field.typeOfField === 'phoneInput'}
|
|
246
|
+
{@render renderPhoneInput(field)}
|
|
247
|
+
{:else if field.typeOfField === 'timepicker'}
|
|
248
|
+
{@render renderTimePicker(field)}
|
|
249
|
+
{:else if field.typeOfField === 'hidden'}
|
|
250
|
+
{@render renderHidden(field)}
|
|
251
|
+
{:else if field.typeOfField === 'date'}
|
|
252
|
+
{@render renderDatePicker(field)}
|
|
253
|
+
{:else if field.typeOfField === 'markdown'}
|
|
254
|
+
{@render renderMdEditory(field)}
|
|
255
|
+
{:else if field.typeOfField === 'html'}
|
|
256
|
+
{@render renderHtmlEditory(field)}
|
|
257
|
+
{/if}
|
|
258
|
+
{@render showErrors(field, errors)}
|
|
259
|
+
</div>
|
|
281
260
|
{/each}
|
|
282
261
|
|
|
283
262
|
<div class="buttons">
|
|
284
263
|
{#each buttons as button}
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
{button.label}
|
|
294
|
-
</Button>
|
|
295
|
-
{:else} -->
|
|
296
|
-
<Button
|
|
297
|
-
type={button.type}
|
|
298
|
-
class={button.class}
|
|
299
|
-
color={button.color}
|
|
300
|
-
onclick={button.action}
|
|
301
|
-
>
|
|
302
|
-
{button.label}
|
|
303
|
-
</Button>
|
|
304
|
-
<!-- {/if} -->
|
|
264
|
+
<Button
|
|
265
|
+
type={button.type}
|
|
266
|
+
class={button.class}
|
|
267
|
+
color={button.color}
|
|
268
|
+
onclick={button.action}
|
|
269
|
+
>
|
|
270
|
+
{button.label}
|
|
271
|
+
</Button>
|
|
305
272
|
{/each}
|
|
306
273
|
</div>
|
|
307
274
|
</fieldset>
|
|
@@ -128,4 +128,4 @@ export type ButtonConfig = {
|
|
|
128
128
|
class?: string;
|
|
129
129
|
action?: (event: Event) => Promise<void>;
|
|
130
130
|
};
|
|
131
|
-
export declare function convertResponseToFields(fields: Array<Field>,
|
|
131
|
+
export declare function convertResponseToFields(fields: Array<Field>, request: any): Promise<any>;
|
|
@@ -124,10 +124,11 @@ export function createEditorField(id, label, type, value) {
|
|
|
124
124
|
typeOfField: type,
|
|
125
125
|
};
|
|
126
126
|
}
|
|
127
|
-
export function convertResponseToFields(fields,
|
|
127
|
+
export async function convertResponseToFields(fields, request) {
|
|
128
|
+
const formData = await request.formData();
|
|
128
129
|
let result = {};
|
|
129
130
|
for (const field of fields) {
|
|
130
|
-
const value =
|
|
131
|
+
const value = formData.get(field.id);
|
|
131
132
|
result[field.id] = value;
|
|
132
133
|
}
|
|
133
134
|
return result;
|