Commit Graph

12 Commits

Author SHA1 Message Date
Andrew Kushnir e88a9c6350 perf(forms): make `RadioControlRegistry` class tree-shakable (#41126)
This commit makes the `RadioControlRegistry` class tree-shakable by adding the `providedIn` property to its
`@Injectable` decorator. Now if the radio buttons are not used in the app (thus no `RadioControlValueAccessor`
directive is initialized), the `RadioControlRegistry` should not be included into application's prod bundle.

PR Close #41126
2021-03-16 09:35:14 -07:00
Andrew Kushnir 937e90cd16 perf(forms): make built-in ControlValueAccessors more tree-shakable (#41146)
This commit updates Forms code to avoid direct references to all built-in ControlValueAccessor classes, which
prevents their tree-shaking from production builds. Instead, a new static property is added to all built-in
ControlValueAccessors, which is checked when we need to identify whether a given ControlValueAccessors is a
built-in one.

PR Close #41146
2021-03-12 15:54:59 -08:00
Michael Jerred 4ec045e12b feat(forms): add `emitEvent` option for AbstractControl-based class methods (#31031)
This commit adds the `emitEvent` option to the following FormArray and FormGroup methods:

* FormGroup.addControl
* FormGroup.removeControl
* FormGroup.setControl
* FormArray.push
* FormArray.insert
* FormArray.removeAt
* FormArray.setControl
* FormArray.clear

This option can be used to prevent an event from being emitted when adding or removing controls.

BREAKING CHANGE:

The `emitEvent` option was added to the following `FormArray` and `FormGroup` methods:

* FormGroup.addControl
* FormGroup.removeControl
* FormGroup.setControl
* FormArray.push
* FormArray.insert
* FormArray.removeAt
* FormArray.setControl
* FormArray.clear

If your app has custom classes that extend `FormArray` or `FormGroup` classes and override the 
above-mentioned methods, you may need to update your implementation to take the new options into
account and make sure that overrides are compatible from a types perspective.

Closes #29662.
PR Close #31031
2021-02-16 08:42:08 -08:00
Sonu Kapoor 8fb83ea1b5 feat(forms): introduce min and max validators (#39063)
This commit adds the missing `min` and `max` validators.

BREAKING CHANGE:

Previously `min` and `max` attributes defined on the `<input type="number">`
were ignored by Forms module. Now presence of these attributes would
trigger min/max validation logic (in case `formControl`, `formControlName`
or `ngModel` directives are also present on a given input) and
corresponding form control status would reflect that.

Fixes #16352

PR Close #39063
2021-02-08 09:34:50 -08:00
Andrew Kushnir a3849611b7 fix(forms): clean up connection between FormControl/FormGroup and corresponding directive instances (#39235)
Prior to this commit, removing `FormControlDirective` and `FormGroupName` directive instances didn't clear
the callbacks previously registered on FromControl/FormGroup class instances. As a result, these callbacks
were executed even after `FormControlDirective` and `FormGroupName` directive instances were destroyed. That was
also causing memory leaks since these callbacks also retained references to DOM elements.

This commit updates the cleanup logic to take care of properly detaching FormControl/FormGroup/FormArray instances
from the view by removing view-specific callback at destroy time.

Closes #20007, #37431, #39590.

PR Close #39235
2021-01-05 11:15:08 -08:00
Ryan Russell e148382bd0 docs(forms): Deprecate legacy options for FormBuilder.group (#39769)
DEPRECATION:

Mark the {[key: string]: any} type for the options property of the FormBuilder.group method as deprecated.
Using AbstractControlOptions gives the same functionality and is type-safe.

PR Close #39769
2020-11-25 14:28:11 -08:00
Andrew Kushnir 0723331b2a refactor(forms): move common validators-related logic to the `AbstractControlDirective` class (#38280)
This commit refactors validators-related logic that is common across most of the directives.

A couple notes on this refactoring:
* common logic was moved to the `AbstractControlDirective` class (including `validator` and
`asyncValidator` getters)
* sync/async validators are now composed in `AbstractControlDirective` class eagerly when validators
are set with `_setValidators` and `_setAsyncValidators` calls and the result is stored in directive
instance (thus getters return cached versions of validator fn). This is needed to make sure composed
validator function remains the same (retains its identity) for a given directive instance, so that
this function can be added and later removed from an instance of an AbstractControl-based class
(like `FormControl`). Preserving validator function is required to perform proper cleanup (in followup
PRs) of the AbstractControl-based classes when a directive is destroyed.

PR Close #38280
2020-10-28 09:48:20 -07:00
Andrew Kushnir 246de9aaad fix(forms): improve types of directive constructor arguments (#38944)
Prior to this change, the `validators` and `asyncValidators` fields of a few Forms directives
were typed as `any[]`. This commit updates the types and makes them consistent for all directives
in the Forms package.

BREAKING CHANGE:

Directives in the `@angular/forms` package used to have `any[]` as a type of `validators` and
`asyncValidators` arguments in constructors. Now these arguments are properly typed, so if your
code relies on directive constructor types it may require some updates to improve type safety.

PR Close #38944
2020-10-06 10:14:21 -07:00
lazarljubenovic f4f1bcc997 fix(forms): include null in .parent of abstract control (#32671)
It's perfectly valid for an abstract control not to have a defined parent; yet previously the
types were asserting that AbstractControl#parent is not a null value. This changes correctly
reflects the run-time behavior through the types.

BREAKING CHANGE: Type of AbstractFormControl.parent now includes null

`null` is now included in the types of .parent. If you don't already have a check for this case,
the TypeScript compiler might compain. A v11 migration exists which adds the not-null assertion
operator where necessary.

In an unlikely case your code was testing the parnet against undefined with sitrct equality,
you'll need to change this to `=== null` instead, since the parent is not explicily initialized
with `null` instead of being left `undefined`.

Fixes #16999

PR Close #32671
2020-10-05 09:30:44 -07:00
Ingo Bürk 2b1b7180db fix(forms): type NG_VALUE_ACCESSOR injection token as array (#29723)
NG_VALUE_ACCESSOR is a multi injection token, users can and
should expect more than one ControlValueAccessor to be
available (and this is how it is used in @angular/forms).

This is now reflected in the definition of the injection token
by typing it as an array of ControlValueAccessor. The motivating
reason is that using the programmatic Injector api will now
type Injector#get correspondingly.

fixes #29351

BREAKING CHANGES

NG_VALUE_ACCESSOR is now typed as a readonly array rather than
a mutable scalar. It is used as a multi injection token and as
such it should always be expected that more than one accessor
may be returned.

PR Close #29723
2020-09-21 12:26:07 -07:00
Andrew Kushnir ad7046b934 feat(forms): AbstractControl to store raw validators in addition to combined validators function (#37881)
This commit refactors the way we store validators in AbstractControl-based classes:
in addition to the combined validators function that we have, we also store the original list of validators.
This is needed to have an ability to clean them up later at destroy time (currently it's problematic since
they are combined in a single function).

The change preserves backwards compatibility by making sure public APIs stay the same.
The only public API update is the change to the `AbstractControl` class constructor to extend the set
of possible types that it can accept and process (which should not be breaking).

PR Close #37881
2020-07-21 10:30:19 -07:00
Joey Perrott 15f8afa4bf ci: move public-api goldens to goldens directory (#35768)
Moves the public api .d.ts files from tools/public_api_guard to
goldens/public-api.

Additionally, provides a README in the goldens directory and a script
assist in testing the current state of the repo against the goldens as
well as a command for accepting all changes to the goldens in a single
command.

PR Close #35768
2020-03-10 20:58:39 -04:00