@frontile/forms 0.17.0-beta.24 → 0.17.0-beta.26

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 (54) hide show
  1. package/declarations/components/checkbox-group.d.ts +3 -1
  2. package/declarations/components/checkbox.d.ts +2 -0
  3. package/declarations/components/field-types.d.ts +14 -0
  4. package/declarations/components/field.d.ts +85 -0
  5. package/declarations/components/form-control.d.ts +1 -0
  6. package/declarations/components/form.d.ts +244 -8
  7. package/declarations/components/input.d.ts +5 -0
  8. package/declarations/components/radio-group.d.ts +1 -1
  9. package/declarations/components/radio.d.ts +2 -0
  10. package/declarations/components/select.d.ts +228 -13
  11. package/declarations/components/switch.d.ts +21 -16
  12. package/declarations/components/textarea.d.ts +2 -0
  13. package/declarations/index.d.ts +3 -0
  14. package/declarations/utils/index.d.ts +1 -0
  15. package/declarations/utils/nested-data.d.ts +44 -0
  16. package/declarations/utils/standard-validator.d.ts +151 -0
  17. package/dist/_app_/components/field-types.js +1 -0
  18. package/dist/_app_/components/field.js +1 -0
  19. package/dist/components/checkbox-group.js +1 -1
  20. package/dist/components/checkbox-group.js.map +1 -1
  21. package/dist/components/checkbox.js +8 -2
  22. package/dist/components/checkbox.js.map +1 -1
  23. package/dist/components/field-types.js +2 -0
  24. package/dist/components/field-types.js.map +1 -0
  25. package/dist/components/field.js +103 -0
  26. package/dist/components/field.js.map +1 -0
  27. package/dist/components/form-control.js.map +1 -1
  28. package/dist/components/form.js +12 -29
  29. package/dist/components/form.js.map +1 -1
  30. package/dist/components/input.js +8 -2
  31. package/dist/components/input.js.map +1 -1
  32. package/dist/components/native-select.js +1 -1
  33. package/dist/components/radio-group.js +1 -1
  34. package/dist/components/radio-group.js.map +1 -1
  35. package/dist/components/radio.js +8 -2
  36. package/dist/components/radio.js.map +1 -1
  37. package/dist/components/select.js +165 -20
  38. package/dist/components/select.js.map +1 -1
  39. package/dist/components/switch.js +5 -2
  40. package/dist/components/switch.js.map +1 -1
  41. package/dist/components/textarea.js +8 -2
  42. package/dist/components/textarea.js.map +1 -1
  43. package/dist/form-CVuJDJUq.js +726 -0
  44. package/dist/form-CVuJDJUq.js.map +1 -0
  45. package/dist/form-Dzyb1-8B.js +623 -0
  46. package/dist/form-Dzyb1-8B.js.map +1 -0
  47. package/dist/index-CeaQc-U7.js.map +1 -1
  48. package/dist/index.js +2 -1
  49. package/dist/index.js.map +1 -1
  50. package/dist/runtime-BPCpkOf1-B5Lbloj7.js +67 -0
  51. package/dist/runtime-BPCpkOf1-B5Lbloj7.js.map +1 -0
  52. package/dist/test-support.js +2 -2
  53. package/dist/test-support.js.map +1 -1
  54. package/package.json +10 -7
