angular / angular/angular

Allow setting enabled/disabled state of AbstractControl using a boolean value

Offen
#47,916 3 Kommentare 7 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
area: forms P3
Vorherrschende Sprache
TypeScript
Sterne
101k
Forks
27.5k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
288

Beschreibung

### Which @angular/* package(s) are relevant/related to the feature request?

_No response_

### Description

Currently, in order to disable or enable an `AbstractControl` depending on another control's value or state, one has to use very verbose explicit code using both if/else branches of a condition:

```ts
.subscribe(value => {
if(valueMeetsCondition(value)){
this.ctrl.enable()
} else {
this.ctrl.disable()
}
});
```
or its ternary equivalent.

It would simplify the code if we could say this instead:

```ts
.subscribe(value => this.ctrl.setEnabledState(valueMeetsCondition(value)));
```

### Proposed solution

In [AbstractControl API](https://angular.io/api/forms/AbstractControl), add a pair of methods that accept a boolean value:

- `setDisabledState`

```ts
setDisabledState(isDisabled: boolean, opts: { onlySelf?: boolean; emitEvent?: boolean; } = {}): void
```

- `setEnabledState`

```ts
setEnabledState(isEnabled: boolean, opts: { onlySelf?: boolean; emitEvent?: boolean; } = {}): void
```

I think it's important to have a pair of companions, just like we have `valid`/`invalid`, `enabled`/`disabled` property pairs on [AbstractControl](https://angular.io/api/forms/AbstractControl#abstractcontrol) for the sake of coding convenience.

It would allow developers for a smoother migration and cleaner code without the need of negating the conditions in their existing code.

### Alternatives considered

Workarounds? Described above which I want to stop using.
Alternative solutions? Can't think of any.

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Die Änderung zielt auf die Angular's AbstractControl API ab. Beginne damit, die vorhandenen enable- und disable-Methoden zu finden und ihre Optionen zu überprüfen. Als abgeschlossen gilt die Arbeit, wenn boolean setEnabledState- und setDisabledState-Methoden mit den vorgeschlagenen Optionen verfügbar sind und die in der Issue beschriebene bedingte Verwendung abdecken.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
angular, typescript
Bereich
api, frontend
Issue-Typ
Feature
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.