Browse Source

refactor(install-dialog): extract install-form-renderer component (#15615)

The form rendering for the SHOW_FORM step was inline in the dialog template.
Extract it into a presentation-only InstallFormRendererComponent next to the
dialog. The dialog now binds [fields], [formGroup], [resolveImagePath], and
[reviewMode] inputs.

[reviewMode] consolidates the previous dialog-side passwordVisible[key]=true
assignment into the renderer's own state — the dialog no longer needs to
track per-field password visibility.

Layout mirrors thingsboard-pe so changes propagate cleanly between branches.
pull/15675/head
Andrew Shvayka 5 months ago
committed by GitHub
parent
commit
d53571ed82
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 107
      ui-ngx/src/app/modules/home/components/iot-hub/device-install-dialog/device-install-dialog.component.html
  2. 20
      ui-ngx/src/app/modules/home/components/iot-hub/device-install-dialog/device-install-dialog.component.ts
  3. 119
      ui-ngx/src/app/modules/home/components/iot-hub/device-install-dialog/install-form-renderer/install-form-renderer.component.html
  4. 29
      ui-ngx/src/app/modules/home/components/iot-hub/device-install-dialog/install-form-renderer/install-form-renderer.component.scss
  5. 82
      ui-ngx/src/app/modules/home/components/iot-hub/device-install-dialog/install-form-renderer/install-form-renderer.component.ts
  6. 4
      ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-components.module.ts

107
ui-ngx/src/app/modules/home/components/iot-hub/device-install-dialog/device-install-dialog.component.html

@ -64,107 +64,12 @@
@case ('form') {
<div class="tb-device-install-form flex flex-col gap-2 p-6">
@if (ws.formGroup) {
<form [formGroup]="ws.formGroup" class="flex flex-col gap-4">
@for (field of ws.formFields; track field.key) {
@switch (field.type) {
@case ('BOOLEAN') {
<mat-checkbox [formControlName]="field.key">
{{ field.label }}
</mat-checkbox>
}
@case ('SELECT') {
<mat-form-field appearance="outline">
<mat-label>{{ field.label }}</mat-label>
<mat-select [formControlName]="field.key">
@for (opt of field.options; track opt.value) {
<mat-option [value]="opt.value">{{ opt.label }}</mat-option>
}
</mat-select>
@if (field.helpText) {
<mat-hint>{{ field.helpText }}</mat-hint>
}
@if (ws.formGroup.controls[field.key]?.hasError('required')) {
<mat-error>{{ field.label + ' is required' }}</mat-error>
}
</mat-form-field>
}
@case ('PASSWORD') {
<mat-form-field appearance="outline">
<mat-label>{{ field.label }}</mat-label>
<input matInput [type]="passwordVisible[field.key] ? 'text' : 'password'"
[formControlName]="field.key">
<button mat-icon-button matSuffix type="button"
(click)="passwordVisible[field.key] = !passwordVisible[field.key]">
<mat-icon>{{ passwordVisible[field.key] ? 'visibility_off' : 'visibility' }}</mat-icon>
</button>
@if (field.randomGenerator) {
<button mat-icon-button matSuffix type="button"
[matTooltip]="'iot-hub.generate-random-value' | translate"
matTooltipPosition="above"
(click)="regenerateFieldValue(ws, field)">
<mat-icon>autorenew</mat-icon>
</button>
}
@if (field.helpText) {
<mat-hint>{{ field.helpText }}</mat-hint>
}
@if (ws.formGroup.controls[field.key]?.hasError('required')) {
<mat-error>{{ field.label + ' is required' }}</mat-error>
}
@if (ws.formGroup.controls[field.key]?.hasError('pattern')) {
<mat-error>{{ getPatternErrorMessage(field) }}</mat-error>
}
</mat-form-field>
}
@case ('INTEGER') {
<mat-form-field appearance="outline">
<mat-label>{{ field.label }}</mat-label>
<input matInput type="number" [formControlName]="field.key">
@if (field.helpText) {
<mat-hint>{{ field.helpText }}</mat-hint>
}
@if (ws.formGroup.controls[field.key]?.hasError('required')) {
<mat-error>{{ field.label + ' is required' }}</mat-error>
}
@if (ws.formGroup.controls[field.key]?.hasError('pattern')) {
<mat-error>{{ getPatternErrorMessage(field) }}</mat-error>
}
</mat-form-field>
}
@default {
<mat-form-field appearance="outline">
<mat-label>{{ field.label }}</mat-label>
<input matInput [formControlName]="field.key">
@if (field.randomGenerator) {
<button mat-icon-button matSuffix type="button"
[matTooltip]="'iot-hub.generate-random-value' | translate"
matTooltipPosition="above"
(click)="regenerateFieldValue(ws, field)">
<mat-icon>autorenew</mat-icon>
</button>
}
@if (field.helpText) {
<mat-hint>{{ field.helpText }}</mat-hint>
}
@if (ws.formGroup.controls[field.key]?.hasError('required')) {
<mat-error>{{ field.label + ' is required' }}</mat-error>
}
@if (ws.formGroup.controls[field.key]?.hasError('pattern')) {
<mat-error>{{ getPatternErrorMessage(field) }}</mat-error>
}
</mat-form-field>
}
}
@if (field.type === 'BOOLEAN' && field.helpText) {
<div class="tb-form-field-help">{{ field.helpText }}</div>
}
@if (field.helpImage) {
<img class="tb-form-field-help-image" [src]="resolveImagePath(field.helpImage)" [alt]="field.label">
}
}
</form>
}
<tb-install-form-renderer
[fields]="ws.formFields"
[formGroup]="ws.formGroup"
[resolveImagePath]="resolveImagePathFn"
[reviewMode]="reviewMode">
</tb-install-form-renderer>
</div>
}

20
ui-ngx/src/app/modules/home/components/iot-hub/device-install-dialog/device-install-dialog.component.ts

@ -108,7 +108,6 @@ export class TbDeviceInstallDialogComponent extends DialogComponent<TbDeviceInst
// Wizard
wizardSteps: WizardStep[] = [];
wizardStarted = false;
passwordVisible: Record<string, boolean> = {};
reviewMode = false;
// Variable resolution state
@ -456,9 +455,9 @@ export class TbDeviceInstallDialogComponent extends DialogComponent<TbDeviceInst
return this.zipImages.get(path) || path;
}
getPatternErrorMessage(field: FormFieldDefinition): string {
return field.validators?.[0]?.message || 'Invalid format';
}
// Bound function reference passed to <tb-install-form-renderer>'s [resolveImagePath] input.
// Defined as a property so the template binding stays stable across change detection.
readonly resolveImagePathFn: (path: string) => string = (path: string) => this.resolveImagePath(path);
// --- Variable Resolution ---
@ -537,7 +536,6 @@ export class TbDeviceInstallDialogComponent extends DialogComponent<TbDeviceInst
this.formValues = {};
this.entityOutputs.clear();
}
this.passwordVisible = {};
this.buildWizardSteps();
this.wizardStarted = true;
@ -651,9 +649,6 @@ export class TbDeviceInstallDialogComponent extends DialogComponent<TbDeviceInst
}
const initialValue = this.resolveInitialFieldValue(field, storedValues?.[field.key]);
controls[field.key] = new UntypedFormControl(initialValue, validators);
if (field.type === FormFieldType.PASSWORD) {
this.passwordVisible[field.key] = true; // Show passwords in review mode
}
}
ws.formGroup = new UntypedFormGroup(controls);
if (this.reviewMode) {
@ -673,15 +668,6 @@ export class TbDeviceInstallDialogComponent extends DialogComponent<TbDeviceInst
return field.defaultValue ?? (field.type === FormFieldType.BOOLEAN ? false : '');
}
regenerateFieldValue(ws: WizardStep, field: FormFieldDefinition): void {
const control = ws.formGroup?.controls[field.key];
if (!control) {
return;
}
control.patchValue(generateSecret(field.randomSize ?? DEFAULT_RANDOM_SIZE));
control.markAsDirty();
}
private onStepActivated(): void {
const step = this.currentWizardStep;
if (!step) {

119
ui-ngx/src/app/modules/home/components/iot-hub/device-install-dialog/install-form-renderer/install-form-renderer.component.html

@ -0,0 +1,119 @@
<!--
Copyright © 2016-2026 The Thingsboard Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
@if (formGroup) {
<form [formGroup]="formGroup" class="tb-install-form flex flex-col gap-4">
@for (field of fields; track field.key; let i = $index) {
<!-- typed widget -->
@switch (field.type) {
@case (FormFieldType.BOOLEAN) {
<mat-checkbox [formControlName]="field.key">{{ field.label }}</mat-checkbox>
}
@case (FormFieldType.SELECT) {
<mat-form-field appearance="outline">
<mat-label>{{ field.label }}</mat-label>
<mat-select [formControlName]="field.key">
@for (opt of field.options; track opt.value) {
<mat-option [value]="opt.value">{{ opt.label }}</mat-option>
}
</mat-select>
@if (field.helpText) {
<mat-hint>{{ field.helpText }}</mat-hint>
}
@if (formGroup.controls[field.key]?.hasError('required')) {
<mat-error>{{ field.label + ' is required' }}</mat-error>
}
</mat-form-field>
}
@case (FormFieldType.PASSWORD) {
<mat-form-field appearance="outline">
<mat-label>{{ field.label }}</mat-label>
<input matInput [type]="passwordVisible[field.key] ? 'text' : 'password'"
[formControlName]="field.key">
<button mat-icon-button matSuffix type="button"
(click)="togglePasswordVisible(field.key)">
<mat-icon>{{ passwordVisible[field.key] ? 'visibility_off' : 'visibility' }}</mat-icon>
</button>
@if (field.randomGenerator) {
<button mat-icon-button matSuffix type="button"
[matTooltip]="'iot-hub.generate-random-value' | translate"
matTooltipPosition="above"
(click)="regenerate(field)">
<mat-icon>autorenew</mat-icon>
</button>
}
@if (field.helpText) {
<mat-hint>{{ field.helpText }}</mat-hint>
}
@if (formGroup.controls[field.key]?.hasError('required')) {
<mat-error>{{ field.label + ' is required' }}</mat-error>
}
@if (formGroup.controls[field.key]?.hasError('pattern')) {
<mat-error>{{ getPatternErrorMessage(field) }}</mat-error>
}
</mat-form-field>
}
@case (FormFieldType.INTEGER) {
<mat-form-field appearance="outline">
<mat-label>{{ field.label }}</mat-label>
<input matInput type="number" [formControlName]="field.key">
@if (field.helpText) {
<mat-hint>{{ field.helpText }}</mat-hint>
}
@if (formGroup.controls[field.key]?.hasError('required')) {
<mat-error>{{ field.label + ' is required' }}</mat-error>
}
@if (formGroup.controls[field.key]?.hasError('pattern')) {
<mat-error>{{ getPatternErrorMessage(field) }}</mat-error>
}
</mat-form-field>
}
@default {
<mat-form-field appearance="outline">
<mat-label>{{ field.label }}</mat-label>
<input matInput [formControlName]="field.key">
@if (field.randomGenerator) {
<button mat-icon-button matSuffix type="button"
[matTooltip]="'iot-hub.generate-random-value' | translate"
matTooltipPosition="above"
(click)="regenerate(field)">
<mat-icon>autorenew</mat-icon>
</button>
}
@if (field.helpText) {
<mat-hint>{{ field.helpText }}</mat-hint>
}
@if (formGroup.controls[field.key]?.hasError('required')) {
<mat-error>{{ field.label + ' is required' }}</mat-error>
}
@if (formGroup.controls[field.key]?.hasError('pattern')) {
<mat-error>{{ getPatternErrorMessage(field) }}</mat-error>
}
</mat-form-field>
}
}
@if (field.type === FormFieldType.BOOLEAN && field.helpText) {
<div class="tb-install-form-field-help">{{ field.helpText }}</div>
}
@if (field.helpImage) {
<img class="tb-install-form-field-help-image" [src]="imagePath(field.helpImage)" [alt]="field.label">
}
}
</form>
}

29
ui-ngx/src/app/modules/home/components/iot-hub/device-install-dialog/install-form-renderer/install-form-renderer.component.scss

@ -0,0 +1,29 @@
/**
* Copyright © 2016-2026 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
:host {
display: block;
}
.tb-install-form-field-help {
font-size: 12px;
color: rgba(0, 0, 0, 0.6);
margin-top: 4px;
}
.tb-install-form-field-help-image {
max-width: 100%;
margin-top: 8px;
}

82
ui-ngx/src/app/modules/home/components/iot-hub/device-install-dialog/install-form-renderer/install-form-renderer.component.ts

@ -0,0 +1,82 @@
///
/// Copyright © 2016-2026 The Thingsboard Authors
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
import { Component, Input, OnChanges, SimpleChanges } from '@angular/core';
import { UntypedFormGroup } from '@angular/forms';
import { FormFieldDefinition, FormFieldType } from '@shared/models/iot-hub/device-package.models';
import { generateSecret } from '@core/utils';
const DEFAULT_RANDOM_SIZE = 20;
/**
* Reusable form renderer for the device install dialog's SHOW_FORM step. Consumes
* the FormFieldDefinition[] parsed from the package's form.json directly.
*
* The renderer is presentation-only. The caller owns the FormGroup and supplies
* the FormFieldDefinition[] array. Optional resolveImagePath callback maps
* relative help-image paths to data URIs (the dialog supplies it from the parsed
* package ZIP).
*/
@Component({
selector: 'tb-install-form-renderer',
templateUrl: './install-form-renderer.component.html',
styleUrls: ['./install-form-renderer.component.scss'],
standalone: false
})
export class InstallFormRendererComponent implements OnChanges {
@Input() fields: FormFieldDefinition[] = [];
@Input() formGroup!: UntypedFormGroup;
@Input() resolveImagePath?: (path: string) => string;
/** When true, PASSWORD inputs render unmasked by default (used by review mode). */
@Input() reviewMode = false;
passwordVisible: Record<string, boolean> = {};
readonly FormFieldType = FormFieldType;
ngOnChanges(changes: SimpleChanges): void {
if (changes.fields || changes.reviewMode) {
this.passwordVisible = {};
if (this.reviewMode) {
for (const f of this.fields) {
if (f.type === FormFieldType.PASSWORD) {
this.passwordVisible[f.key] = true;
}
}
}
}
}
togglePasswordVisible(key: string): void {
this.passwordVisible[key] = !this.passwordVisible[key];
}
regenerate(field: FormFieldDefinition): void {
const control = this.formGroup.controls[field.key];
if (!control) return;
control.patchValue(generateSecret(field.randomSize ?? DEFAULT_RANDOM_SIZE));
control.markAsDirty();
}
getPatternErrorMessage(field: FormFieldDefinition): string {
return field.validators?.[0]?.message || 'Invalid format';
}
imagePath(path: string): string {
return this.resolveImagePath ? this.resolveImagePath(path) : path;
}
}

4
ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-components.module.ts

@ -26,6 +26,7 @@ import { TbIotHubUpdateDialogComponent } from './iot-hub-update-dialog.component
import { TbIotHubDeleteDialogComponent } from './iot-hub-delete-dialog.component';
import { TbIotHubUnpublishedWarningDialogComponent } from './iot-hub-unpublished-warning-dialog.component';
import { TbDeviceInstallDialogComponent } from './device-install-dialog/device-install-dialog.component';
import { InstallFormRendererComponent } from './device-install-dialog/install-form-renderer/install-form-renderer.component';
import { TbIotHubSearchComponent } from './iot-hub-search.component';
import { TbIotHubInstalledItemsTableComponent } from './iot-hub-installed-items-table.component';
import { TbIotHubInstalledItemsDialogComponent } from './iot-hub-installed-items-dialog.component';
@ -51,7 +52,8 @@ import { IotHubItemLinkModule } from './iot-hub-item-link-card/iot-hub-item-link
TbIotHubInstalledItemsDialogComponent,
TbPeConnectivityMethodPromptComponent,
TbIotHubMarkdownComponent,
SolutionInstallDialogComponent
SolutionInstallDialogComponent,
InstallFormRendererComponent
],
imports: [
CommonModule,

Loading…
Cancel
Save