Skip to content

Commit 5be3304

Browse files
authored
refactor(forms): Break logic.ts into separate files
This would make it easier to navigate
1 parent 0bb8924 commit 5be3304

49 files changed

Lines changed: 507 additions & 399 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/forms/signals/compat/src/api/compat_validation_error.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {AbstractControl} from '@angular/forms';
1010
import {FieldTree} from '../../../src/api/types';
11-
import {ValidationError} from '../../../src/api/validation_errors';
11+
import {ValidationError} from '../../../src/api/rules/validation/validation_errors';
1212

1313
/**
1414
* An error used for compat errors.

packages/forms/signals/compat/src/compat_validation_state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {computed, Signal} from '@angular/core';
1010
import {AbstractControl} from '@angular/forms';
11-
import {ValidationError} from '../../src/api/validation_errors';
11+
import {ValidationError} from '../../src/api/rules/validation/validation_errors';
1212
import {calculateValidationSelfStatus, ValidationState} from '../../src/field/validation';
1313
import type {CompatValidationError} from './api/compat_validation_error';
1414
import {getControlStatusSignal} from './compat_field_node';

packages/forms/signals/public_api.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@
1111
* @description
1212
* Entry point for all public APIs of this package.
1313
*/
14-
export * from './src/api/async';
1514
export * from './src/api/control';
16-
export * from './src/api/debounce';
15+
export * from './src/api/rules/debounce';
1716
export * from './src/api/di';
1817
export * from './src/api/field_directive';
19-
export * from './src/api/logic';
20-
export * from './src/api/metadata';
18+
export * from './src/api/rules';
19+
export * from './src/api/rules/metadata';
2120
export * from './src/api/structure';
2221
export * from './src/api/types';
23-
export * from './src/api/validation_errors';
24-
export * from './src/api/validators';
22+
export * from './src/api/rules/validation/validation_errors';

packages/forms/signals/src/api/control.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {InputSignal, InputSignalWithTransform, ModelSignal, OutputRef} from '@angular/core';
1010
import type {DisabledReason} from './types';
11-
import {ValidationError, type WithOptionalField} from './validation_errors';
11+
import {ValidationError, type WithOptionalField} from './rules/validation/validation_errors';
1212

1313
/**
1414
* The base set of properties shared by all form control contracts.

packages/forms/signals/src/api/logic.ts

Lines changed: 0 additions & 241 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import {FieldPathNode} from '../../schema/path_node';
10+
import {assertPathIsCurrent} from '../../schema/schema';
11+
import {AggregateMetadataKey} from './metadata';
12+
import type {SchemaPath, LogicFn, PathKind, SchemaPathRules} from '../types';
13+
14+
/**
15+
* Adds a value to an {@link AggregateMetadataKey} of a field.
16+
*
17+
* @param path The target path to set the aggregate metadata on.
18+
* @param key The aggregate metadata key
19+
* @param logic A function that receives the `FieldContext` and returns a value to add to the aggregate metadata.
20+
* @template TValue The type of value stored in the field the logic is bound to.
21+
* @template TMetadataItem The type of value the metadata aggregates over.
22+
* @template TPathKind The kind of path the logic is bound to (a root path, child path, or item of an array)
23+
*
24+
* @category logic
25+
* @experimental 21.0.0
26+
*/
27+
export function aggregateMetadata<
28+
TValue,
29+
TMetadataItem,
30+
TPathKind extends PathKind = PathKind.Root,
31+
>(
32+
path: SchemaPath<TValue, SchemaPathRules.Supported, TPathKind>,
33+
key: AggregateMetadataKey<any, TMetadataItem>,
34+
logic: NoInfer<LogicFn<TValue, TMetadataItem, TPathKind>>,
35+
): void {
36+
assertPathIsCurrent(path);
37+
38+
const pathNode = FieldPathNode.unwrapFieldPath(path);
39+
pathNode.builder.addAggregateMetadataRule(key, logic);
40+
}

packages/forms/signals/src/api/debounce.ts renamed to packages/forms/signals/src/api/rules/debounce.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {DEBOUNCER} from '../field/debounce';
10-
import {FieldPathNode} from '../schema/path_node';
11-
import {assertPathIsCurrent} from '../schema/schema';
12-
import type {Debouncer, PathKind, SchemaPath, SchemaPathRules} from './types';
9+
import {DEBOUNCER} from '../../field/debounce';
10+
import {FieldPathNode} from '../../schema/path_node';
11+
import {assertPathIsCurrent} from '../../schema/schema';
12+
import type {Debouncer, PathKind, SchemaPath, SchemaPathRules} from '../types';
1313

1414
/**
1515
* Configures the frequency at which a form field is updated by UI events.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import {FieldPathNode} from '../../schema/path_node';
10+
import {assertPathIsCurrent} from '../../schema/schema';
11+
import type {FieldContext, SchemaPath, LogicFn, PathKind, SchemaPathRules} from '../types';
12+
13+
/**
14+
* Adds logic to a field to conditionally disable it. A disabled field does not contribute to the
15+
* validation, touched/dirty, or other state of its parent field.
16+
*
17+
* @param path The target path to add the disabled logic to.
18+
* @param logic A reactive function that returns `true` (or a string reason) when the field is disabled,
19+
* and `false` when it is not disabled.
20+
* @template TValue The type of value stored in the field the logic is bound to.
21+
* @template TPathKind The kind of path the logic is bound to (a root path, child path, or item of an array)
22+
*
23+
* @category logic
24+
* @experimental 21.0.0
25+
*/
26+
export function disabled<TValue, TPathKind extends PathKind = PathKind.Root>(
27+
path: SchemaPath<TValue, SchemaPathRules.Supported, TPathKind>,
28+
logic?: string | NoInfer<LogicFn<TValue, boolean | string, TPathKind>>,
29+
): void {
30+
assertPathIsCurrent(path);
31+
32+
const pathNode = FieldPathNode.unwrapFieldPath(path);
33+
pathNode.builder.addDisabledReasonRule((ctx) => {
34+
let result: boolean | string = true;
35+
if (typeof logic === 'string') {
36+
result = logic;
37+
} else if (logic) {
38+
result = logic(ctx as FieldContext<TValue, TPathKind>);
39+
}
40+
if (typeof result === 'string') {
41+
return {field: ctx.field, message: result};
42+
}
43+
return result ? {field: ctx.field} : undefined;
44+
});
45+
}

0 commit comments

Comments
 (0)