mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
This commit introduces an experimental version of a new signal-based forms API for Angular. This new API aims to explore how signals can be leveraged to create a more declarative, intuitive, and reactive way of handling forms. The primary goals of this new signal-based approach are: * **Signal-centric Design:** Place signals at the core of the forms experience, enabling a truly reactive programming model for form state and logic. * **Declarative Logic:** Allow developers to define form behavior, such as validation and conditional fields, declaratively using TypeScript. This moves logic out of the template and into a typed, testable schema. * **Developer-Owned Data Model:** The library does not maintain a copy of data in a form model, but instead read and write it via a developer-provided `WritableSignal`, eliminating the need for applications to synchronize their data with the form system. * **Interoperability:** A key design goal is seamless interoperability with existing reactive forms, allowing for incremental adoption. * **Bridging Template and Reactive Forms:** This exploration hopes to close the gap between template and reactive forms, offering a unified and more powerful approach that combines the best aspects of both. This initial version of the experimental API includes the core building blocks, such as the `form()` function, `Field` and `FieldState` objects, and a `[control]` directive for binding to UI elements. It also introduces a schema-based system for defining validation, conditional logic, and other form behaviors. Note: This is an early, experimental API. It is not yet complete and is subject to change based on feedback and further exploration. Co-authored-by: Kirill Cherkashin <kirts@google.com> Co-authored-by: Alex Rickabaugh <alxhub@users.noreply.github.com> Co-authored-by: Leon Senft <leonsenft@users.noreply.github.com> Co-authored-by: Dylan Hunn <dylhunn@gmail.com> Co-authored-by: Michael Small <michael-small@users.noreply.github.com> PR Close #63408
54 lines
1.7 KiB
TypeScript
54 lines
1.7 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright Google LLC All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
* found in the LICENSE file at https://angular.dev/license
|
|
*/
|
|
|
|
import {Injector, signal} from '@angular/core';
|
|
import {TestBed} from '@angular/core/testing';
|
|
import {form} from '../../public_api';
|
|
|
|
const noop = () => {};
|
|
|
|
describe('dynamic data patterns', () => {
|
|
it('returns `undefined` for declared fields with an undefined value', () => {
|
|
const model = signal({data: undefined as string | undefined});
|
|
const f = form(model, noop, {injector: TestBed.inject(Injector)});
|
|
expect(f.data).toBe(undefined);
|
|
|
|
// @ts-expect-error: 2722
|
|
expect(() => f.data()).toThrow();
|
|
});
|
|
|
|
it('supports non-null assertions for declared fields with a potentially undefined value', () => {
|
|
const model = signal({data: 'test' as string | undefined});
|
|
const f = form(model, noop, {injector: TestBed.inject(Injector)});
|
|
|
|
expect(f.data).not.toBeUndefined();
|
|
expect(f.data!().value()).toBe('test');
|
|
|
|
// Asserts that the type of `value()` is indeed `string` and excludes `undefined`.
|
|
let value: string = f.data!().value();
|
|
});
|
|
|
|
describe('tracking', () => {
|
|
it('should write to the right key after a move', () => {
|
|
const data = signal([
|
|
{name: 'Alex', counter: 0},
|
|
{name: 'Miles', counter: 0},
|
|
]);
|
|
const f = form(data, {injector: TestBed.inject(Injector)});
|
|
const c0 = f[0].counter();
|
|
// Swap
|
|
data.update(([v0, v1]) => [v1, v0]);
|
|
|
|
c0.value.set(1);
|
|
expect(data()[0].name).toBe('Miles');
|
|
expect(data()[0].counter).toBe(0);
|
|
expect(data()[1].name).toBe('Alex');
|
|
expect(data()[1].counter).toBe(1);
|
|
});
|
|
});
|
|
});
|