-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(forms): support for dynamic control change (#1074)
- Loading branch information
Showing
8 changed files
with
205 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
projects/demo/src/app/forms/dynamic-controls/dynamic-controls.demo.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<h1>Dynamic Control Demo</h1> | ||
|
||
<p> | ||
We have a State service which have three statuses: NONE, INVALID, VALID. Success and Error messages are shown based on | ||
the value of this state service but if you bind formControl \ ngModel to a control that is currently not available and | ||
then add it dynamically with initial value while marking it as touched and updateValueValidility status change doesn't | ||
trigger and state service value is not updated. | ||
</p> | ||
<br /> | ||
<label for="steps"> Steps to test:</label> | ||
<ul id="steps" style="list-style-type: none"> | ||
<li>1. Choose scenario two ( text area appears but the control binded to formControl is missing )</li> | ||
<li>2. Choose platform two which is adding the control used in the binding with a initial value.</li> | ||
<li>3. Control should be valid and in success state.</li> | ||
</ul> | ||
|
||
<h2>Reactive form</h2> | ||
<form [formGroup]="form"> | ||
<clr-select-container> | ||
<label>Choose Platform</label> | ||
<select class="w-60" clrSelect formControlName="platform"> | ||
<option value="one">One</option> | ||
<option value="two">Two</option> | ||
</select> | ||
<clr-control-error> Required </clr-control-error> | ||
</clr-select-container> | ||
<clr-radio-container clrInline> | ||
<label>Scenario</label> | ||
<clr-radio-wrapper> | ||
<input type="radio" clrRadio [name]="'scenario'" value="one" formControlName="scenario" /> | ||
<label>One</label> | ||
</clr-radio-wrapper> | ||
<clr-radio-wrapper> | ||
<input type="radio" clrRadio [name]="'scenario'" value="two" formControlName="scenario" /> | ||
<label>Two</label> | ||
</clr-radio-wrapper> | ||
<clr-control-error> {{ 'REQUIRED' }} </clr-control-error> | ||
</clr-radio-container> | ||
<clr-textarea-container *ngIf="form.get('scenario')?.value === 'two'"> | ||
<label>Label</label> | ||
<textarea clrTextarea [formControl]="$any(form).get('control')" required></textarea> | ||
<clr-control-helper>Helper Message</clr-control-helper> | ||
<clr-control-success>Successful</clr-control-success> | ||
<clr-control-error>Required error</clr-control-error> | ||
</clr-textarea-container> | ||
</form> |
44 changes: 44 additions & 0 deletions
44
projects/demo/src/app/forms/dynamic-controls/dynamic-controls.demo.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (c) 2016-2023 VMware, Inc. All Rights Reserved. | ||
* This software is released under MIT license. | ||
* The full license information can be found in LICENSE in the root directory of this project. | ||
*/ | ||
|
||
import { ChangeDetectorRef, Component } from '@angular/core'; | ||
import { FormControl, FormGroup } from '@angular/forms'; | ||
import { Subject } from 'rxjs'; | ||
import { filter, takeUntil, tap } from 'rxjs/operators'; | ||
|
||
@Component({ | ||
selector: 'dynamic-controls-demo', | ||
templateUrl: './dynamic-controls.demo.html', | ||
}) | ||
export class DynamicControlsDemo { | ||
destroyed = new Subject(); | ||
form = new FormGroup<any>({ | ||
platform: new FormControl(), | ||
scenario: new FormControl('one'), | ||
}); | ||
|
||
constructor(private cd: ChangeDetectorRef) {} | ||
|
||
ngOnInit() { | ||
this.form | ||
.get('platform') | ||
?.valueChanges.pipe( | ||
filter(value => value === 'two'), | ||
tap(() => { | ||
this.form.addControl('control', new FormControl('test')); | ||
this.form.get('control')?.markAsTouched(); | ||
this.form.get('control')?.updateValueAndValidity(); | ||
}), | ||
takeUntil(this.destroyed) | ||
) | ||
.subscribe(); | ||
} | ||
|
||
ngOnDestroy() { | ||
this.destroyed.next(); | ||
this.destroyed.complete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters