@finsweet/webflow-apps-utils 1.0.8 → 1.0.10

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 (51) hide show
  1. package/dist/types/customCode.d.ts +1 -1
  2. package/dist/ui/components/Loader.svelte +0 -2
  3. package/dist/ui/components/LoadingScreen.svelte +6 -2
  4. package/dist/ui/components/LoadingScreen.svelte.d.ts +1 -0
  5. package/dist/ui/components/breakpoints/BreakpointItem.svelte +2 -2
  6. package/dist/ui/components/button/Button.stories.svelte +0 -8
  7. package/dist/ui/components/button/Button.svelte +68 -76
  8. package/dist/ui/components/button/index.d.ts +1 -1
  9. package/dist/ui/components/button/index.js +1 -0
  10. package/dist/ui/components/color-picker/ColorPicker.stories.svelte +42 -0
  11. package/dist/ui/components/color-picker/ColorPicker.stories.svelte.d.ts +19 -0
  12. package/dist/ui/components/color-picker/ColorPicker.svelte +155 -0
  13. package/dist/ui/components/color-picker/ColorPicker.svelte.d.ts +8 -0
  14. package/dist/ui/components/color-picker/ColorSelect.stories.svelte +61 -0
  15. package/dist/ui/components/color-picker/ColorSelect.stories.svelte.d.ts +27 -0
  16. package/dist/ui/components/color-picker/ColorSelect.svelte +940 -0
  17. package/dist/ui/components/color-picker/ColorSelect.svelte.d.ts +7 -0
  18. package/dist/ui/components/color-picker/index.d.ts +1 -0
  19. package/dist/ui/components/color-picker/index.js +1 -0
  20. package/dist/ui/components/controlled-buttons/ControlledButtons.svelte +17 -7
  21. package/dist/ui/components/copy-text/CopyText.svelte +48 -39
  22. package/dist/ui/components/divider/Divider.stories.svelte +0 -4
  23. package/dist/ui/components/input/index.d.ts +1 -1
  24. package/dist/ui/components/input/index.js +1 -0
  25. package/dist/ui/components/layout/Layout.svelte +0 -5
  26. package/dist/ui/components/layout/examples/ExampleLayout.svelte +9 -8
  27. package/dist/ui/components/modal/Example.svelte +0 -7
  28. package/dist/ui/components/modal/Modal.stories.svelte +0 -2
  29. package/dist/ui/components/modal/Modal.svelte +1 -1
  30. package/dist/ui/components/notification/Notification.stories.svelte +0 -8
  31. package/dist/ui/components/notification/Notification.svelte +2 -2
  32. package/dist/ui/components/section/Section.stories.svelte +0 -1
  33. package/dist/ui/components/text/README.md +0 -2
  34. package/dist/ui/components/text/Text.stories.svelte +0 -6
  35. package/dist/ui/components/text/Text.svelte +0 -19
  36. package/dist/ui/components/tooltip/Tooltip.svelte +9 -5
  37. package/dist/ui/icons/FinsweetLogoOutlineIcon.svelte +17 -0
  38. package/dist/ui/icons/FinsweetLogoOutlineIcon.svelte.d.ts +26 -0
  39. package/dist/ui/icons/TriangleDownIconToggle.svelte +0 -1
  40. package/dist/ui/icons/index.d.ts +2 -1
  41. package/dist/ui/icons/index.js +2 -1
  42. package/dist/ui/index.css +1 -1
  43. package/dist/ui/providers/GlobalProviderDemo.svelte +0 -2
  44. package/dist/ui/router/Router.stories.js +7 -7
  45. package/dist/ui/router/examples/RouterExample.svelte +0 -9
  46. package/dist/ui/router/examples/pages/AboutPage.svelte +0 -4
  47. package/dist/ui/router/providers/Link.svelte +0 -2
  48. package/dist/ui/router/providers/Route.svelte +0 -3
  49. package/dist/ui/stores/form.d.ts +136 -3
  50. package/dist/ui/stores/form.js +310 -2
  51. package/package.json +1 -1
