Browse Source

refactor(iot-hub): precompute the hint box per form row

The renderer looked a field's row up in a map (rowOf) from the template, and
called hasHintBox() / isInHintBoxRow() for every field on each change
detection. FormRow now carries hasHintBox, computed in ngOnChanges when the
fields or reviewMode change, and fieldTemplate gets the row in its context.
The subscriptSizing binding and the mat-hint checks read the flag; the map
and both methods are gone. hasFieldError() stays a call, since it depends on
the control state.

The group doc comment in FormFieldDefinition now describes the panel:
consecutive fields with the same group share one.

(cherry picked from commit 0777a0edbd)
pull/16455/head
dpinkevych 1 month ago
committed by dshvaika
parent
commit
d67b0a9d34
  1. 28
      ui-ngx/src/app/modules/home/components/iot-hub/device-install-dialog/install-form-renderer/install-form-renderer.component.html
  2. 30
      ui-ngx/src/app/modules/home/components/iot-hub/device-install-dialog/install-form-renderer/install-form-renderer.component.ts
  3. 2
      ui-ngx/src/app/shared/models/iot-hub/device-package.models.ts

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

@ -42,16 +42,16 @@
@for (field of row.fields; track field.key) { @for (field of row.fields; track field.key) {
<div class="flex min-w-0 flex-col" [class.flex-1]="!field.width" <div class="flex min-w-0 flex-col" [class.flex-1]="!field.width"
[style.flex]="field.width ? '0 1 ' + field.width + 'px' : null"> [style.flex]="field.width ? '0 1 ' + field.width + 'px' : null">
<ng-container *ngTemplateOutlet="fieldTemplate; context: { $implicit: field }"></ng-container> <ng-container *ngTemplateOutlet="fieldTemplate; context: { $implicit: field, row: row }"></ng-container>
</div> </div>
} }
</div> </div>
} @else { } @else {
<ng-container *ngTemplateOutlet="fieldTemplate; context: { $implicit: row.fields[0] }"></ng-container> <ng-container *ngTemplateOutlet="fieldTemplate; context: { $implicit: row.fields[0], row: row }"></ng-container>
} }
<!-- A field with a help popup shows its hint under the row, next to the popup trigger --> <!-- A field with a help popup shows its hint under the row, next to the popup trigger -->
@for (field of row.fields; track field.key) { @for (field of row.fields; track field.key) {
@if (hasHintBox(field)) { @if (row.hasHintBox && field.helpPopupId) {
<div class="tb-form-hint tb-primary-fill flex items-center justify-between gap-5"> <div class="tb-form-hint tb-primary-fill flex items-center justify-between gap-5">
<div class="w-full">{{ helpTextOf(field) }}</div> <div class="w-full">{{ helpTextOf(field) }}</div>
<div class="flex shrink-0" <div class="flex shrink-0"
@ -71,7 +71,7 @@
} }
</ng-template> </ng-template>
<ng-template #fieldTemplate let-field> <ng-template #fieldTemplate let-field let-row="row">
@if (isSecretWidget(field)) { @if (isSecretWidget(field)) {
<!-- secretSupport=true: render Secret picker (handles plaintext + Secret refs internally) --> <!-- secretSupport=true: render Secret picker (handles plaintext + Secret refs internally) -->
<tb-secret-key-input appearance="outline" <tb-secret-key-input appearance="outline"
@ -88,7 +88,7 @@
<mat-checkbox [formControlName]="field.key">{{ field.label }}</mat-checkbox> <mat-checkbox [formControlName]="field.key">{{ field.label }}</mat-checkbox>
} }
@case (FormFieldType.SELECT) { @case (FormFieldType.SELECT) {
<mat-form-field appearance="outline" [subscriptSizing]="isInHintBoxRow(field) ? 'dynamic' : 'fixed'" <mat-form-field appearance="outline" [subscriptSizing]="row.hasHintBox ? 'dynamic' : 'fixed'"
[floatLabel]="field.placeholder ? 'always' : 'auto'"> [floatLabel]="field.placeholder ? 'always' : 'auto'">
<mat-label>{{ field.label }}</mat-label> <mat-label>{{ field.label }}</mat-label>
<mat-select [formControlName]="field.key"> <mat-select [formControlName]="field.key">
@ -96,7 +96,7 @@
<mat-option [value]="opt.value">{{ opt.label }}</mat-option> <mat-option [value]="opt.value">{{ opt.label }}</mat-option>
} }
</mat-select> </mat-select>
@if (helpTextOf(field) && !hasHintBox(field)) { @if (helpTextOf(field) && !(row.hasHintBox && field.helpPopupId)) {
<mat-hint>{{ helpTextOf(field) }}</mat-hint> <mat-hint>{{ helpTextOf(field) }}</mat-hint>
} }
@if (formGroup.controls[field.key]?.hasError('required')) { @if (formGroup.controls[field.key]?.hasError('required')) {
@ -105,7 +105,7 @@
</mat-form-field> </mat-form-field>
} }
@case (FormFieldType.STRING_AUTOCOMPLETE) { @case (FormFieldType.STRING_AUTOCOMPLETE) {
<mat-form-field appearance="outline" [subscriptSizing]="isInHintBoxRow(field) ? 'dynamic' : 'fixed'" <mat-form-field appearance="outline" [subscriptSizing]="row.hasHintBox ? 'dynamic' : 'fixed'"
[floatLabel]="field.placeholder ? 'always' : 'auto'"> [floatLabel]="field.placeholder ? 'always' : 'auto'">
<mat-label>{{ field.label }}</mat-label> <mat-label>{{ field.label }}</mat-label>
<input matInput [formControlName]="field.key" [placeholder]="field.placeholder ?? ''" [matAutocomplete]="auto" <input matInput [formControlName]="field.key" [placeholder]="field.placeholder ?? ''" [matAutocomplete]="auto"
@ -116,7 +116,7 @@
<mat-option [value]="opt.value">{{ opt.label }}</mat-option> <mat-option [value]="opt.value">{{ opt.label }}</mat-option>
} }
</mat-autocomplete> </mat-autocomplete>
@if (helpTextOf(field) && !hasHintBox(field)) { @if (helpTextOf(field) && !(row.hasHintBox && field.helpPopupId)) {
<mat-hint>{{ helpTextOf(field) }}</mat-hint> <mat-hint>{{ helpTextOf(field) }}</mat-hint>
} }
@if (formGroup.controls[field.key]?.hasError('required')) { @if (formGroup.controls[field.key]?.hasError('required')) {
@ -131,7 +131,7 @@
</mat-form-field> </mat-form-field>
} }
@case (FormFieldType.PASSWORD) { @case (FormFieldType.PASSWORD) {
<mat-form-field appearance="outline" [subscriptSizing]="isInHintBoxRow(field) ? 'dynamic' : 'fixed'" <mat-form-field appearance="outline" [subscriptSizing]="row.hasHintBox ? 'dynamic' : 'fixed'"
[floatLabel]="field.placeholder ? 'always' : 'auto'"> [floatLabel]="field.placeholder ? 'always' : 'auto'">
<mat-label>{{ field.label }}</mat-label> <mat-label>{{ field.label }}</mat-label>
<input matInput [type]="passwordVisible[field.key] ? 'text' : 'password'" <input matInput [type]="passwordVisible[field.key] ? 'text' : 'password'"
@ -150,7 +150,7 @@
<mat-icon>autorenew</mat-icon> <mat-icon>autorenew</mat-icon>
</button> </button>
} }
@if (helpTextOf(field) && !hasHintBox(field)) { @if (helpTextOf(field) && !(row.hasHintBox && field.helpPopupId)) {
<mat-hint>{{ helpTextOf(field) }}</mat-hint> <mat-hint>{{ helpTextOf(field) }}</mat-hint>
} }
@if (formGroup.controls[field.key]?.hasError('required')) { @if (formGroup.controls[field.key]?.hasError('required')) {
@ -165,13 +165,13 @@
</mat-form-field> </mat-form-field>
} }
@case (FormFieldType.INTEGER) { @case (FormFieldType.INTEGER) {
<mat-form-field appearance="outline" [subscriptSizing]="isInHintBoxRow(field) ? 'dynamic' : 'fixed'" <mat-form-field appearance="outline" [subscriptSizing]="row.hasHintBox ? 'dynamic' : 'fixed'"
[floatLabel]="field.placeholder ? 'always' : 'auto'"> [floatLabel]="field.placeholder ? 'always' : 'auto'">
<mat-label>{{ field.label }}</mat-label> <mat-label>{{ field.label }}</mat-label>
<input matInput type="number" [formControlName]="field.key" [placeholder]="field.placeholder ?? ''" <input matInput type="number" [formControlName]="field.key" [placeholder]="field.placeholder ?? ''"
[errorStateMatcher]="errorStateMatcher" (focus)="onFocus(field)" (blur)="onBlur(field)" [errorStateMatcher]="errorStateMatcher" (focus)="onFocus(field)" (blur)="onBlur(field)"
data-lpignore="true" data-1p-ignore data-bwignore> data-lpignore="true" data-1p-ignore data-bwignore>
@if (helpTextOf(field) && !hasHintBox(field)) { @if (helpTextOf(field) && !(row.hasHintBox && field.helpPopupId)) {
<mat-hint>{{ helpTextOf(field) }}</mat-hint> <mat-hint>{{ helpTextOf(field) }}</mat-hint>
} }
@if (formGroup.controls[field.key]?.hasError('required')) { @if (formGroup.controls[field.key]?.hasError('required')) {
@ -186,7 +186,7 @@
</mat-form-field> </mat-form-field>
} }
@default { @default {
<mat-form-field appearance="outline" [subscriptSizing]="isInHintBoxRow(field) ? 'dynamic' : 'fixed'" <mat-form-field appearance="outline" [subscriptSizing]="row.hasHintBox ? 'dynamic' : 'fixed'"
[floatLabel]="field.placeholder ? 'always' : 'auto'"> [floatLabel]="field.placeholder ? 'always' : 'auto'">
<mat-label>{{ field.label }}</mat-label> <mat-label>{{ field.label }}</mat-label>
<input matInput [formControlName]="field.key" [placeholder]="field.placeholder ?? ''" <input matInput [formControlName]="field.key" [placeholder]="field.placeholder ?? ''"
@ -200,7 +200,7 @@
<mat-icon>autorenew</mat-icon> <mat-icon>autorenew</mat-icon>
</button> </button>
} }
@if (helpTextOf(field) && !hasHintBox(field)) { @if (helpTextOf(field) && !(row.hasHintBox && field.helpPopupId)) {
<mat-hint>{{ helpTextOf(field) }}</mat-hint> <mat-hint>{{ helpTextOf(field) }}</mat-hint>
} }
@if (formGroup.controls[field.key]?.hasError('required')) { @if (formGroup.controls[field.key]?.hasError('required')) {

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

@ -9,9 +9,14 @@ import { generateSecret } from '@core/utils';
const DEFAULT_RANDOM_SIZE = 20; const DEFAULT_RANDOM_SIZE = 20;
/** Fields shown on one line. */ /**
* Fields shown on one line. When one of them has a help popup (outside review mode), the row shows
* the field's hint in a box under it, next to the popup trigger, and its fields don't reserve space
* for a hint or an error, so the box sits right under them.
*/
interface FormRow { interface FormRow {
fields: FormFieldDefinition[]; fields: FormFieldDefinition[];
hasHintBox: boolean;
} }
/** Consecutive rows of one group, shown in a panel with the group as its title; no title for ungrouped fields. */ /** Consecutive rows of one group, shown in a panel with the group as its title; no title for ungrouped fields. */
@ -53,7 +58,6 @@ export class InstallFormRendererComponent implements OnChanges {
sections: FormSection[] = []; sections: FormSection[] = [];
// The same for the fields of the collapsed Advanced settings panel // The same for the fields of the collapsed Advanced settings panel
advancedSections: FormSection[] = []; advancedSections: FormSection[] = [];
private rowOf = new Map<string, FormRow>();
private focusedControl: AbstractControl | null = null; private focusedControl: AbstractControl | null = null;
private keepFocusedError = false; private keepFocusedError = false;
@ -81,8 +85,8 @@ export class InstallFormRendererComponent implements OnChanges {
} }
ngOnChanges(changes: SimpleChanges): void { ngOnChanges(changes: SimpleChanges): void {
if (changes.fields) { // The hint boxes depend on reviewMode too
this.rowOf.clear(); if (changes.fields || changes.reviewMode) {
const fields = this.fields ?? []; const fields = this.fields ?? [];
this.sections = this.buildSections(fields.filter(field => field.section !== 'advanced')); this.sections = this.buildSections(fields.filter(field => field.section !== 'advanced'));
this.advancedSections = this.buildSections(fields.filter(field => field.section === 'advanced')); this.advancedSections = this.buildSections(fields.filter(field => field.section === 'advanced'));
@ -108,12 +112,13 @@ export class InstallFormRendererComponent implements OnChanges {
} }
const rows = sections[sections.length - 1].rows; const rows = sections[sections.length - 1].rows;
const last = rows[rows.length - 1]; const last = rows[rows.length - 1];
const hasHintBox = !!field.helpPopupId && !this.reviewMode;
if (field.row && last?.fields[0].row === field.row) { if (field.row && last?.fields[0].row === field.row) {
last.fields.push(field); last.fields.push(field);
last.hasHintBox ||= hasHintBox;
} else { } else {
rows.push({ fields: [field] }); rows.push({ fields: [field], hasHintBox });
} }
this.rowOf.set(field.key, rows[rows.length - 1]);
previous = field; previous = field;
} }
return sections; return sections;
@ -158,19 +163,6 @@ export class InstallFormRendererComponent implements OnChanges {
return field.validators?.[0]?.message || 'Invalid format'; return field.validators?.[0]?.message || 'Invalid format';
} }
/** Fields with a help popup show their hint in a box under the row, with the popup trigger. */
hasHintBox(field: FormFieldDefinition): boolean {
return !!field.helpPopupId && !this.reviewMode;
}
/**
* True for the fields of a row that has a hint box. They don't reserve space for a hint or an
* error, so the box sits right under them.
*/
isInHintBoxRow(field: FormFieldDefinition): boolean {
return !!this.rowOf.get(field.key)?.fields.some(f => this.hasHintBox(f));
}
/** True when the field fails a validator other than required and pattern (one the dialog added). */ /** True when the field fails a validator other than required and pattern (one the dialog added). */
hasFieldError(field: FormFieldDefinition): boolean { hasFieldError(field: FormFieldDefinition): boolean {
const errors = this.formGroup.controls[field.key]?.errors; const errors = this.formGroup.controls[field.key]?.errors;

2
ui-ngx/src/app/shared/models/iot-hub/device-package.models.ts

@ -279,7 +279,7 @@ export interface FormFieldDefinition {
// (tb-secret-key-input); false / undefined → render the typed widget. // (tb-secret-key-input); false / undefined → render the typed widget.
secretSupport?: boolean; secretSupport?: boolean;
secretType?: SecretType; secretType?: SecretType;
// Fields with the same group get one header above the first of them // Consecutive fields with the same group are shown in one panel titled with the group
group?: string; group?: string;
readOnly?: boolean; readOnly?: boolean;
// Shown inside the empty field; the label then always stays above it // Shown inside the empty field; the label then always stays above it

Loading…
Cancel
Save