Allow setting enabled/disabled state of AbstractControl using a boolean value
- Lingua principale
- TypeScript
- Stelle
- 101k
- Fork
- 27.5k
- Merge medio
- 1g 19h
- PR unite (30g)
- 288
Descrizione
### 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.
Guida per i contributori
Apri la guida per i contributori
Direzione di ricerca
La modifica riguarda l'API AbstractControl di Angular; inizia individuando i metodi enable e disable esistenti e verificandone le opzioni. Il lavoro è completato quando sono disponibili i metodi boolean setEnabledState e setDisabledState con le opzioni proposte e coprono l'utilizzo condizionale descritto nell'issue.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- angular, typescript
- Ambito
- api, frontend
- Tipo di issue
- Funzionalità
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 45/100