@@ -3,8 +3,280 @@ import { z } from 'zod';
3
3
  // Registry to track all form states by identifier
4
4
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
5
5
  const formsRegistry = writable({});
6
+ // Registry to track field registrations per form
7
+ const fieldRegistrations = writable({});
6
8
  // Validates class name according to HTML class name rules
7
9
  const classNameRegex = /^[a-zA-Z0-9_-]+$/;
10
+ /**
11
+ * Generic Form Manager for any Zod schema with cross-component field support
12
+ */
13
+ export class FormManager {
14
+ schema;
15
+ _store;
16
+ initialValues;
17
+ identifier;
18
+ fieldRegistrations = new Map();
19
+ // Public reactive stores that components can directly use
20
+ values;
21
+ errors;
22
+ touched;
23
+ isValid;
24
+ isDirty;
25
+ isSubmitting;
26
+ constructor(identifier, schema, initialValues) {
27
+ this.identifier = identifier;
28
+ this.schema = schema;
29
+ this.initialValues = initialValues;
30
+ // Create the internal form state store
31
+ this._store = writable({
32
+ values: initialValues,
33
+ errors: {},
34
+ touched: {},
35
+ isValid: false,
36
+ isDirty: false,
37
+ isSubmitting: false
38
+ });
39
+ // Create derived stores
40
+ this.values = derived(this._store, ($store) => $store.values);
41
+ this.errors = derived(this._store, ($store) => $store.errors);
42
+ this.touched = derived(this._store, ($store) => $store.touched);
43
+ this.isValid = derived(this._store, ($store) => $store.isValid);
44
+ this.isDirty = derived(this._store, ($store) => $store.isDirty);
45
+ this.isSubmitting = derived(this._store, ($store) => $store.isSubmitting);
46
+ // Register this form with the global registry
47
+ formsRegistry.update((registry) => {
48
+ registry[identifier] = this;
49
+ return registry;
50
+ });
51
+ // Initialize field registrations for this form
52
+ fieldRegistrations.update((registrations) => {
53
+ registrations[identifier] = {};
54
+ return registrations;
55
+ });
56
+ // Initial validation
57
+ this.validate();
58
+ }
59
+ /**
60
+ * Register a field with the form for cross-component management
61
+ */
62
+ registerField(fieldName, options) {
63
+ const registration = {
64
+ name: fieldName,
65
+ validate: options?.validate,
66
+ transform: options?.transform
67
+ };
68
+ this.fieldRegistrations.set(fieldName, registration);
69
+ // Update global field registrations
70
+ fieldRegistrations.update((registrations) => {
71
+ registrations[this.identifier][fieldName] =
72
+ registration;
73
+ return registrations;
74
+ });
75
+ return {
76
+ // Return field-specific reactive stores
77
+ value: derived(this.values, ($values) => $values[fieldName]),
78
+ error: derived(this.errors, ($errors) => $errors[fieldName] || []),
79
+ touched: derived(this.touched, ($touched) => $touched[fieldName] || false),
80
+ // Field-specific methods
81
+ setValue: (value) => this.setField(fieldName, value),
82
+ setTouched: () => this.setTouched(fieldName),
83
+ validate: () => this.validateField(fieldName)
84
+ };
85
+ }
86
+ /**
87
+ * Set the value of a specific field
88
+ */
89
+ setField(field, value) {
90
+ this._store.update((state) => {
91
+ const newState = {
92
+ ...state,
93
+ values: {
94
+ ...state.values,
95
+ [field]: value
96
+ },
97
+ touched: {
98
+ ...state.touched,
99
+ [field]: true
100
+ },
101
+ isDirty: true
102
+ };
103
+ return newState;
104
+ });
105
+ this.validate();
106
+ }
107
+ /**
108
+ * Set multiple field values at once
109
+ */
110
+ setFields(values) {
111
+ this._store.update((state) => {
112
+ const newTouched = { ...state.touched };
113
+ // Mark all updated fields as touched
114
+ Object.keys(values).forEach((key) => {
115
+ newTouched[key] = true;
116
+ });
117
+ return {
118
+ ...state,
119
+ values: {
120
+ ...state.values,
121
+ ...values
122
+ },
123
+ touched: newTouched,
124
+ isDirty: true
125
+ };
126
+ });
127
+ this.validate();
128
+ }
129
+ /**
130
+ * Set a field as touched without changing its value
131
+ */
132
+ setTouched(field) {
133
+ this._store.update((state) => ({
134
+ ...state,
135
+ touched: {
136
+ ...state.touched,
137
+ [field]: true
138
+ }
139
+ }));
140
+ }
141
+ /**
142
+ * Validate a specific field
143
+ */
144
+ validateField(field) {
145
+ const currentState = get(this._store);
146
+ const fieldValue = currentState.values[field];
147
+ const errors = [];
148
+ // Check custom field validation
149
+ const registration = this.fieldRegistrations.get(field);
150
+ if (registration?.validate) {
151
+ const customError = registration.validate(fieldValue);
152
+ if (customError) {
153
+ errors.push(customError);
154
+ }
155
+ }
156
+ // Run Zod validation on the entire form to get field-specific errors
157
+ const result = this.schema.safeParse(currentState.values);
158
+ if (!result.success) {
159
+ const zodErrors = result.error.format();
160
+ const fieldErrors = zodErrors[field]?._errors || [];
161
+ errors.push(...fieldErrors);
162
+ }
163
+ return errors;
164
+ }
165
+ /**
166
+ * Reset the form to initial values
167
+ */
168
+ reset() {
169
+ this._store.set({
170
+ values: { ...this.initialValues },
171
+ errors: {},
172
+ touched: {},
173
+ isValid: false,
174
+ isDirty: false,
175
+ isSubmitting: false
176
+ });
177
+ this.validate();
178
+ }
179
+ /**
180
+ * Set the form as submitting
181
+ */
182
+ setSubmitting(isSubmitting) {
183
+ this._store.update((state) => ({
184
+ ...state,
185
+ isSubmitting
186
+ }));
187
+ }
188
+ /**
189
+ * Update the schema (useful for dynamic forms)
190
+ */
191
+ updateSchema(newSchema) {
192
+ this.schema = newSchema;
193
+ this.validate();
194
+ }
195
+ /**
196
+ * Validate the entire form
197
+ */
198
+ validate() {
199
+ this._store.update((state) => {
200
+ const result = this.schema.safeParse(state.values);
201
+ const errors = {};
202
+ let isValid = true;
203
+ if (!result.success) {
204
+ isValid = false;
205
+ // Convert Zod errors to our error format
206
+ Object.keys(state.values).forEach((key) => {
207
+ const fieldKey = key;
208
+ const fieldErrors = result.error.format()[fieldKey]
209
+ ?._errors || [];
210
+ // Add custom field validation errors
211
+ const registration = this.fieldRegistrations.get(key);
212
+ if (registration?.validate) {
213
+ const customError = registration.validate(state.values[fieldKey]);
214
+ if (customError) {
215
+ fieldErrors.push(customError);
216
+ }
217
+ }
218
+ if (fieldErrors.length > 0) {
219
+ errors[fieldKey] = fieldErrors;
220
+ }
221
+ });
222
+ }
223
+ return {
224
+ ...state,
225
+ errors,
226
+ isValid
227
+ };
228
+ });
229
+ }
230
+ /**
231
+ * Get the current state of the form
232
+ */
233
+ getState() {
234
+ return get(this._store);
235
+ }
236
+ /**
237
+ * Destroy the form and clean up resources
238
+ */
239
+ destroy() {
240
+ // Remove from registries
241
+ formsRegistry.update((registry) => {
242
+ delete registry[this.identifier];
243
+ return registry;
244
+ });
245
+ fieldRegistrations.update((registrations) => {
246
+ delete registrations[this.identifier];
247
+ return registrations;
248
+ });
249
+ // Clear field registrations
250
+ this.fieldRegistrations.clear();
251
+ }
252
+ }
253
+ /**
254
+ * Creates a generic form with Zod validation
255
+ */
256
+ export function createGenericForm(identifier, schema, initialValues) {
257
+ const form = new FormManager(identifier, schema, initialValues);
258
+ return {
259
+ // Reactive stores that components can directly subscribe to
260
+ values: form.values,
261
+ errors: form.errors,
262
+ touched: form.touched,
263
+ isValid: form.isValid,
264
+ isDirty: form.isDirty,
265
+ isSubmitting: form.isSubmitting,
266
+ // Helper methods
267
+ setField: form.setField.bind(form),
268
+ setFields: form.setFields.bind(form),
269
+ setTouched: form.setTouched.bind(form),
270
+ reset: form.reset.bind(form),
271
+ setSubmitting: form.setSubmitting.bind(form),
272
+ updateSchema: form.updateSchema.bind(form),
273
+ registerField: form.registerField.bind(form),
274
+ validateField: form.validateField.bind(form),
275
+ // For advanced use cases
276
+ getState: form.getState.bind(form),
277
+ destroy: form.destroy.bind(form)
278
+ };
279
+ }
8
280
  /**
9
281
  * Creates a form validation utility with Svelte 5 reactive stores
10
282
  * @param identifier - Unique identifier for the form
@@ -265,7 +537,7 @@ export class FormValidator {
265
537
  }));
266
538
  }
267
539
  /**
268
- * Get the current state of the form (for legacy compatibility)
540
+ * Get the current state of the form
269
541
  */
