@dvirus-js/angular-signals 0.0.15 → 0.0.17

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.
@@ -0,0 +1,1409 @@
1
+ import { isSignal, signal, computed } from '@angular/core';
2
+ import { signalOrValue, fromSignalObj, writableSignal } from '@dvirus-js/angular-signals';
3
+
4
+ /**
5
+ * Normalizes a validation error object by filtering out empty/null values.
6
+ *
7
+ * Converts a raw validation result into a clean error map containing only
8
+ * non-empty string messages. Filters out undefined, null, and empty strings.
9
+ *
10
+ * @param error - Raw validation error object or null
11
+ * @returns Clean error map with only valid error messages
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * normalizeValidationError({ required: 'Error', empty: '', invalid: null });
16
+ * // Returns: { required: 'Error' }
17
+ * ```
18
+ */
19
+ function normalizeValidationError(error) {
20
+ if (!error)
21
+ return {};
22
+ return Object.entries(error).reduce((acc, [key, val]) => {
23
+ if (typeof val === 'string' && val.length > 0) {
24
+ acc[key] = val;
25
+ }
26
+ return acc;
27
+ }, {});
28
+ }
29
+ /**
30
+ * Executes an array of validators and collects all error messages.
31
+ *
32
+ * Runs each validator function with the provided context and merges all
33
+ * error messages into a single error map. Empty/null results are filtered out.
34
+ *
35
+ * @template TControls - Object type defining available sibling controls
36
+ * @template TValue - The type of value being validated
37
+ *
38
+ * @param validators - Array of validator functions to execute
39
+ * @param ctx - Validation context with current value and control accessor
40
+ * @returns Combined error map from all validators
41
+ *
42
+ * @example
43
+ * ```typescript
44
+ * const errors = collectValidationErrors(
45
+ * [signalFormValidators.required, signalFormValidators.minLength(3)],
46
+ * { item: { value: '' }, getControl: () => {} }
47
+ * );
48
+ * // Returns: { required: 'This field is required' }
49
+ * ```
50
+ */
51
+ function collectValidationErrors(validators, ctx) {
52
+ if (!validators?.length)
53
+ return {};
54
+ return validators.reduce((acc, validator) => {
55
+ const normalized = normalizeValidationError(validator(ctx));
56
+ Object.entries(normalized).forEach(([key, val]) => {
57
+ acc[key] = val;
58
+ });
59
+ return acc;
60
+ }, {});
61
+ }
62
+ /**
63
+ * Checks if an error map contains any errors.
64
+ *
65
+ * Simple utility to determine if a control has validation errors by
66
+ * checking if the error map object has any keys.
67
+ *
68
+ * @param errorMap - Error map to check
69
+ * @returns True if there are any errors, false if empty
70
+ *
71
+ * @example
72
+ * ```typescript
73
+ * hasErrors({ required: 'Error' }); // true
74
+ * hasErrors({}); // false
75
+ * ```
76
+ */
77
+ function hasErrors(errorMap) {
78
+ return Object.keys(errorMap).length > 0;
79
+ }
80
+ /**
81
+ * Recursively checks if an error tree structure is completely empty.
82
+ *
83
+ * Traverses nested error structures (objects and arrays) to determine if
84
+ * there are any actual error messages anywhere in the tree. Returns true
85
+ * only when the entire structure contains no errors.
86
+ *
87
+ * @param value - Error tree to check (can be nested objects/arrays)
88
+ * @returns True if no errors exist anywhere in the tree
89
+ *
90
+ * @example
91
+ * ```typescript
92
+ * isEmptyErrorTree({ name: undefined, address: { street: undefined } }); // true
93
+ * isEmptyErrorTree({ name: { required: 'Error' } }); // false
94
+ * isEmptyErrorTree([undefined, undefined]); // true
95
+ * ```
96
+ */
97
+ function isEmptyErrorTree(value) {
98
+ if (value === null || value === undefined)
99
+ return true;
100
+ if (Array.isArray(value)) {
101
+ return value.every(isEmptyErrorTree);
102
+ }
103
+ if (typeof value === 'object') {
104
+ return Object.values(value).every(isEmptyErrorTree);
105
+ }
106
+ return false;
107
+ }
108
+
109
+ /**
110
+ * Shared empty error map object to avoid creating new objects.
111
+ *
112
+ * @internal
113
+ */
114
+ const EMPTY_ERROR_MAP = {};
115
+ /**
116
+ * Checks if a value is a plain JavaScript object (not array, not null, not class instance).
117
+ *
118
+ * @internal
119
+ * @param value - Value to check
120
+ * @returns True if value is a plain object
121
+ */
122
+ function isPlainObject(value) {
123
+ if (!value || typeof value !== 'object')
124
+ return false;
125
+ const proto = Object.getPrototypeOf(value);
126
+ return proto === Object.prototype || proto === null;
127
+ }
128
+ /**
129
+ * Checks if an object contains any Angular signal values.
130
+ *
131
+ * @internal
132
+ * @param obj - Object to check for signal values
133
+ * @returns True if any property value is a signal
134
+ */
135
+ function hasSignalValues(obj) {
136
+ return Object.values(obj).some((value) => isSignal(value));
137
+ }
138
+ /**
139
+ * Resolves a SignalOrValue to its actual value, handling nested signal objects.
140
+ *
141
+ * If the resolved value is a plain object containing signals, it converts
142
+ * all signal properties to their values using fromSignalObj.
143
+ *
144
+ * @internal
145
+ * @template TValue - The type of value to resolve
146
+ * @param value - Signal or static value to resolve
147
+ * @returns The resolved value
148
+ */
149
+ function resolveControlValue(value) {
150
+ const resolved = signalOrValue(value);
151
+ if (isPlainObject(resolved) && hasSignalValues(resolved)) {
152
+ return fromSignalObj(resolved);
153
+ }
154
+ return resolved;
155
+ }
156
+ /**
157
+ * Normalizes various control input formats to a standard SignalFormControlConfig.
158
+ *
159
+ * Handles three input types:
160
+ * - SignalFormControl: extracts current value
161
+ * - Config object with 'value' property: uses as-is
162
+ * - Raw value: wraps in config object
163
+ *
164
+ * @internal
165
+ * @template TValue - The type of control value
166
+ * @template TControls - Object type defining available sibling controls
167
+ * @param input - Input in any accepted format
168
+ * @returns Normalized control configuration
169
+ */
170
+ function normalizeControlInput(input) {
171
+ if (isSignalFormControl(input)) {
172
+ return { value: input.value() };
173
+ }
174
+ if (typeof input === 'object' && input !== null && 'value' in input) {
175
+ return input;
176
+ }
177
+ return { value: input };
178
+ }
179
+ /**
180
+ * Type guard to check if an object is a SignalFormControl.
181
+ *
182
+ * @template TValue - The type of value the control manages
183
+ * @param obj - Object to check
184
+ * @returns True if obj is a SignalFormControl
185
+ *
186
+ * @example
187
+ * ```typescript
188
+ * if (isSignalFormControl(value)) {
189
+ * console.log(value.value()); // TypeScript knows this is a control
190
+ * }
191
+ * ```
192
+ */
193
+ function isSignalFormControl(obj) {
194
+ return (!!obj &&
195
+ typeof obj === 'object' &&
196
+ obj.kind === 'control');
197
+ }
198
+ /**
199
+ * Creates a reactive form control with signal-based state management.
200
+ *
201
+ * Builds a control that wraps a primitive value (string, number, boolean, etc.)
202
+ * with validation, state tracking, and reactive updates using Angular signals.
203
+ *
204
+ * Features:
205
+ * - Reactive value updates through signals
206
+ * - Validators for errors (mark control as invalid)
207
+ * - Warnings (validation messages without invalidating)
208
+ * - Dynamic disabled state based on form context
209
+ * - State tracking (touched, dirty)
210
+ * - Manual error/warning management
211
+ *
212
+ * @template TControls - Object type defining available sibling controls for cross-field validation
213
+ * @template TValue - The type of value this control manages
214
+ *
215
+ * @param input - Control input (raw value, config object, or existing control)
216
+ * @param getControl - Optional accessor function for sibling controls
217
+ * @returns Fully configured SignalFormControl instance
218
+ *
219
+ * @example
220
+ * ```typescript
221
+ * // Simple control
222
+ * const nameControl = createSignalFormControl('John');
223
+ *
224
+ * // With validators
225
+ * const ageControl = createSignalFormControl({
226
+ * value: 25,
227
+ * validators: [signalFormValidators.required, signalFormValidators.min(0)],
228
+ * warnings: [signalFormValidators.max(120)]
229
+ * });
230
+ *
231
+ * // With dynamic disabled
232
+ * const emailControl = createSignalFormControl({
233
+ * value: '',
234
+ * disabled: (ctx) => ctx.getControl('accountType').value() === 'guest'
235
+ * });
236
+ * ```
237
+ */
238
+ function createSignalFormControl(input, getControl) {
239
+ if (isSignalFormControl(input)) {
240
+ return input;
241
+ }
242
+ const config = normalizeControlInput(input);
243
+ const accessControl = getControl ??
244
+ (() => {
245
+ throw new Error('getControl is not available for this control.');
246
+ });
247
+ const initialValue = resolveControlValue(config.value);
248
+ const value = writableSignal(() => resolveControlValue(config.value));
249
+ const manualErrors = signal({}, ...(ngDevMode ? [{ debugName: "manualErrors" }] : []));
250
+ const manualWarnings = signal({}, ...(ngDevMode ? [{ debugName: "manualWarnings" }] : []));
251
+ const selfTouched = signal(false, ...(ngDevMode ? [{ debugName: "selfTouched" }] : []));
252
+ const selfDirty = signal(false, ...(ngDevMode ? [{ debugName: "selfDirty" }] : []));
253
+ const selfDisabled = signal(false, ...(ngDevMode ? [{ debugName: "selfDisabled" }] : []));
254
+ const disabledResolver = config.disabled;
255
+ const disabled = computed(() => {
256
+ const derived = typeof disabledResolver === 'function'
257
+ ? disabledResolver({
258
+ item: { value: value() },
259
+ getControl: accessControl,
260
+ })
261
+ : disabledResolver
262
+ ? signalOrValue(disabledResolver)
263
+ : false;
264
+ return selfDisabled() || derived;
265
+ }, ...(ngDevMode ? [{ debugName: "disabled" }] : []));
266
+ const errors = computed(() => {
267
+ if (disabled())
268
+ return EMPTY_ERROR_MAP;
269
+ const ctx = {
270
+ item: { value: value() },
271
+ getControl: accessControl,
272
+ };
273
+ return {
274
+ ...collectValidationErrors(config.validators, ctx),
275
+ ...manualErrors(),
276
+ };
277
+ }, ...(ngDevMode ? [{ debugName: "errors" }] : []));
278
+ const warnings = computed(() => {
279
+ if (disabled())
280
+ return EMPTY_ERROR_MAP;
281
+ const ctx = {
282
+ item: { value: value() },
283
+ getControl: accessControl,
284
+ };
285
+ return {
286
+ ...collectValidationErrors(config.warnings, ctx),
287
+ ...manualWarnings(),
288
+ };
289
+ }, ...(ngDevMode ? [{ debugName: "warnings" }] : []));
290
+ const invalid = computed(() => !disabled() && hasErrors(errors()), ...(ngDevMode ? [{ debugName: "invalid" }] : []));
291
+ const valid = computed(() => !invalid(), ...(ngDevMode ? [{ debugName: "valid" }] : []));
292
+ const firstError = computed(() => {
293
+ const entries = Object.entries(errors());
294
+ if (!entries.length)
295
+ return undefined;
296
+ const [name, message] = entries[0] ?? ['', ''];
297
+ return { name, message, type: 'error' };
298
+ }, ...(ngDevMode ? [{ debugName: "firstError" }] : []));
299
+ const firstWarning = computed(() => {
300
+ const entries = Object.entries(warnings());
301
+ if (!entries.length)
302
+ return undefined;
303
+ const [name, message] = entries[0] ?? ['', ''];
304
+ return { name, message, type: 'warning' };
305
+ }, ...(ngDevMode ? [{ debugName: "firstWarning" }] : []));
306
+ const firstErrorOrWarning = computed(() => {
307
+ return firstError() ?? firstWarning();
308
+ }, ...(ngDevMode ? [{ debugName: "firstErrorOrWarning" }] : []));
309
+ const setValue = (next, options) => {
310
+ value.set(next);
311
+ if (options?.markDirty ?? true)
312
+ selfDirty.set(true);
313
+ if (options?.markTouched)
314
+ selfTouched.set(true);
315
+ };
316
+ const reset = (next) => {
317
+ value.set(next ?? initialValue);
318
+ selfDirty.set(false);
319
+ selfTouched.set(false);
320
+ manualErrors.set({});
321
+ manualWarnings.set({});
322
+ };
323
+ return {
324
+ kind: 'control',
325
+ value,
326
+ disabled,
327
+ touched: computed(() => selfTouched()),
328
+ dirty: computed(() => selfDirty()),
329
+ errors,
330
+ warnings,
331
+ selfErrors: errors,
332
+ selfWarnings: warnings,
333
+ invalid,
334
+ valid,
335
+ firstError,
336
+ firstWarning,
337
+ firstErrorOrWarning,
338
+ setValue,
339
+ reset,
340
+ markTouched: () => selfTouched.set(true),
341
+ markUntouched: () => selfTouched.set(false),
342
+ markDirty: () => selfDirty.set(true),
343
+ markPristine: () => selfDirty.set(false),
344
+ setDisabled: (next) => selfDisabled.set(next),
345
+ setError: (key, message) => manualErrors.update((current) => ({ ...current, [key]: message })),
346
+ clearError: (key) => manualErrors.update((current) => {
347
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
348
+ const { [key]: _removed, ...rest } = current;
349
+ return rest;
350
+ }),
351
+ clearErrors: () => manualErrors.set({}),
352
+ setWarning: (key, message) => manualWarnings.update((current) => ({ ...current, [key]: message })),
353
+ clearWarning: (key) => manualWarnings.update((current) => {
354
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
355
+ const { [key]: _removed, ...rest } = current;
356
+ return rest;
357
+ }),
358
+ clearWarnings: () => manualWarnings.set({}),
359
+ };
360
+ }
361
+
362
+ /**
363
+ * Set of valid keys for control configuration objects.
364
+ * Used to distinguish config objects from plain value objects.
365
+ *
366
+ * @internal
367
+ */
368
+ const CONTROL_CONFIG_KEYS$1 = new Set([
369
+ 'value',
370
+ 'validators',
371
+ 'warnings',
372
+ 'disabled',
373
+ ]);
374
+ /**
375
+ * Checks if a value is a control configuration object.
376
+ *
377
+ * Determines if the value has a 'value' property and only contains
378
+ * valid control config keys (value, validators, warnings, disabled).
379
+ *
380
+ * @internal
381
+ * @param value - Value to check
382
+ * @returns True if value is a control config object
383
+ */
384
+ function isControlConfig$1(value) {
385
+ if (!value || typeof value !== 'object')
386
+ return false;
387
+ if (!('value' in value))
388
+ return false;
389
+ return Object.keys(value).every(key => CONTROL_CONFIG_KEYS$1.has(key));
390
+ }
391
+ /**
392
+ * Creates the appropriate control node from various input types.
393
+ *
394
+ * Recursively determines and creates the correct control type:
395
+ * - Existing controls are returned as-is
396
+ * - Arrays become SignalFormArray
397
+ * - Objects (non-config) become SignalForm (group)
398
+ * - Primitives/configs become SignalFormControl
399
+ *
400
+ * @internal
401
+ * @template TValue - The type of value to create a control for
402
+ * @template TControls - Object type defining available sibling controls
403
+ * @param input - Input value in any accepted format
404
+ * @param getControl - Accessor for sibling controls
405
+ * @returns Appropriate control type for the input
406
+ */
407
+ function createNodeFromInput$1(input, getControl) {
408
+ if (isSignalFormControl(input)) {
409
+ return input;
410
+ }
411
+ if (isSignalFormGroup(input)) {
412
+ return input;
413
+ }
414
+ if (isSignalFormArray(input)) {
415
+ return input;
416
+ }
417
+ if (Array.isArray(input)) {
418
+ return createSignalFormArray(input, getControl);
419
+ }
420
+ if (typeof input === 'object' && input !== null && !isControlConfig$1(input)) {
421
+ return createSignalFormGroup(input);
422
+ }
423
+ return createSignalFormControl(input, getControl);
424
+ }
425
+ /**
426
+ * Type guard to check if an object is a SignalForm (form group).
427
+ *
428
+ * @template TData - Object type defining the form structure
429
+ * @param obj - Object to check
430
+ * @returns True if obj is a SignalForm
431
+ *
432
+ * @example
433
+ * ```typescript
434
+ * if (isSignalFormGroup(value)) {
435
+ * console.log(value.controls.name); // TypeScript knows this is a form group
436
+ * }
437
+ * ```
438
+ */
439
+ function isSignalFormGroup(obj) {
440
+ return (!!obj &&
441
+ typeof obj === 'object' &&
442
+ obj.kind === 'group');
443
+ }
444
+ /**
445
+ * Creates a reactive form group for managing structured form data.
446
+ *
447
+ * Builds a typed form container that holds multiple named controls, groups, or arrays.
448
+ * Each property in the input object becomes a control with full signal-based reactivity.
449
+ * Provides type-safe access to controls and tracks collective validation state.
450
+ *
451
+ * Features:
452
+ * - Type-safe control access via `.controls` property
453
+ * - Reactive value and error tracking across all controls
454
+ * - Collective state management (touched, dirty, valid)
455
+ * - Manual error/warning management at group level
456
+ * - Support for nested groups and arrays
457
+ * - Cross-field validation via getControl accessor
458
+ *
459
+ * @template TData - Object type defining the structure and types of all controls
460
+ *
461
+ * @param inputs - Object mapping property names to their control inputs
462
+ * @returns Fully configured SignalForm instance
463
+ *
464
+ * @example
465
+ * ```typescript
466
+ * // Simple form
467
+ * const form = createSignalFormGroup({
468
+ * name: 'John',
469
+ * age: 25
470
+ * });
471
+ *
472
+ * // With validators and nested structure
473
+ * const form = createSignalFormGroup<User>({
474
+ * email: {
475
+ * value: '',
476
+ * validators: [signalFormValidators.required, signalFormValidators.email]
477
+ * },
478
+ * age: {
479
+ * value: 25,
480
+ * validators: [signalFormValidators.min(0)],
481
+ * warnings: [signalFormValidators.max(120)]
482
+ * },
483
+ * address: {
484
+ * street: '123 Main St',
485
+ * city: 'NYC'
486
+ * },
487
+ * hobbies: ['coding', 'gaming']
488
+ * });
489
+ *
490
+ * // Access controls
491
+ * form.controls.email.value(); // Type-safe access
492
+ * form.getControl('age').setValue(30);
493
+ * ```
494
+ */
495
+ function createSignalFormGroup(inputs) {
496
+ const controls = {};
497
+ const getControl = key => controls[key];
498
+ Object.entries(inputs).forEach(([key, value]) => {
499
+ controls[key] = createNodeFromInput$1(value, getControl);
500
+ });
501
+ const selfDirty = signal(false, ...(ngDevMode ? [{ debugName: "selfDirty" }] : []));
502
+ const selfTouched = signal(false, ...(ngDevMode ? [{ debugName: "selfTouched" }] : []));
503
+ const selfDisabled = signal(false, ...(ngDevMode ? [{ debugName: "selfDisabled" }] : []));
504
+ const manualErrors = signal({}, ...(ngDevMode ? [{ debugName: "manualErrors" }] : []));
505
+ const manualWarnings = signal({}, ...(ngDevMode ? [{ debugName: "manualWarnings" }] : []));
506
+ const value = computed(() => {
507
+ return Object.entries(controls).reduce((acc, [key, control]) => {
508
+ acc[key] = control.value();
509
+ return acc;
510
+ }, {});
511
+ }, ...(ngDevMode ? [{ debugName: "value" }] : []));
512
+ const errors = computed(() => {
513
+ const all = {};
514
+ Object.entries(controls).forEach(([key, control]) => {
515
+ if (control.kind === 'control') {
516
+ const map = control.errors();
517
+ all[key] = (hasErrors(map) ? map : undefined);
518
+ return;
519
+ }
520
+ const childErrors = control.errors();
521
+ all[key] = (isEmptyErrorTree(childErrors) ? undefined : childErrors);
522
+ });
523
+ return all;
524
+ }, ...(ngDevMode ? [{ debugName: "errors" }] : []));
525
+ const warnings = computed(() => {
526
+ const all = {};
527
+ Object.entries(controls).forEach(([key, control]) => {
528
+ if (control.kind === 'control') {
529
+ const map = control.warnings();
530
+ all[key] = (hasErrors(map) ? map : undefined);
531
+ return;
532
+ }
533
+ const childWarnings = control.warnings();
534
+ all[key] = (isEmptyErrorTree(childWarnings) ? undefined : childWarnings);
535
+ });
536
+ return all;
537
+ }, ...(ngDevMode ? [{ debugName: "warnings" }] : []));
538
+ const selfErrors = computed(() => {
539
+ if (selfDisabled())
540
+ return {};
541
+ return { ...manualErrors() };
542
+ }, ...(ngDevMode ? [{ debugName: "selfErrors" }] : []));
543
+ const selfWarnings = computed(() => {
544
+ if (selfDisabled())
545
+ return {};
546
+ return { ...manualWarnings() };
547
+ }, ...(ngDevMode ? [{ debugName: "selfWarnings" }] : []));
548
+ const invalid = computed(() => {
549
+ if (selfDisabled())
550
+ return false;
551
+ return (hasErrors(selfErrors()) ||
552
+ Object.values(controls).some(control => control.invalid()));
553
+ }, ...(ngDevMode ? [{ debugName: "invalid" }] : []));
554
+ const valid = computed(() => !invalid(), ...(ngDevMode ? [{ debugName: "valid" }] : []));
555
+ const touched = computed(() => selfTouched() ||
556
+ Object.values(controls).some(control => control.touched()), ...(ngDevMode ? [{ debugName: "touched" }] : []));
557
+ const dirty = computed(() => selfDirty() ||
558
+ Object.values(controls).some(control => control.dirty()), ...(ngDevMode ? [{ debugName: "dirty" }] : []));
559
+ const setValue = (nextValues, options) => {
560
+ Object.entries(nextValues).forEach(([key, nextValue]) => {
561
+ const control = controls[key];
562
+ control?.setValue(nextValue, options);
563
+ });
564
+ if (options?.markDirty ?? true)
565
+ selfDirty.set(true);
566
+ if (options?.markTouched)
567
+ selfTouched.set(true);
568
+ };
569
+ const reset = (nextValues) => {
570
+ if (nextValues) {
571
+ setValue(nextValues, { markDirty: false });
572
+ }
573
+ else {
574
+ Object.values(controls).forEach(control => control.reset());
575
+ }
576
+ selfDirty.set(false);
577
+ selfTouched.set(false);
578
+ manualErrors.set({});
579
+ manualWarnings.set({});
580
+ };
581
+ const setDisabled = (disabled, options) => {
582
+ selfDisabled.set(disabled);
583
+ if (!options?.onlySelf) {
584
+ Object.values(controls).forEach(control => control.setDisabled(disabled));
585
+ }
586
+ };
587
+ return {
588
+ kind: 'group',
589
+ controls,
590
+ value,
591
+ errors,
592
+ warnings,
593
+ selfErrors,
594
+ selfWarnings,
595
+ disabled: computed(() => selfDisabled()),
596
+ touched,
597
+ dirty,
598
+ invalid,
599
+ valid,
600
+ getControl,
601
+ setValue,
602
+ reset,
603
+ markTouched: () => selfTouched.set(true),
604
+ markUntouched: () => selfTouched.set(false),
605
+ markDirty: () => selfDirty.set(true),
606
+ markPristine: () => selfDirty.set(false),
607
+ markAllTouched: () => Object.values(controls).forEach(control => control.markTouched()),
608
+ setDisabled,
609
+ setError: (key, message) => manualErrors.update(current => ({ ...current, [key]: message })),
610
+ clearError: (key) => manualErrors.update(current => {
611
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
612
+ const { [key]: _removed, ...rest } = current;
613
+ return rest;
614
+ }),
615
+ clearErrors: () => manualErrors.set({}),
616
+ setWarning: (key, message) => manualWarnings.update(current => ({ ...current, [key]: message })),
617
+ clearWarning: (key) => manualWarnings.update(current => {
618
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
619
+ const { [key]: _removed, ...rest } = current;
620
+ return rest;
621
+ }),
622
+ clearWarnings: () => manualWarnings.set({}),
623
+ };
624
+ }
625
+ /**
626
+ * Helper function to create a standalone form control.
627
+ *
628
+ * Creates a control without sibling control access. Useful for creating
629
+ * individual controls outside of a form group context.
630
+ *
631
+ * @template TValue - The type of value the control manages
632
+ * @param input - Control input (raw value, config object, or existing control)
633
+ * @returns SignalFormControl instance
634
+ *
635
+ * @example
636
+ * ```typescript
637
+ * const nameControl = formControl('John');
638
+ * const ageControl = formControl({
639
+ * value: 25,
640
+ * validators: [signalFormValidators.min(0)]
641
+ * });
642
+ * ```
643
+ */
644
+ function formControl(input) {
645
+ return createSignalFormControl(input, undefined);
646
+ }
647
+ /**
648
+ * Helper function to create a standalone form array.
649
+ *
650
+ * Creates an array without sibling control access. Useful for creating
651
+ * array controls outside of a form group context.
652
+ *
653
+ * @template TValue - The type of each item in the array
654
+ * @param input - Array of initial items
655
+ * @returns SignalFormArray instance
656
+ *
657
+ * @example
658
+ * ```typescript
659
+ * const tagsArray = formArray(['tag1', 'tag2', 'tag3']);
660
+ * const addressesArray = formArray<Address>([
661
+ * { street: '123 Main', city: 'NYC' }
662
+ * ]);
663
+ * ```
664
+ */
665
+ function formArray(input) {
666
+ return createSignalFormArray(input, undefined);
667
+ }
668
+ /**
669
+ * Helper function to create a form group.
670
+ *
671
+ * Alias for createSignalFormGroup. Creates a typed form with multiple controls.
672
+ *
673
+ * @template TData - Object type defining the form structure
674
+ * @param input - Object mapping property names to control inputs
675
+ * @returns SignalForm instance
676
+ *
677
+ * @example
678
+ * ```typescript
679
+ * const form = formGroup({
680
+ * name: 'John',
681
+ * email: {
682
+ * value: 'john@example.com',
683
+ * validators: [signalFormValidators.email]
684
+ * }
685
+ * });
686
+ * ```
687
+ */
688
+ function formGroup(input) {
689
+ return createSignalFormGroup(input);
690
+ }
691
+ /**
692
+ * Primary API for creating signal-based reactive forms.
693
+ *
694
+ * Alias for `formGroup`. This is the main entry point for creating forms.
695
+ * Provides type-safe, signal-based form state management with built-in validation.
696
+ *
697
+ * @example
698
+ * ```typescript
699
+ * // Basic form
700
+ * const form = signalForm({ name: 'John', age: 25 });
701
+ *
702
+ * // Complex form with validation
703
+ * const form = signalForm<Person>({
704
+ * name: {
705
+ * value: '',
706
+ * validators: [signalFormValidators.required, signalFormValidators.minLength(2)]
707
+ * },
708
+ * age: {
709
+ * value: 30,
710
+ * validators: [signalFormValidators.min(0)],
711
+ * warnings: [signalFormValidators.max(120)],
712
+ * disabled: (ctx) => ctx.getControl('name').value() === 'admin'
713
+ * },
714
+ * address: {
715
+ * street: '123 Main St',
716
+ * city: 'NYC'
717
+ * },
718
+ * hobbies: ['coding', 'gaming']
719
+ * });
720
+ *
721
+ * // Access form state
722
+ * console.log(form.value()); // { name: '', age: 30, address: {...}, hobbies: [...] }
723
+ * console.log(form.valid()); // boolean
724
+ * console.log(form.controls.name.errors()); // { required: 'This field is required' }
725
+ * ```
726
+ */
727
+ const signalForm = formGroup;
728
+
729
+ /**
730
+ * Set of valid keys for control configuration objects.
731
+ * Used to distinguish config objects from plain value objects.
732
+ *
733
+ * @internal
734
+ */
735
+ const CONTROL_CONFIG_KEYS = new Set([
736
+ 'value',
737
+ 'validators',
738
+ 'warnings',
739
+ 'disabled',
740
+ ]);
741
+ /**
742
+ * Checks if a value is a control configuration object.
743
+ *
744
+ * Determines if the value has a 'value' property and only contains
745
+ * valid control config keys (value, validators, warnings, disabled).
746
+ *
747
+ * @internal
748
+ * @param value - Value to check
749
+ * @returns True if value is a control config object
750
+ */
751
+ function isControlConfig(value) {
752
+ if (!value || typeof value !== 'object')
753
+ return false;
754
+ if (!('value' in value))
755
+ return false;
756
+ return Object.keys(value).every(key => CONTROL_CONFIG_KEYS.has(key));
757
+ }
758
+ /**
759
+ * Creates the appropriate control node from various input types.
760
+ *
761
+ * Recursively determines and creates the correct control type:
762
+ * - Existing controls are returned as-is
763
+ * - Arrays become SignalFormArray
764
+ * - Objects (non-config) become SignalForm (group)
765
+ * - Primitives/configs become SignalFormControl
766
+ *
767
+ * @internal
768
+ * @template TItem - The type of item to create a control for
769
+ * @template TControls - Object type defining available sibling controls
770
+ * @param input - Input value in any accepted format
771
+ * @param getControl - Accessor for sibling controls
772
+ * @returns Appropriate control type for the input
773
+ */
774
+ function createNodeFromInput(input, getControl) {
775
+ if (isSignalFormControl(input)) {
776
+ return input;
777
+ }
778
+ if (isSignalFormGroup(input)) {
779
+ return input;
780
+ }
781
+ if (isSignalFormArray(input)) {
782
+ return input;
783
+ }
784
+ if (Array.isArray(input)) {
785
+ return createSignalFormArray(input, getControl);
786
+ }
787
+ if (typeof input === 'object' && input !== null && !isControlConfig(input)) {
788
+ return createSignalFormGroup(input);
789
+ }
790
+ return createSignalFormControl(input, getControl);
791
+ }
792
+ /**
793
+ * Type guard to check if an object is a SignalFormArray.
794
+ *
795
+ * @template TValue - The type of items in the array
796
+ * @param obj - Object to check
797
+ * @returns True if obj is a SignalFormArray
798
+ *
799
+ * @example
800
+ * ```typescript
801
+ * if (isSignalFormArray(value)) {
802
+ * console.log(value.controls().length); // TypeScript knows this is an array
803
+ * }
804
+ * ```
805
+ */
806
+ function isSignalFormArray(obj) {
807
+ return (!!obj &&
808
+ typeof obj === 'object' &&
809
+ obj.kind === 'array');
810
+ }
811
+ /**
812
+ * Creates a reactive form array for managing dynamic collections of controls.
813
+ *
814
+ * Builds an array container that can hold multiple controls (primitives, groups, or nested arrays)
815
+ * with full signal-based reactivity. Provides methods for dynamic addition/removal of items
816
+ * and tracks collective validation state.
817
+ *
818
+ * Features:
819
+ * - Dynamic array operations (push, insert, remove, clear)
820
+ * - Reactive value and error tracking across all items
821
+ * - Collective state management (touched, dirty, valid)
822
+ * - Manual error/warning management at array level
823
+ * - Type-safe access to individual controls
824
+ *
825
+ * @template TItem - The type of each item in the array
826
+ * @template TControls - Object type defining available sibling controls
827
+ *
828
+ * @param inputItems - Array of initial items (values, configs, or controls)
829
+ * @param getControl - Optional accessor for sibling controls
830
+ * @returns Fully configured SignalFormArray instance
831
+ *
832
+ * @example
833
+ * ```typescript
834
+ * // Array of primitives
835
+ * const tagsArray = createSignalFormArray(['tag1', 'tag2']);
836
+ * tagsArray.push('tag3');
837
+ *
838
+ * // Array of objects
839
+ * const addressesArray = createSignalFormArray<Address>([
840
+ * { street: '123 Main', city: 'NYC' },
841
+ * { street: '456 Oak', city: 'LA' }
842
+ * ]);
843
+ *
844
+ * // Array with validators
845
+ * const hobbiesArray = createSignalFormArray([
846
+ * { value: 'coding', validators: [signalFormValidators.minLength(3)] },
847
+ * 'gaming'
848
+ * ]);
849
+ * ```
850
+ */
851
+ function createSignalFormArray(inputItems, getControl) {
852
+ const accessControl = getControl ??
853
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
854
+ ((_) => {
855
+ throw new Error('getControl is not available for this array.');
856
+ });
857
+ const controls = signal(inputItems.map(item => createNodeFromInput(item, accessControl)), ...(ngDevMode ? [{ debugName: "controls" }] : []));
858
+ const selfDirty = signal(false, ...(ngDevMode ? [{ debugName: "selfDirty" }] : []));
859
+ const selfTouched = signal(false, ...(ngDevMode ? [{ debugName: "selfTouched" }] : []));
860
+ const selfDisabled = signal(false, ...(ngDevMode ? [{ debugName: "selfDisabled" }] : []));
861
+ const manualErrors = signal({}, ...(ngDevMode ? [{ debugName: "manualErrors" }] : []));
862
+ const manualWarnings = signal({}, ...(ngDevMode ? [{ debugName: "manualWarnings" }] : []));
863
+ const value = computed(() => controls().map(control => control.value()), ...(ngDevMode ? [{ debugName: "value" }] : []));
864
+ const errors = computed(() => controls().map(control => {
865
+ if (control.kind === 'control') {
866
+ const map = control.errors();
867
+ return hasErrors(map) ? map : undefined;
868
+ }
869
+ const childErrors = control.errors();
870
+ return isEmptyErrorTree(childErrors) ? undefined : childErrors;
871
+ }), ...(ngDevMode ? [{ debugName: "errors" }] : []));
872
+ const warnings = computed(() => controls().map(control => {
873
+ if (control.kind === 'control') {
874
+ const map = control.warnings();
875
+ return hasErrors(map) ? map : undefined;
876
+ }
877
+ const childWarnings = control.warnings();
878
+ return isEmptyErrorTree(childWarnings) ? undefined : childWarnings;
879
+ }), ...(ngDevMode ? [{ debugName: "warnings" }] : []));
880
+ const selfErrors = computed(() => {
881
+ if (selfDisabled())
882
+ return {};
883
+ return { ...manualErrors() };
884
+ }, ...(ngDevMode ? [{ debugName: "selfErrors" }] : []));
885
+ const selfWarnings = computed(() => {
886
+ if (selfDisabled())
887
+ return {};
888
+ return { ...manualWarnings() };
889
+ }, ...(ngDevMode ? [{ debugName: "selfWarnings" }] : []));
890
+ const invalid = computed(() => {
891
+ if (selfDisabled())
892
+ return false;
893
+ return (hasErrors(selfErrors()) || controls().some(control => control.invalid()));
894
+ }, ...(ngDevMode ? [{ debugName: "invalid" }] : []));
895
+ const valid = computed(() => !invalid(), ...(ngDevMode ? [{ debugName: "valid" }] : []));
896
+ const touched = computed(() => selfTouched() || controls().some(control => control.touched()), ...(ngDevMode ? [{ debugName: "touched" }] : []));
897
+ const dirty = computed(() => selfDirty() || controls().some(control => control.dirty()), ...(ngDevMode ? [{ debugName: "dirty" }] : []));
898
+ const insert = (item, index) => {
899
+ const node = createNodeFromInput(item, accessControl);
900
+ const position = index ?? controls().length;
901
+ controls.update(old => {
902
+ const newArray = [...old];
903
+ newArray.splice(position, 0, node);
904
+ return newArray;
905
+ });
906
+ selfDirty.set(true);
907
+ return position;
908
+ };
909
+ const push = (item) => insert(item);
910
+ const removeAt = (index) => {
911
+ controls.update(old => {
912
+ const newArray = [...old];
913
+ newArray.splice(index, 1);
914
+ return newArray;
915
+ });
916
+ selfDirty.set(true);
917
+ };
918
+ const clear = () => {
919
+ controls.set([]);
920
+ selfDirty.set(true);
921
+ };
922
+ const at = (index) => controls()[index];
923
+ const setValue = (nextValues, options) => {
924
+ controls.update(old => {
925
+ if (nextValues.length !== old.length) {
926
+ return nextValues.map(_value => createNodeFromInput(_value, accessControl));
927
+ }
928
+ old.forEach((control, index) => {
929
+ control.setValue(nextValues[index], options);
930
+ });
931
+ return old;
932
+ });
933
+ if (options?.markDirty ?? true)
934
+ selfDirty.set(true);
935
+ if (options?.markTouched)
936
+ selfTouched.set(true);
937
+ };
938
+ const reset = (nextValues) => {
939
+ if (nextValues) {
940
+ setValue(nextValues, { markDirty: false });
941
+ }
942
+ else {
943
+ controls().forEach(control => control.reset());
944
+ }
945
+ selfDirty.set(false);
946
+ selfTouched.set(false);
947
+ manualErrors.set({});
948
+ manualWarnings.set({});
949
+ };
950
+ const setDisabled = (disabled, options) => {
951
+ selfDisabled.set(disabled);
952
+ if (!options?.onlySelf) {
953
+ controls().forEach(control => control.setDisabled(disabled));
954
+ }
955
+ };
956
+ return {
957
+ kind: 'array',
958
+ controls,
959
+ value,
960
+ errors,
961
+ warnings,
962
+ selfErrors,
963
+ selfWarnings,
964
+ disabled: computed(() => selfDisabled()),
965
+ touched,
966
+ dirty,
967
+ invalid,
968
+ valid,
969
+ insert,
970
+ push,
971
+ removeAt,
972
+ clear,
973
+ at,
974
+ setValue,
975
+ reset,
976
+ markTouched: () => selfTouched.set(true),
977
+ markUntouched: () => selfTouched.set(false),
978
+ markDirty: () => selfDirty.set(true),
979
+ markPristine: () => selfDirty.set(false),
980
+ markAllTouched: () => controls().forEach(control => control.markTouched()),
981
+ setDisabled,
982
+ setError: (key, message) => manualErrors.update(current => ({ ...current, [key]: message })),
983
+ clearError: (key) => manualErrors.update(current => {
984
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
985
+ const { [key]: _removed, ...rest } = current;
986
+ return rest;
987
+ }),
988
+ clearErrors: () => manualErrors.set({}),
989
+ setWarning: (key, message) => manualWarnings.update(current => ({ ...current, [key]: message })),
990
+ clearWarning: (key) => manualWarnings.update(current => {
991
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
992
+ const { [key]: _removed, ...rest } = current;
993
+ return rest;
994
+ }),
995
+ clearWarnings: () => manualWarnings.set({}),
996
+ };
997
+ }
998
+
999
+ /**
1000
+ * Regular expression for email validation.
1001
+ *
1002
+ * Aligned with Angular's built-in email validator. Validates standard email formats
1003
+ * with proper domain and local parts.
1004
+ *
1005
+ * Pattern requirements:
1006
+ * - Total length: 1-254 characters
1007
+ * - Local part (before @): 1-64 characters
1008
+ * - Allows alphanumeric and special characters: !#$%&'*+/=?^_`{|}~-
1009
+ * - Domain must have valid format with optional subdomains
1010
+ */
1011
+ const EMAIL_REGEXP = /^(?=.{1,254}$)(?=.{1,64}@)[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
1012
+ /**
1013
+ * Determines if a value is considered empty for validation purposes.
1014
+ *
1015
+ * Checks various types:
1016
+ * - null/undefined: always empty
1017
+ * - Numbers: empty when equals 0
1018
+ * - Strings/Arrays: empty when length is 0
1019
+ * - Sets: empty when size is 0
1020
+ *
1021
+ * @param value - Value to check for emptiness
1022
+ * @returns True if the value is considered empty
1023
+ *
1024
+ * @example
1025
+ * ```typescript
1026
+ * isEmptyInputValue(null); // true
1027
+ * isEmptyInputValue(''); // true
1028
+ * isEmptyInputValue(0); // true
1029
+ * isEmptyInputValue('hello'); // false
1030
+ * ```
1031
+ */
1032
+ function isEmptyInputValue(value) {
1033
+ if (value === null || value === undefined)
1034
+ return true;
1035
+ // TODO 1: check if number==0 is empty or number.length==0 is empty
1036
+ if (typeof value === 'number' /* or - isNumber(value) */) {
1037
+ return value == 0;
1038
+ }
1039
+ if (Array.isArray(value) || typeof value === 'string') {
1040
+ return value.length === 0;
1041
+ }
1042
+ if (value instanceof Set) {
1043
+ return value.size === 0;
1044
+ }
1045
+ return false;
1046
+ }
1047
+ /**
1048
+ * Type guard that checks if a value can be coerced to a valid number.
1049
+ *
1050
+ * Uses JavaScript's Number coercion and checks for NaN to determine
1051
+ * if a value represents a valid numeric value.
1052
+ *
1053
+ * @param value - Value to check
1054
+ * @returns True if value is or can be coerced to a number
1055
+ *
1056
+ * @example
1057
+ * ```typescript
1058
+ * isNumber(42); // true
1059
+ * isNumber('42'); // true
1060
+ * isNumber('hello'); // false
1061
+ * ```
1062
+ */
1063
+ function isNumber(value) {
1064
+ return !Number.isNaN(Number(value));
1065
+ }
1066
+ /**
1067
+ * Validator that requires a non-empty value.
1068
+ *
1069
+ * Fails when the value is null, undefined, empty string, empty array,
1070
+ * zero (for numbers), or empty Set.
1071
+ *
1072
+ * @param ctx - Validation context with the value to check
1073
+ * @returns Error object with 'required' key if validation fails, null otherwise
1074
+ *
1075
+ * @example
1076
+ * ```typescript
1077
+ * const control = signalForm({ name: { value: '', validators: [signalFormValidators.required] } });
1078
+ * // control.controls.name.errors() => { required: 'This field is required' }
1079
+ * ```
1080
+ */
1081
+ const required = ({ item: { value } }) => isEmptyInputValue(value) ? { required: 'This field is required' } : null;
1082
+ /**
1083
+ * Validator that enforces a maximum string or number length.
1084
+ *
1085
+ * Converts the value to string and checks if its length exceeds the specified maximum.
1086
+ * Works with both string and number types.
1087
+ *
1088
+ * @param num - Maximum allowed length (inclusive)
1089
+ * @returns Validator function
1090
+ *
1091
+ * @example
1092
+ * ```typescript
1093
+ * const control = signalForm({
1094
+ * username: { value: 'verylongusername', validators: [signalFormValidators.maxLength(10)] }
1095
+ * });
1096
+ * // control.controls.username.errors() => { maxLength: 'To long' }
1097
+ * ```
1098
+ */
1099
+ function maxLength(num) {
1100
+ return ({ item: { value } }) => (typeof value == 'string' || typeof value == 'number') &&
1101
+ value.toString().length > num
1102
+ ? { maxLength: 'To long' }
1103
+ : null;
1104
+ }
1105
+ /**
1106
+ * Validator that enforces a minimum string or number length.
1107
+ *
1108
+ * Converts the value to string and checks if its length is less than or equal to the specified minimum.
1109
+ * Works with both string and number types.
1110
+ *
1111
+ * @param num - Minimum required length (inclusive)
1112
+ * @returns Validator function
1113
+ *
1114
+ * @example
1115
+ * ```typescript
1116
+ * const control = signalForm({
1117
+ * code: { value: 'ab', validators: [signalFormValidators.minLength(3)] }
1118
+ * });
1119
+ * // control.controls.code.errors() => { minLength: 'To short' }
1120
+ * ```
1121
+ */
1122
+ function minLength(num) {
1123
+ return ({ item: { value } }) => (typeof value == 'string' || typeof value == 'number') &&
1124
+ value.toString().length <= num
1125
+ ? { minLength: 'To short' }
1126
+ : null;
1127
+ }
1128
+ /**
1129
+ * Validator that enforces a minimum numeric value.
1130
+ *
1131
+ * Checks if a numeric value is less than or equal to the specified minimum.
1132
+ * Value is coerced to a number for comparison.
1133
+ *
1134
+ * @param num - Minimum allowed value (exclusive - value must be greater than this)
1135
+ * @returns Validator function
1136
+ *
1137
+ * @example
1138
+ * ```typescript
1139
+ * const control = signalForm({
1140
+ * age: { value: -5, validators: [signalFormValidators.min(0)] }
1141
+ * });
1142
+ * // control.controls.age.errors() => { minLength: 'To small' }
1143
+ * ```
1144
+ */
1145
+ function min(num) {
1146
+ return ({ item: { value } }) => isNumber(value) && value <= num ? { minLength: 'To small' } : null;
1147
+ }
1148
+ /**
1149
+ * Validator that enforces a maximum numeric value.
1150
+ *
1151
+ * Checks if a numeric value exceeds the specified maximum.
1152
+ * Value is coerced to a number for comparison.
1153
+ *
1154
+ * @param num - Maximum allowed value (inclusive)
1155
+ * @returns Validator function
1156
+ *
1157
+ * @example
1158
+ * ```typescript
1159
+ * const control = signalForm({
1160
+ * age: { value: 150, validators: [signalFormValidators.max(120)] }
1161
+ * });
1162
+ * // control.controls.age.errors() => { minLength: 'To big' }
1163
+ * ```
1164
+ */
1165
+ function max(num) {
1166
+ return ({ item: { value } }) => isNumber(value) && value > num ? { minLength: 'To big' } : null;
1167
+ }
1168
+ /**
1169
+ * Validator that checks if a value is a valid email address.
1170
+ *
1171
+ * Uses Angular-compatible email regex pattern to validate email format.
1172
+ * Skips validation for empty values (use with `required` if needed).
1173
+ *
1174
+ * @param ctx - Validation context with the email value to check
1175
+ * @returns Error object with 'email' key if validation fails, null otherwise
1176
+ *
1177
+ * @example
1178
+ * ```typescript
1179
+ * const control = signalForm({
1180
+ * email: { value: 'invalid-email', validators: [signalFormValidators.email] }
1181
+ * });
1182
+ * // control.controls.email.errors() => { email: 'Invalid email' }
1183
+ * ```
1184
+ */
1185
+ const email = ({ item: { value } }) => {
1186
+ if (isEmptyInputValue(value))
1187
+ return null;
1188
+ return EMAIL_REGEXP.test(String(value)) ? null : { email: 'Invalid email' };
1189
+ };
1190
+ /**
1191
+ * Validator that checks if a value matches a specified regular expression pattern.
1192
+ *
1193
+ * Accepts either a RegExp object or a string pattern. String patterns are automatically
1194
+ * wrapped with ^ and $ anchors to match the entire value.
1195
+ *
1196
+ * Skips validation for empty values (use with `required` if needed).
1197
+ *
1198
+ * @param valuePattern - Regular expression or pattern string to match against
1199
+ * @returns Validator function
1200
+ *
1201
+ * @example
1202
+ * ```typescript
1203
+ * // Using regex
1204
+ * const control1 = signalForm({
1205
+ * code: { value: 'abc', validators: [signalFormValidators.pattern(/^[0-9]+$/)] }
1206
+ * });
1207
+ * // control1.controls.code.errors() => { pattern: 'RequiredPattern: ^[0-9]+$, ActualValue: abc' }
1208
+ *
1209
+ * // Using string pattern
1210
+ * const control2 = signalForm({
1211
+ * zipCode: { value: 'ABC', validators: [signalFormValidators.pattern('[0-9]{5}')] }
1212
+ * });
1213
+ * ```
1214
+ */
1215
+ function pattern(valuePattern) {
1216
+ if (!valuePattern)
1217
+ return () => null;
1218
+ let regex;
1219
+ let regexStr;
1220
+ if (typeof valuePattern === 'string') {
1221
+ regexStr = '';
1222
+ if (valuePattern.charAt(0) !== '^')
1223
+ regexStr += '^';
1224
+ regexStr += valuePattern;
1225
+ if (valuePattern.charAt(valuePattern.length - 1) !== '$')
1226
+ regexStr += '$';
1227
+ regex = new RegExp(regexStr);
1228
+ }
1229
+ else {
1230
+ regexStr = valuePattern.toString();
1231
+ regex = valuePattern;
1232
+ }
1233
+ return ({ item: { value } }) => {
1234
+ if (isEmptyInputValue(value))
1235
+ return null;
1236
+ const valueStr = String(value);
1237
+ return regex.test(valueStr)
1238
+ ? null
1239
+ : {
1240
+ pattern: `RequiredPattern: ${regexStr}, ActualValue: ${valueStr}`,
1241
+ };
1242
+ };
1243
+ }
1244
+ /**
1245
+ * Collection of built-in validators for signal-form controls.
1246
+ *
1247
+ * Provides common validation functions that can be used in the `validators` or `warnings`
1248
+ * arrays of form controls. All validators skip empty values except `required`.
1249
+ *
1250
+ * @property required - Ensures the value is not empty (null, undefined, '', [], 0, empty Set)
1251
+ * @property maxLength - Ensures string/number length doesn't exceed maximum
1252
+ * @property minLength - Ensures string/number length meets minimum requirement
1253
+ * @property min - Ensures numeric value is greater than minimum (exclusive)
1254
+ * @property max - Ensures numeric value doesn't exceed maximum (inclusive)
1255
+ * @property email - Validates email address format using Angular-compatible regex
1256
+ * @property pattern - Validates value matches a regular expression pattern
1257
+ *
1258
+ * @example
1259
+ * ```typescript
1260
+ * const form = signalForm({
1261
+ * email: {
1262
+ * value: '',
1263
+ * validators: [signalFormValidators.required, signalFormValidators.email]
1264
+ * },
1265
+ * age: {
1266
+ * value: 25,
1267
+ * validators: [signalFormValidators.min(0), signalFormValidators.max(120)],
1268
+ * warnings: [signalFormValidators.max(100)] // Warning but doesn't invalidate
1269
+ * },
1270
+ * username: {
1271
+ * value: '',
1272
+ * validators: [
1273
+ * signalFormValidators.required,
1274
+ * signalFormValidators.minLength(3),
1275
+ * signalFormValidators.maxLength(20),
1276
+ * signalFormValidators.pattern(/^[a-zA-Z0-9_]+$/)
1277
+ * ]
1278
+ * }
1279
+ * });
1280
+ * ```
1281
+ */
1282
+ const signalFormValidators = {
1283
+ required,
1284
+ maxLength,
1285
+ minLength,
1286
+ min,
1287
+ max,
1288
+ email,
1289
+ pattern,
1290
+ };
1291
+
1292
+ /**
1293
+ * Example demonstrating the usage of signal-based reactive forms.
1294
+ *
1295
+ * Shows various features including:
1296
+ * - Simple value initialization
1297
+ * - Validators that mark controls as invalid
1298
+ * - Warnings that don't affect validity
1299
+ * - Dynamic disabled state based on other controls
1300
+ * - Nested objects and arrays
1301
+ * - Type-safe control access
1302
+ *
1303
+ * This function is exported as an example and reference implementation.
1304
+ * It's not meant to be called in production code.
1305
+ *
1306
+ * @example
1307
+ * ```typescript
1308
+ * // Basic usage pattern from the example
1309
+ * const form = signalForm<Person>({
1310
+ * name: 'dvirus',
1311
+ * age: {
1312
+ * value: 130,
1313
+ * validators: [signalFormValidators.required, signalFormValidators.min(0)],
1314
+ * warnings: [signalFormValidators.max(120)],
1315
+ * disabled: (ctx) => ctx.getControl('name').value() === 'admin'
1316
+ * },
1317
+ * address: {
1318
+ * street: { value: '123 Main St', validators: [signalFormValidators.required] }
1319
+ * },
1320
+ * hobbies: [
1321
+ * { value: 'coding', validators: [signalFormValidators.minLength(3)] },
1322
+ * 'programming'
1323
+ * ]
1324
+ * });
1325
+ *
1326
+ * // Access form state
1327
+ * form.getControl('address').value(); // { street: '123 Main St' }
1328
+ * form.controls.hobbies.controls()[0].errors(); // {}
1329
+ * form.controls.age.firstErrorOrWarning(); // { name: 'max', message: 'To big', type: 'warning' }
1330
+ * ```
1331
+ */
1332
+ // function main() {
1333
+ // type Person = {
1334
+ // name: string;
1335
+ // age: number;
1336
+ // address: {
1337
+ // street: string;
1338
+ // city: string;
1339
+ // };
1340
+ // hobbies: string[];
1341
+ // };
1342
+ // /**
1343
+ // * Example 1: Simple form with direct value initialization.
1344
+ // *
1345
+ // * Creates a form from a plain object. Each property becomes
1346
+ // * a control with the provided value.
1347
+ // */
1348
+ // const model1: Person = {
1349
+ // name: 'dvirus',
1350
+ // age: 30,
1351
+ // address: {
1352
+ // street: '123 Main St',
1353
+ // city: 'Any-town',
1354
+ // },
1355
+ // hobbies: ['coding', 'gaming'],
1356
+ // };
1357
+ // const form1 = signalForm(model1);
1358
+ // /**
1359
+ // * Example 2: Advanced form with validators, warnings, and dynamic disabled state.
1360
+ // *
1361
+ // * Demonstrates:
1362
+ // * - Simple value for name field
1363
+ // * - Age control with validators (required, min) and warnings (max)
1364
+ // * - Dynamic disabled logic based on name value
1365
+ // * - Nested address object with validated street field
1366
+ // * - Array of hobbies with mixed control configs and simple values
1367
+ // */
1368
+ // const form2 = signalForm<Person>({
1369
+ // name: 'dvirus',
1370
+ // age: {
1371
+ // // initial value
1372
+ // value: 130,
1373
+ // // example of validators, they add error messages and mark the control as invalid if the validation fails
1374
+ // validators: [signalFormValidators.required, signalFormValidators.min(0)],
1375
+ // // warning are like validators but they don't make the control invalid, just add a warning message
1376
+ // warnings: [signalFormValidators.max(120)],
1377
+ // // example of dynamic disabling based on another control's value
1378
+ // disabled: (ctx) => ctx.getControl('name').value() === 'admin',
1379
+ // },
1380
+ // address: {
1381
+ // street: {
1382
+ // value: '123 Main St',
1383
+ // validators: [signalFormValidators.required],
1384
+ // disabled: false,
1385
+ // },
1386
+ // },
1387
+ // hobbies: [
1388
+ // {
1389
+ // value: 'coding',
1390
+ // validators: [signalFormValidators.minLength(3)],
1391
+ // disabled: computed(() => false),
1392
+ // },
1393
+ // 'programming',
1394
+ // 'Typing',
1395
+ // ],
1396
+ // });
1397
+ // // Example outputs demonstrating form access patterns
1398
+ // console.log(form2.getControl('address').value()); // { street: '123 Main St' }
1399
+ // console.log(form2.controls.hobbies.controls()[0].errors()); // {}
1400
+ // console.log(form2.controls.age.firstErrorOrWarning()); // {name: 'minLength', message: 'To big', type: 'warning'}
1401
+ // console.log(form2.controls.age.warnings()); // { minLength: 'To big' }
1402
+ // }
1403
+
1404
+ /**
1405
+ * Generated bundle index. Do not edit.
1406
+ */
1407
+
1408
+ export { createSignalFormArray, createSignalFormControl, createSignalFormGroup, formArray, formControl, formGroup, isSignalFormArray, isSignalFormControl, isSignalFormGroup, signalForm, signalFormValidators };
1409
+ //# sourceMappingURL=dvirus-js-angular-signals-signal-form.mjs.map