-
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
- Loading branch information
Showing
8 changed files
with
292 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>Reproduce Step</h1> | ||
<ol> | ||
<li>Select ECS, Public signed radio is automatically selected. Click Signature (empty textarea).</li> | ||
<li style="color: red"> | ||
When switching platform dropdown to Cloudian, signature textarea has value, but its successful state is not updated. | ||
clr-success not show | ||
</li> | ||
</ol> | ||
|
||
<form clrForm [formGroup]="form"> | ||
<clr-select-container> | ||
<label>{{ 'CHOOSE_PLATFORM' }}</label> | ||
<select class="w-60" clrSelect formControlName="platform"> | ||
<option value="CLOUDIAN">CLOUDIAN</option> | ||
<option value="ECS">ECS</option> | ||
</select> | ||
<clr-control-error> {{ 'REQUIRED' }} </clr-control-error> | ||
</clr-select-container> | ||
|
||
<clr-input-container> | ||
<label>{{ 'S3_URL' }}</label> | ||
<input clrInput type="text" [formControl]="$any(form)?.get('s3')?.get('url')" /> | ||
<clr-control-error>{{ 'INVALID_HTTPS_DESC' }}</clr-control-error> | ||
</clr-input-container> | ||
|
||
<clr-radio-container clrInline> | ||
<label>{{ 'CERTIFICATE_VALIDATION' }}</label> | ||
<clr-radio-wrapper> | ||
<input type="radio" clrRadio [name]="'s3-cert-validate'" value="tlsSignature" [formControl]="certValidation" /> | ||
<label>{{ 'SIGNATURE' }}</label> | ||
</clr-radio-wrapper> | ||
<clr-radio-wrapper> | ||
<input type="radio" clrRadio [name]="'s3-cert-validate'" value="publicSigned" [formControl]="certValidation" /> | ||
<label>{{ 'PUBLIC_SIGNED' }}</label> | ||
</clr-radio-wrapper> | ||
<clr-control-error> {{ 'REQUIRED' }} </clr-control-error> | ||
</clr-radio-container> | ||
|
||
<clr-textarea-container *ngIf="certValidation.value === 'tlsSignature'"> | ||
<label>{{ 'SIGNATURE' }}</label> | ||
<textarea clrTextarea [formControl]="$any(form).get('s3').get('tlsSignature')"></textarea> | ||
<clr-control-helper>{{ 'SIGNATURE_DESC' }}</clr-control-helper> | ||
<clr-control-success>I should show when successful!!!</clr-control-success> | ||
<clr-control-error> {{ 'REQUIRED' }} </clr-control-error> | ||
</clr-textarea-container> | ||
</form> |
131 changes: 131 additions & 0 deletions
131
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,131 @@ | ||
/* | ||
* 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 { Component, inject } from '@angular/core'; | ||
import { FormControl, UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms'; | ||
|
||
@Component({ | ||
selector: 'dynamic-controls-demo', | ||
templateUrl: './dynamic-controls.demo.html', | ||
}) | ||
export class DynamicControlsDemo { | ||
// radio control | ||
readonly certValidation = new FormControl<string | null>(null, Validators.required); | ||
|
||
fb = inject(UntypedFormBuilder); | ||
|
||
form: UntypedFormGroup = this.fb.group({ | ||
platform: new FormControl<string | null>(null, [Validators.required]), | ||
}); | ||
|
||
ngOnInit() { | ||
this.registerPlatformChange(); | ||
|
||
this.registerCertValidateChange(); | ||
} | ||
|
||
private registerPlatformChange() { | ||
this.form.controls.platform.valueChanges.subscribe(value => { | ||
this.form.removeControl('s3'); | ||
this.form.removeControl('iam'); | ||
this.form.removeControl('console'); | ||
|
||
switch (value) { | ||
case 'CLOUDIAN': | ||
this.addControlsForCloudian(); | ||
break; | ||
case 'ECS': | ||
this.addControlsForECS(); | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
this.setCertValidationValue(); | ||
}); | ||
} | ||
|
||
private addControlsForCloudian() { | ||
this.addS3Control('cloudian signature'); | ||
this.addIAMControl(); | ||
} | ||
|
||
private addControlsForECS() { | ||
this.addS3Control(); | ||
this.addConsoleControl(); | ||
} | ||
|
||
private addS3Control(signature?: string) { | ||
this.form.addControl( | ||
's3', | ||
this.fb.group({ | ||
url: this.fb.control('', [Validators.required]), | ||
tlsSignature: this.fb.control( | ||
{ | ||
value: signature, | ||
disabled: true, | ||
}, | ||
Validators.required | ||
), | ||
}) | ||
); | ||
} | ||
|
||
private addIAMControl() { | ||
this.form.addControl( | ||
'iam', | ||
this.fb.group({ | ||
url: this.fb.control('', [Validators.required]), | ||
tlsSignature: this.fb.control( | ||
{ | ||
value: '', | ||
disabled: true, | ||
}, | ||
Validators.required | ||
), | ||
}) | ||
); | ||
} | ||
|
||
private addConsoleControl() { | ||
this.form.addControl( | ||
'console', | ||
this.fb.group({ | ||
url: ['', [Validators.required]], | ||
secretKey: ['', [Validators.required]], | ||
}) | ||
); | ||
} | ||
|
||
// set radio based on form value | ||
private setCertValidationValue() { | ||
const signatureControl = this.form.controls['s3'].get('tlsSignature') as FormControl; | ||
|
||
const value = signatureControl.value ? 'tlsSignature' : 'publicSigned'; | ||
|
||
this.certValidation.setValue(value); | ||
} | ||
|
||
private registerCertValidateChange() { | ||
this.certValidation.valueChanges.subscribe(value => { | ||
const tlsSignature = this.form.controls['s3'].get('tlsSignature') as FormControl; | ||
|
||
if (value === 'tlsSignature') { | ||
tlsSignature.enable({ emitEvent: false }); | ||
} else { | ||
// public signed | ||
tlsSignature.disable({ emitEvent: false }); | ||
} | ||
|
||
setTimeout(() => { | ||
if (tlsSignature.value) { | ||
tlsSignature.markAsTouched(); | ||
tlsSignature.updateValueAndValidity(); | ||
} | ||
}); | ||
}); | ||
} | ||
} |
Oops, something went wrong.