@@ -0,0 +1,623 @@
1
+ import Component from '@glimmer/component';
2
+ import { tracked } from '@glimmer/tracking';
3
+ import { action } from '@ember/object';
4
+ import { hash } from '@ember/helper';
5
+ import { on } from '@ember/modifier';
6
+ import { dataFrom } from 'form-data-utils';
7
+ import { Field } from './components/field.js';
8
+ import { precompileTemplate } from '@ember/template-compilation';
9
+ import { setComponentTemplate } from '@ember/component';
10
+ import { d as decorateFieldV2, i as initializeDeferredDecorator, a as decorateMethodV2 } from './runtime-BPCpkOf1-B5Lbloj7.js';
11
+
12
+ /**
13
+ * Utility functions for working with nested data structures in forms.
14
+ * These functions convert between nested objects and flat dotted-path notation.
15
+ */
16
+
17
+ /**
18
+ * Type guard to check if a value is a plain object (not an array, Date, File, etc.)
19
+ */
20
+ function isPlainObject(value) {
21
+ return value !== null && typeof value === 'object' && !Array.isArray(value) && !(value instanceof Date) && !(value instanceof File) && !(value instanceof FileList);
22
+ }
23
+
24
+ /**
25
+ * Flattens a nested object into a flat object with dotted-path keys.
26
+ *
27
+ * @example
28
+ * flattenData({name: {first: 'John', last: 'Doe'}, email: 'john@example.com'})
29
+ * // Returns: {'name.first': 'John', 'name.last': 'Doe', 'email': 'john@example.com'}
30
+ *
31
+ * @param data - The nested data object to flatten
32
+ * @param prefix - Internal parameter for recursion (parent path)
33
+ * @returns A flat object with dotted-path keys
34
+ */
35
+ function flattenData(data, prefix = '') {
36
+ const result = {};
37
+ for (const key in data) {
38
+ if (!Object.prototype.hasOwnProperty.call(data, key)) {
39
+ continue;
40
+ }
41
+ const value = data[key];
42
+ const fullPath = prefix ? `${prefix}.${key}` : key;
43
+ if (isPlainObject(value)) {
44
+ // Recursively flatten nested objects
45
+ Object.assign(result, flattenData(value, fullPath));
46
+ } else {
47
+ // Store primitive values, arrays, and special objects as-is
48
+ result[fullPath] = value;
49
+ }
50
+ }
51
+ return result;
52
+ }
53
+
54
+ /**
55
+ * Unflattens a flat object with dotted-path keys into a nested object structure.
56
+ *
57
+ * @example
58
+ * unflattenData({'name.first': 'John', 'name.last': 'Doe', 'email': 'john@example.com'})
59
+ * // Returns: {name: {first: 'John', last: 'Doe'}, email: 'john@example.com'}
60
+ *
61
+ * @param data - The flat data object with dotted-path keys
62
+ * @returns A nested object structure
63
+ */
64
+ function unflattenData(data) {
65
+ const result = {};
66
+ for (const path in data) {
67
+ if (!Object.prototype.hasOwnProperty.call(data, path)) {
68
+ continue;
69
+ }
70
+ const value = data[path];
71
+ const keys = path.split('.');
72
+ let current = result;
73
+
74
+ // Navigate/create nested structure
75
+ for (let i = 0; i < keys.length - 1; i++) {
76
+ const key = keys[i];
77
+ if (!key) continue;
78
+ if (!isPlainObject(current[key])) {
79
+ current[key] = {};
80
+ }
81
+ current = current[key];
82
+ }
83
+
84
+ // Set the final value
85
+ const lastKey = keys[keys.length - 1];
86
+ if (lastKey) {
87
+ current[lastKey] = value;
88
+ }
89
+ }
90
+ return result;
91
+ }
92
+
93
+ /**
94
+ * Checks if the data structure contains any nested objects.
95
+ * If true, we need to handle it as nested data.
96
+ *
97
+ * @param data - The data object to check
98
+ * @returns True if the data contains nested plain objects
99
+ */
100
+ function hasNestedData(data) {
101
+ for (const key in data) {
102
+ if (Object.prototype.hasOwnProperty.call(data, key)) {
103
+ if (isPlainObject(data[key])) {
104
+ return true;
105
+ }
106
+ }
107
+ }
108
+ return false;
109
+ }
110
+
111
+ /**
112
+ * Deep equality comparison for nested objects.
113
+ * Handles primitives, arrays, and nested objects.
114
+ *
115
+ * @param a - First value to compare
116
+ * @param b - Second value to compare
117
+ * @returns True if values are deeply equal
118
+ */
119
+ function deepEqual(a, b) {
120
+ // Handle primitives and same reference
121
+ if (a === b) return true;
122
+
123
+ // Handle null/undefined
124
+ if (a == null || b == null) return a === b;
125
+
126
+ // Handle different types
127
+ if (typeof a !== typeof b) return false;
128
+
129
+ // Handle arrays
130
+ if (Array.isArray(a) && Array.isArray(b)) {
131
+ if (a.length !== b.length) return false;
132
+ return a.every((item, index) => deepEqual(item, b[index]));
133
+ }
134
+
135
+ // Handle plain objects
136
+ if (isPlainObject(a) && isPlainObject(b)) {
137
+ const keysA = Object.keys(a);
138
+ const keysB = Object.keys(b);
139
+ if (keysA.length !== keysB.length) return false;
140
+ return keysA.every(key => deepEqual(a[key], b[key]));
141
+ }
142
+
143
+ // Handle special objects (Date, File, etc.) - compare by reference
144
+ return a === b;
145
+ }
146
+
147
+ /**
148
+ * The issues collection returned by a Standard Schema validation failure.
149
+ */
150
+
151
+ /**
152
+ * The return type of a validator function.
153
+ * Resolves to the issues (when validation fails) or undefined
154
+ * (when it succeeds).
155
+ */
156
+
157
+ /**
158
+ * StandardSchemaV1 issues as either a promise or synchronous. If there are
159
+ * no issues, `undefined` is returned.
160
+ */
161
+
162
+ /**
163
+ * A custom validator function.
164
+ * If there are errors, they are returned by the validator as
165
+ * StandardSchemaV1 issues. If there are no errors, nothing is returned.
166
+ */
167
+
168
+ /**
169
+ * Provides convenience methods for validating data using Standard Schema.
170
+ * Also supports custom validator functions that return issues in the
171
+ * Standard Schema format.
172
+ *
173
+ * Most of the time you should use `validateAll` or `validateFieldAll` to
174
+ * validate data. Schema and custom validation are optional arguments
175
+ * to these methods. This is useful because schema and custom validation
176
+ * must both be optional in the form component, which is the primary use
177
+ * case for this class.
178
+ *
179
+ * Note: all methods are static, so you do not need to instantiate
180
+ * this class.
181
+ *
182
+ * @see https://standardschema.dev/
183
+ */
184
+ class StandardValidator {
185
+ constructor() {
186
+ throw new Error('StandardValidator is static and cannot be instantiated');
187
+ }
188
+
189
+ /**
190
+ * Validates data against the provided standard schema.
191
+ * If there are errors, they are returned. Otherwise, nothing is returned.
192
+ * This provides a boolean indication of outcome: if there is a return
193
+ * value, validation failed. Otherwise, validation succeeded.
194
+ * @see https://standardschema.dev/
195
+ *
196
+ * @example
197
+ * const input = …; // your data to validate
198
+ * const schema = …; // your standard schema
199
+ * const issues = await StandardValidator.validate(input, schema);
200
+ *
201
+ * @param input The input to validate.
202
+ * @param schema The standard schema to validate against.
203
+ * @return The issues if validation failed, otherwise undefined.
204
+ */
205
+ static async validate(input, schema) {
206
+ const result = await schema['~standard'].validate(input);
207
+ return result.issues;
208
+ }
209
+
210
+ /**
211
+ * Validates a single field within the input data using the provided
212
+ * standard schema. If there are issues related to the specified field,
213
+ * they are returned. Otherwise, nothing is returned.
214
+ *
215
+ * Note: because standard schema does not provide a way to validate
216
+ * individual fields, this method filters the issues returned by the
217
+ * schema validation to find those related to the specified field.
218
+ * @see https://standardschema.dev/
219
+ *
220
+ * @param input The input data to validate.
221
+ * @param fieldName The name of the field to validate.
222
+ * @param schema The standard schema to validate against.
223
+ * @returns The issues related to the specified field if validation failed,
224
+ * otherwise undefined.
225
+ */
226
+ static async validateField(input, fieldName, schema) {
227
+ const issues = await StandardValidator.validate(input, schema);
228
+ if (issues) {
229
+ return StandardValidator.filterFieldIssues(issues, fieldName);
230
+ }
231
+ }
232
+
233
+ /**
234
+ * Validates input using a custom validator function.
235
+ * If there are errors, they are returned by the validator as
236
+ * StandardSchemaV1 issues. If there are no errors, nothing is returned.
237
+ * @see https://standardschema.dev/
238
+ *
239
+ * @param input The input data to validate.
240
+ * @param customValidator The custom validator function to use.
241
+ * @returns The issues if validation failed, otherwise undefined.
242
+ */
243
+ static async validateCustom(input, customValidator) {
244
+ return await customValidator(input);
245
+ }
246
+
247
+ /**
248
+ * Validates a single field within the input data using a custom validator
249
+ * function. If there are issues related to the specified field, they are
250
+ * returned. Otherwise, nothing is returned.
251
+ *
252
+ * Note: because custom validators may return issues for multiple fields,
253
+ * this method filters the issues returned by the custom validator to find
254
+ * those related to the specified field.
255
+ * @see https://standardschema.dev/
256
+ *
257
+ * @param input The input data to validate.
258
+ * @param fieldName The name of the field to validate.
259
+ * @param customValidator The custom validator function to use.
260
+ * @returns The issues related to the specified field if validation failed,
261
+ * otherwise undefined.
262
+ */
263
+ static async validateFieldCustom(input, fieldName, customValidator) {
264
+ const issues = await StandardValidator.validateCustom(input, customValidator);
265
+ if (issues) {
266
+ return StandardValidator.filterFieldIssues(issues, fieldName);
267
+ }
268
+ }
269
+
270
+ /**
271
+ * Validates input data using both a standard schema and a custom validator
272
+ * function, merging any issues found by either method. If there are issues,
273
+ * they are returned. Otherwise, nothing is returned. This method runs both
274
+ * validations in parallel.
275
+ *
276
+ * Note: `schema` and `customValidator` are both optional. If either the
277
+ * schema or custom validator is not provided, that validation is skipped.
278
+ * @see https://standardschema.dev/
279
+ *
280
+ * @param input The input data to validate.
281
+ * @param schema Optional standard schema to validate against.
282
+ * @param customValidator Optional custom validator function to use.
283
+ * @returns issues if validation failed, otherwise undefined.
284
+ */
285
+ static async validateAll(input, schema, customValidator) {
286
+ const standardIssues = schema && StandardValidator.validate(input, schema);
287
+ const customIssues = customValidator && StandardValidator.validateCustom(input, customValidator);
288
+ const [s, c] = await Promise.all([standardIssues, customIssues]);
289
+ if (s || c) {
290
+ return [...(s ?? []), ...(c ?? [])];
291
+ }
292
+ }
293
+
294
+ /**
295
+ * Validates a single field within the input data using both a standard
296
+ * schema and a custom validator function, merging any issues found by
297
+ * either method. If there are issues related to the specified field,
298
+ * they are returned. Otherwise, nothing is returned. This method runs
299
+ * both validations in parallel.
300
+ *
301
+ * Note: `schema` and `customValidator` are both optional. If either the
302
+ * schema or custom validator is not provided, that validation is skipped.
303
+ * @see https://standardschema.dev/
304
+ *
305
+ * @param input The input data to validate.
306
+ * @param fieldName The name of the field to validate.
307
+ * @param schema Optional standard schema to validate against.
308
+ * @param customValidator Optional custom validator function to use.
309
+ * @returns The issues related to the specified field if validation failed,
310
+ * otherwise undefined.
311
+ */
312
+ static async validateFieldAll(input, fieldName, schema, customValidator) {
313
+ const standardIssues = schema && StandardValidator.validateField(input, fieldName, schema);
314
+ const customIssues = customValidator && StandardValidator.validateFieldCustom(input, fieldName, customValidator);
315
+ const [s, c] = await Promise.all([standardIssues, customIssues]);
316
+ if (s || c) {
317
+ return [...(s ?? []), ...(c ?? [])];
318
+ }
319
+ }
320
+
321
+ /**
322
+ * Filters issues to those related to the specified field name.
323
+ * Supports both simple field names and dotted notation for nested fields.
324
+ *
325
+ * @param issues The issues to filter.
326
+ * @param fieldName The name of the field to filter by (e.g., "email" or
327
+ * "profile.contact.email").
328
+ * @returns The issues related to the specified field, or undefined if
329
+ * there are none.
330
+ */
331
+ static filterFieldIssues(issues, fieldName) {
332
+ const fieldParts = fieldName.split('.');
333
+ const fieldIssues = issues.filter(issue => {
334
+ if (!issue.path) return false;
335
+
336
+ // Extract keys from path segments
337
+ const pathKeys = issue.path.map(s => typeof s === 'object' && 'key' in s ? s.key : s).filter(Boolean);
338
+
339
+ // For exact path matching:
340
+ // The paths must be the same length and match exactly
341
+ return pathKeys.length === fieldParts.length && pathKeys.every((key, i) => key === fieldParts[i]);
342
+ });
343
+ return fieldIssues.length > 0 ? fieldIssues : undefined;
344
+ }
345
+ }
346
+
347
+ /**
348
+ * A form component that handles form submissions and input changes.
349
+ *
350
+ * @example
351
+ * ```hbs
352
+ * <Form
353
+ * @data={{this.formData}}
354
+ * @schema={{this.schema}}
355
+ * @validate={{this.customValidator}}
356
+ * @onSubmit={{this.onSubmit}}
357
+ * @onChange={{this.onChange}}
358
+ * as |form|
359
+ * >
360
+ * <form.Field name="firstName" as |field|>
361
+ * <field.Input />
362
+ * </form.Field>
363
+ * <form.Field name="lastName" as |field|>
364
+ * <field.Input />
365
+ * </form.Field>
366
+ * <button type="submit" disabled={{form.isLoading}}>
367
+ * {{#if form.isLoading}}Submitting...{{else}}Submit{{/if}}
368
+ * </button>
369
+ * </Form>
370
+ * ```
371
+ */
372
+ class Form extends Component {
373
+ static {
374
+ decorateFieldV2(this.prototype, "isLoading", [tracked], function () {
375
+ return false;
376
+ });
377
+ }
378
+ #isLoading = (initializeDeferredDecorator(this, "isLoading"), void 0);
379
+ /** Whether the form is currently submitting. */
380
+ static {
381
+ decorateFieldV2(this.prototype, "errors", [tracked], function () {
382
+ return {};
383
+ });
384
+ }
385
+ #errors = (initializeDeferredDecorator(this, "errors"), void 0);
386
+ /** The current form validation errors. */
387
+ static {
388
+ decorateFieldV2(this.prototype, "uncontrolledData", [tracked]);
389
+ }
390
+ #uncontrolledData = (initializeDeferredDecorator(this, "uncontrolledData"), void 0);
391
+ /** The current uncontrolled form data. */
392
+ static {
393
+ decorateFieldV2(this.prototype, "initialDataSnapshot", [tracked]);
394
+ }
395
+ #initialDataSnapshot = (initializeDeferredDecorator(this, "initialDataSnapshot"), void 0);
396
+ /**
397
+ * Flattened snapshot of initial data for dirty field comparison.
398
+ * Stored in flattened form regardless of whether source data is nested or flat.
399
+ */
400
+ static {
401
+ decorateFieldV2(this.prototype, "dirty", [tracked], function () {
402
+ return new Set();
403
+ });
404
+ }
405
+ #dirty = (initializeDeferredDecorator(this, "dirty"), void 0);
406
+ /**
407
+ * The set of fields that have changed from their initial values.
408
+ * For flat data structures: contains top-level keys (e.g., "firstName", "email")
409
+ * For nested data structures: contains dotted paths (e.g., "user.name.first", "profile.email")
410
+ */
411
+ /** Reference to the form element. */element;
412
+ /**
413
+ * Creates a new instance of the Form component.
414
+ * Initializes internal state based on provided args.
415
+ * @param owner - The owner of the component.
416
+ * @param args - The arguments passed to the component.
417
+ */
418
+ constructor(owner, args) {
419
+ super(owner, args);
420
+ // Create copy of initial data
421
+ if (args.data) {
422
+ // Always store flattened snapshot for dirty tracking
423
+ // This ensures consistent comparison regardless of initial structure
424
+ this.initialDataSnapshot = flattenData(args.data);
425
+ // Keep uncontrolled data in original structure
426
+ this.uncontrolledData = {
427
+ ...args.data
428
+ };
429
+ }
430
+ }
431
+ /** Whether the form is valid (i.e. has no validation errors). */
432
+ get isValid() {
433
+ return Object.keys(this.errors).length === 0;
434
+ }
435
+ /** Whether the form is invalid (i.e. has validation errors). */
436
+ get isInvalid() {
437
+ return !this.isValid;
438
+ }
439
+ /** Whether the form is controlled (`onChange` is provided). */
440
+ get isControlled() {
441
+ return !!this.args.onChange;
442
+ }
443
+ /** The current form data, from args if controlled, or internal state if uncontrolled. */
444
+ get currentData() {
445
+ if (this.isControlled) {
446
+ return this.args.data;
447
+ }
448
+ return this.uncontrolledData;
449
+ }
450
+ /**
451
+ * Validates data against the provided schema using StandardValidator.
452
+ * Updates the `errors` property with any validation errors found.
453
+ *
454
+ * @param data - The form data to validate.
455
+ * @returns A promise that resolves to the validation errors, if any.
456
+ */
457
+ async validate(data) {
458
+ // Run validator and get issues
459
+ const errors = await StandardValidator.validateAll(data, this.args.schema, this.args.validate);
460
+ // Convert validator errors to FormErrors
461
+ if (errors) {
462
+ const formErrors = validatorToFormErrors(errors);
463
+ this.errors = formErrors;
464
+ return formErrors;
465
+ }
466
+ // Clear errors if no issues
467
+ this.errors = {};
468
+ }
469
+ /**
470
+ * Computes which fields have changed from their initial values.
471
+ * Uses deep comparison for consistency across flat and nested structures.
472
+ * This is a pure function with no side effects.
473
+ *
474
+ * @param data - The current form data.
475
+ * @returns A set of field names that have changed.
476
+ */
477
+ computeDirtyFields(data) {
478
+ const dirty = new Set();
479
+ if (!this.initialDataSnapshot) {
480
+ return dirty;
481
+ }
482
+ // Flatten current data to same representation as snapshot
483
+ const currentFlat = flattenData(data);
484
+ // Compare all keys in current data
485
+ for (const key in currentFlat) {
486
+ if (!deepEqual(currentFlat[key], this.initialDataSnapshot[key])) {
487
+ dirty.add(key);
488
+ }
489
+ }
490
+ // Check for keys in initial data that are missing in current data
491
+ for (const key in this.initialDataSnapshot) {
492
+ if (!(key in currentFlat) && this.initialDataSnapshot[key] !== undefined) {
493
+ dirty.add(key);
494
+ }
495
+ }
496
+ return dirty;
497
+ }
498
+ /**
499
+ * Builds the form result data object to pass to callbacks.
500
+ * Also updates the dirty state as a side effect.
501
+ *
502
+ * @param data - The current form data.
503
+ * @returns The form result data object.
504
+ */
505
+ buildFormResultData(data) {
506
+ const {
507
+ isValid,
508
+ isInvalid,
509
+ errors
510
+ } = this;
511
+ const dirty = this.computeDirtyFields(data);
512
+ this.dirty = dirty; // Update tracked state
513
+ return {
514
+ data,
515
+ isValid,
516
+ isInvalid,
517
+ errors,
518
+ dirty
519
+ };
520
+ }
521
+ /**
522
+ * Handles the `input` event on the form element.
523
+ * Calls the `onChange` callback with the current form data if provided.
524
+ * Automatically unflattens dotted field names to nested structure.
525
+ */
526
+ handleInput(event) {
527
+ const form = event.currentTarget;
528
+ if (form instanceof HTMLFormElement) {
529
+ let data = dataFrom(event);
530
+ // Always unflatten form data to handle dotted field names
531
+ // This is safe for flat data too - keys without dots remain unchanged
532
+ data = unflattenData(data);
533
+ const resultData = this.buildFormResultData(data);
534
+ this.uncontrolledData = data;
535
+ this.args.onChange?.(resultData, event);
536
+ }
537
+ }
538
+ /**
539
+ * Handles the `submit` event on the form element.
540
+ * Prevents the default form submission and calls the `onSubmit` callback
541
+ * with the current form data. Manages the `isLoading` state during the
542
+ * submission process. Automatically unflattens dotted field names to nested structure.
543
+ */
544
+ static {
545
+ decorateMethodV2(this.prototype, "handleInput", [action]);
546
+ }
547
+ async handleSubmit(event) {
548
+ event.preventDefault();
549
+ const form = event.currentTarget;
550
+ if (form instanceof HTMLFormElement) {
551
+ this.isLoading = true;
552
+ let data = dataFrom(event);
553
+ // Always unflatten form data to handle dotted field names
554
+ // This is safe for flat data too - keys without dots remain unchanged
555
+ data = unflattenData(data);
556
+ const errors = await this.validate(data);
557
+ const resultData = this.buildFormResultData(data);
558
+ this.uncontrolledData = data;
559
+ try {
560
+ if (errors && this.args.onError) {
561
+ // Call onError handler when there are validation errors
562
+ await this.args.onError(errors, data, event);
563
+ } else if (!errors && this.args.onSubmit) {
564
+ // Run `onSubmit` only if there are no validation errors
565
+ await this.args.onSubmit(resultData, event);
566
+ // Update snapshot on successful submit (new baseline)
567
+ this.initialDataSnapshot = flattenData(data);
568
+ // Clear dirty state since we've updated the baseline
569
+ this.dirty = new Set();
570
+ }
571
+ } finally {
572
+ this.isLoading = false;
573
+ }
574
+ }
575
+ }
576
+ static {
577
+ decorateMethodV2(this.prototype, "handleSubmit", [action]);
578
+ }
579
+ static {
580
+ setComponentTemplate(precompileTemplate("\n {{!-- @glint-nocheck component generics (field) trigger: type instantiation is excessively deep and possibly infinite --}}\n <form {{on \"input\" this.handleInput}} {{on \"submit\" this.handleSubmit}} ...attributes>\n {{yield (hash data=this.currentData isLoading=this.isLoading isValid=this.isValid isInvalid=this.isInvalid errors=this.errors dirty=this.dirty Field=(component Field errors=this.errors formData=this.currentData disabled=@disabled)) to=\"default\"}}\n </form>\n ", {
581
+ strictMode: true,
582
+ scope: () => ({
583
+ on,
584
+ hash,
585
+ Field
586
+ })
587
+ }), this);
588
+ }
589
+ }
590
+ /**
591
+ * Helper function.
592
+ * Converts validation type `Issues` from StandardValidator into a format
593
+ * suitable for the form component, type `FormErrors`.
594
+ *
595
+ * For example, an issue with path `['email']` will be converted
596
+ * to a form error for the field `email`, `{"email": "error message" }`.
597
+ *
598
+ * @param errors - The validation issues to convert.
599
+ * @returns A mapping of field names to their validation error messages.
600
+ */
601
+ function validatorToFormErrors(errors) {
602
+ const formErrors = {};
603
+ for (const issue of errors) {
604
+ if (!issue.path || issue.path.length === 0) continue;
605
+ // Extract keys from path segments to build field name
606
+ const pathKeys = issue.path.map(s => typeof s === 'object' && 'key' in s ? s.key : s).filter(Boolean);
607
+ const fieldName = pathKeys.join('.');
608
+ const message = issue.message;
609
+ // Add message to the field's errors
610
+ const existing = formErrors[fieldName];
611
+ if (existing === undefined) {
612
+ formErrors[fieldName] = message;
613
+ } else if (Array.isArray(existing)) {
614
+ existing.push(message);
615
+ } else {
616
+ formErrors[fieldName] = [existing, message];
617
+ }
618
+ }
619
+ return formErrors;
620
+ }
621
+
622
+ export { Form as F, StandardValidator as S, deepEqual as d, flattenData as f, hasNestedData as h, unflattenData as u };
623
+ //# sourceMappingURL=form-Dzyb1-8B.js.map