270
542
  getState() {
271
543
  return get(this._store);
@@ -302,7 +574,6 @@ export function createForm(identifier, initialValues, options) {
302
574
  /**
303
575
  * Get a form by its identifier
304
576
  */
305
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
306
577
  export function getFormById(identifier) {
307
578
  const registry = get(formsRegistry);
308
579
  return registry[identifier];
@@ -354,6 +625,43 @@ export function createFormSubscription(identifier) {
354
625
  destroy: () => clearInterval(intervalId)
355
626
  };
356
627
  }
628
+ /**
629
+ * Helper to get field state for a specific form and field
630
+ */
631
+ export function getFieldState(formId, fieldName) {
632
+ const form = getFormById(formId);
633
+ if (!form) {
634
+ return {
635
+ value: undefined,
636
+ error: [],
637
+ touched: false,
638
+ isValid: true
639
+ };
640
+ }
641
+ const state = form.getState();
642
+ return {
643
+ value: state.values[fieldName],
644
+ error: state.errors[fieldName] || [],
645
+ touched: state.touched[fieldName] || false,
646
+ isValid: !state.errors[fieldName] || state.errors[fieldName].length === 0
647
+ };
648
+ }
649
+ /**
650
+ * Helper to update a field value for a specific form
651
+ */
652
+ export function updateFieldValue(formId, fieldName, value) {
653
+ const form = getFormById(formId);
654
+ if (form && 'setField' in form) {
655
+ form.setField(fieldName, value);
656
+ }
657
+ }
658
+ /**
659
+ * Helper to get all registered field names for a form
660
+ */
661
+ export function getFormFieldNames(formId) {
662
+ const registrations = get(fieldRegistrations);
663
+ return Object.keys(registrations[formId] || {});
664
+ }
357
665
  /**
358
666
  * Store to track if the form is adding or updating to the Canvas
359
667
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@finsweet/webflow-apps-utils",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "description": "Shared utilities for Webflow apps",
5
5
  "homepage": "https://github.com/finsweet/webflow-apps-utils",
6
6
  "repository": {