mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
refactor(forms): use overloads and JSDoc for deprecations
This commit removes runtime console warnings and uses TypeScript overloads with JSDoc @deprecated annotations to handle backward compatibility for conditional rules.
This commit is contained in:
parent
3f3b85ca44
commit
0806b2f02b
4 changed files with 59 additions and 45 deletions
|
|
@ -15,7 +15,7 @@ import type {FieldContext, LogicFn, PathKind, SchemaPath, SchemaPathRules} from
|
|||
* validation, touched/dirty, or other state of its parent field.
|
||||
*
|
||||
* @param path The target path to add the disabled logic to.
|
||||
* @param configOrLogic Optional configuration object containing `when`, or the logic directly (deprecated).
|
||||
* @param config Optional configuration object.
|
||||
* - `when`: A reactive function that returns `true` (or a string reason) when the field is disabled,
|
||||
* and `false` when it is not disabled. Can also be a static string reason.
|
||||
* @template TValue The type of value stored in the field the logic is bound to.
|
||||
|
|
@ -24,6 +24,21 @@ import type {FieldContext, LogicFn, PathKind, SchemaPath, SchemaPathRules} from
|
|||
* @category logic
|
||||
* @publicApi 22.0
|
||||
*/
|
||||
export function disabled<TValue, TPathKind extends PathKind = PathKind.Root>(
|
||||
path: SchemaPath<TValue, SchemaPathRules.Supported, TPathKind>,
|
||||
config?: {when?: string | NoInfer<LogicFn<TValue, boolean | string, TPathKind>>},
|
||||
): void;
|
||||
|
||||
/**
|
||||
* Adds logic to a field to conditionally disable it.
|
||||
*
|
||||
* @deprecated Passing a function or string directly to `disabled` is deprecated. Use `{ when: ... }` instead.
|
||||
*/
|
||||
export function disabled<TValue, TPathKind extends PathKind = PathKind.Root>(
|
||||
path: SchemaPath<TValue, SchemaPathRules.Supported, TPathKind>,
|
||||
logic?: string | NoInfer<LogicFn<TValue, boolean | string, TPathKind>>,
|
||||
): void;
|
||||
|
||||
export function disabled<TValue, TPathKind extends PathKind = PathKind.Root>(
|
||||
path: SchemaPath<TValue, SchemaPathRules.Supported, TPathKind>,
|
||||
configOrLogic?:
|
||||
|
|
@ -38,15 +53,8 @@ export function disabled<TValue, TPathKind extends PathKind = PathKind.Root>(
|
|||
let logic: string | LogicFn<TValue, boolean | string, TPathKind> | undefined;
|
||||
if (typeof configOrLogic === 'function' || typeof configOrLogic === 'string') {
|
||||
logic = configOrLogic;
|
||||
if (typeof ngDevMode === 'undefined' || ngDevMode) {
|
||||
console.warn(
|
||||
`[Signal Forms] Passing a function or string directly to 'disabled' is deprecated. Use '{ when: ... }' instead.`,
|
||||
);
|
||||
}
|
||||
} else if (configOrLogic !== undefined) {
|
||||
logic = configOrLogic.when;
|
||||
} else {
|
||||
logic = undefined;
|
||||
logic = configOrLogic?.when;
|
||||
}
|
||||
|
||||
pathNode.builder.addDisabledReasonRule((ctx) => {
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ import type {LogicFn, PathKind, SchemaPath, SchemaPathRules} from '../types';
|
|||
* ```
|
||||
*
|
||||
* @param path The target path to add the hidden logic to.
|
||||
* @param configOrLogic Options object containing the `when` condition, or the logic function directly (deprecated).
|
||||
* @param config Options object containing the `when` condition.
|
||||
* - `when`: A reactive function that returns `true` when the field is hidden.
|
||||
* @template TValue The type of value stored in the field the logic is bound to.
|
||||
* @template TPathKind The kind of path the logic is bound to (a root path, child path, or item of an array)
|
||||
|
|
@ -31,6 +31,21 @@ import type {LogicFn, PathKind, SchemaPath, SchemaPathRules} from '../types';
|
|||
* @category logic
|
||||
* @publicApi 22.0
|
||||
*/
|
||||
export function hidden<TValue, TPathKind extends PathKind = PathKind.Root>(
|
||||
path: SchemaPath<TValue, SchemaPathRules.Supported, TPathKind>,
|
||||
config: {when: NoInfer<LogicFn<TValue, boolean, TPathKind>>},
|
||||
): void;
|
||||
|
||||
/**
|
||||
* Adds logic to a field to conditionally hide it.
|
||||
*
|
||||
* @deprecated Passing a function directly to `hidden` is deprecated. Use `{ when: ... }` instead.
|
||||
*/
|
||||
export function hidden<TValue, TPathKind extends PathKind = PathKind.Root>(
|
||||
path: SchemaPath<TValue, SchemaPathRules.Supported, TPathKind>,
|
||||
logic: NoInfer<LogicFn<TValue, boolean, TPathKind>>,
|
||||
): void;
|
||||
|
||||
export function hidden<TValue, TPathKind extends PathKind = PathKind.Root>(
|
||||
path: SchemaPath<TValue, SchemaPathRules.Supported, TPathKind>,
|
||||
configOrLogic:
|
||||
|
|
@ -41,17 +56,7 @@ export function hidden<TValue, TPathKind extends PathKind = PathKind.Root>(
|
|||
|
||||
const pathNode = FieldPathNode.unwrapFieldPath(path);
|
||||
|
||||
let logic: LogicFn<TValue, boolean, TPathKind>;
|
||||
if (typeof configOrLogic === 'function') {
|
||||
logic = configOrLogic;
|
||||
if (typeof ngDevMode === 'undefined' || ngDevMode) {
|
||||
console.warn(
|
||||
`[Signal Forms] Passing a function directly to 'hidden' is deprecated. Use '{ when: ... }' instead.`,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
logic = configOrLogic.when;
|
||||
}
|
||||
const logic = typeof configOrLogic === 'function' ? configOrLogic : configOrLogic.when;
|
||||
|
||||
pathNode.builder.addHiddenRule(logic);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ import type {LogicFn, PathKind, SchemaPath, SchemaPathRules} from '../types';
|
|||
* the validation, touched/dirty, or other state of its parent field.
|
||||
*
|
||||
* @param path The target path to make readonly.
|
||||
* @param configOrLogic Optional configuration object containing `when`, or the logic directly (deprecated).
|
||||
* @param config Optional configuration object.
|
||||
* - `when`: A reactive function that returns `true` when the field is readonly.
|
||||
* @template TValue The type of value stored in the field the logic is bound to.
|
||||
* @template TPathKind The kind of path the logic is bound to (a root path, child path, or item of an array)
|
||||
|
|
@ -23,6 +23,21 @@ import type {LogicFn, PathKind, SchemaPath, SchemaPathRules} from '../types';
|
|||
* @category logic
|
||||
* @publicApi 22.0
|
||||
*/
|
||||
export function readonly<TValue, TPathKind extends PathKind = PathKind.Root>(
|
||||
path: SchemaPath<TValue, SchemaPathRules.Supported, TPathKind>,
|
||||
config?: {when?: NoInfer<LogicFn<TValue, boolean, TPathKind>>},
|
||||
): void;
|
||||
|
||||
/**
|
||||
* Adds logic to a field to conditionally make it readonly.
|
||||
*
|
||||
* @deprecated Passing a function directly to `readonly` is deprecated. Use `{ when: ... }` instead.
|
||||
*/
|
||||
export function readonly<TValue, TPathKind extends PathKind = PathKind.Root>(
|
||||
path: SchemaPath<TValue, SchemaPathRules.Supported, TPathKind>,
|
||||
logic?: NoInfer<LogicFn<TValue, boolean, TPathKind>>,
|
||||
): void;
|
||||
|
||||
export function readonly<TValue, TPathKind extends PathKind = PathKind.Root>(
|
||||
path: SchemaPath<TValue, SchemaPathRules.Supported, TPathKind>,
|
||||
configOrLogic?:
|
||||
|
|
@ -38,11 +53,6 @@ export function readonly<TValue, TPathKind extends PathKind = PathKind.Root>(
|
|||
logic = configOrLogic.when ?? (() => true);
|
||||
} else if (typeof configOrLogic === 'function') {
|
||||
logic = configOrLogic;
|
||||
if (typeof ngDevMode === 'undefined' || ngDevMode) {
|
||||
console.warn(
|
||||
`[Signal Forms] Passing a function directly to 'readonly' is deprecated. Use '{ when: ... }' instead.`,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
logic = () => true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -128,24 +128,15 @@ describe('hidden', () => {
|
|||
|
||||
it('supports deprecated function syntax', () => {
|
||||
const cat = signal({name: 'Pirojok-the-cat', age: 5});
|
||||
const spy = jasmine.createSpy('console.warn');
|
||||
const originalWarn = console.warn;
|
||||
console.warn = spy;
|
||||
const f = form(
|
||||
cat,
|
||||
(p) => {
|
||||
hidden(p.name, ((ctx: any) => ctx.value() === 'hidden-cat') as any);
|
||||
},
|
||||
{injector: TestBed.inject(Injector)},
|
||||
);
|
||||
|
||||
try {
|
||||
const f = form(
|
||||
cat,
|
||||
(p) => {
|
||||
hidden(p.name, ((ctx: any) => ctx.value() === 'hidden-cat') as any);
|
||||
},
|
||||
{injector: TestBed.inject(Injector)},
|
||||
);
|
||||
|
||||
f.name().value.set('hidden-cat');
|
||||
expect(f.name().hidden()).toBe(true);
|
||||
expect(spy).toHaveBeenCalled();
|
||||
} finally {
|
||||
console.warn = originalWarn;
|
||||
}
|
||||
f.name().value.set('hidden-cat');
|
||||
expect(f.name().hidden()).toBe(true);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue