Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | 1x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 49x 49x 49x 49x 49x 49x 50x 50x 50x 50x 50x 50x 50x 50x 50x 10x 40x 50x 50x 52x 53x 50x 50x 32x 123x 123x 50x 123x 123x 33x 90x 90x 16x 16x 16x 16x 123x 123x 123x 31x 31x 27x 31x | /** * Copyright (c) Siemens 2016 - 2025 * SPDX-License-Identifier: MIT */ import { NgTemplateOutlet } from '@angular/common'; import { AfterContentChecked, AfterContentInit, booleanAttribute, ChangeDetectionStrategy, Component, computed, contentChild, ElementRef, inject, input, isSignal, OnChanges, OnDestroy, OnInit, signal, SimpleChanges } from '@angular/core'; import { FormControl, NgControl, RequiredValidator, ValidationErrors, ValidatorFn } from '@angular/forms'; import { SiTranslatePipe, TranslatableString } from '@siemens/element-translate-ng/translate'; import { SiFormFieldsetComponent } from '../form-fieldset/si-form-fieldset.component'; import { SiFormContainerComponent } from '../si-form-container/si-form-container.component'; import { SI_FORM_ITEM_CONTROL, SiFormItemControl } from '../si-form-item.control'; import { SiFormValidationErrorMapper } from '../si-form-validation-error.model'; import { SiFormValidationErrorService } from '../si-form-validation-error.service'; import { SiFormFieldNativeControl } from './si-form-field-native.control'; /** @internal */ export interface SiFormError { key: string; message?: TranslatableString; params: any; } @Component({ selector: 'si-form-item', imports: [SiTranslatePipe, NgTemplateOutlet], templateUrl: './si-form-item.component.html', styleUrl: '../si-form.shared.scss', changeDetection: ChangeDetectionStrategy.OnPush, host: { '[class.required]': 'required()', '[style.--si-form-label-width]': 'labelWidthCssVar()', '[class.form-check]': 'fieldControl()?.isFormCheck', '[class.form-check-inline]': 'fieldset?.inline()', '[class.si-form-input]': '!fieldset' } }) export class SiFormItemComponent implements AfterContentInit, AfterContentChecked, OnChanges, OnInit, OnDestroy { /** * The label to be displayed in the form item. * It will be translated if a translation key is available. */ readonly label = input<TranslatableString | null>(); /** * A custom width value to be applied to the label. * A value of 0 is allowed as well to visually hide away the label area. * * Numbers will be converted to pixels. * Using numbers is discouraged and might be dropped. * * @example labelWidth="100px" */ readonly labelWidth = input<string | number>(); /** * Disables the automatic error printing. Error printing will be enabled by default in v46. * * @defaultValue false */ readonly disableErrorPrinting = input(false, { transform: booleanAttribute }); readonly formErrorMapper = input<SiFormValidationErrorMapper>(); /** * Defines that this form item is required for the overall form to be valid. * * @defaultValue false */ // eslint-disable-next-line @angular-eslint/no-input-rename readonly requiredInput = input(false, { transform: booleanAttribute, alias: 'required' }); protected readonly fieldControlQuery = contentChild(SI_FORM_ITEM_CONTROL, { descendants: true }); /** @internal */ readonly ngControl = contentChild(NgControl, { descendants: true }); protected readonly controlElementRef = contentChild<NgControl, ElementRef<HTMLElement>>( NgControl, { read: ElementRef, descendants: true } ); protected readonly requiredValidator = contentChild(RequiredValidator, { descendants: true }); /** @internal */ readonly errors = signal<SiFormError[]>([]); protected fieldset = inject(SiFormFieldsetComponent, { optional: true }); protected container = inject(SiFormContainerComponent, { optional: true }); protected get formItemId(): string | null { const fieldControl = this.fieldControl(); return isSignal(fieldControl?.id) ? (fieldControl.id() ?? null) : (fieldControl?.id ?? null); } protected get formItemLabelledBy(): string | null { const fieldControl = this.fieldControl(); return isSignal(fieldControl?.labelledby) ? (fieldControl.labelledby() ?? null) : (fieldControl?.labelledby ?? null); } protected get formItemErrormessageId(): string | null { const fieldControl = this.fieldControl(); return isSignal(fieldControl?.errormessageId) ? (fieldControl?.errormessageId() ?? null) : (fieldControl?.errormessageId ?? null); } protected readonly fieldControl = computed( () => this.fieldControlQuery() ?? this.fieldControlNative() ); private validationErrorService = inject(SiFormValidationErrorService); private requiredTestControl = new FormControl(''); private validator?: ValidatorFn | null; private previousErrors?: ValidationErrors | null; private readonly hasRequiredValidator = signal(false); private readonly fieldControlNative = signal<SiFormItemControl | undefined>(undefined); protected readonly labelWidthCssVar = computed(() => { const labelWidth = this.labelWidth(); if (typeof labelWidth === 'number') { return `${labelWidth}px`; } return labelWidth; }); protected readonly printErrors = computed(() => { return !this.disableErrorPrinting() && !(this.container?.disableErrorPrinting() ?? false); }); /** @internal */ readonly required = computed(() => this.requiredInput() || this.hasRequiredValidator()); ngOnChanges(changes: SimpleChanges): void { Iif (changes.formErrorMapper) { this.updateValidationMessages(); } } ngOnInit(): void { this.fieldset?.registerFormItem(this); } ngAfterContentInit(): void { if (!this.fieldControlQuery()) { this.fieldControlNative.set( SiFormFieldNativeControl.createForElementRef(this.controlElementRef()) ); } } ngAfterContentChecked(): void { this.updateRequiredState(); this.updateValidationMessages(); } ngOnDestroy(): void { this.fieldset?.unregisterFormItem(this); } private updateRequiredState(): void { const requiredValidator = this.requiredValidator(); if (requiredValidator) { // the easy case: required validator is applied in the template: <input required> this.hasRequiredValidator.set(booleanAttribute(requiredValidator.required)); } else { // No required validator is applied via template, it could still be applied in the code. // If validators are applied via code, the validator object will change of controls. // So we only need to check for required if the validator object was changed. const validator = this.ngControl()?.control?.validator; if (this.validator !== validator) { this.validator = validator; this.requiredTestControl.setValidators(this.validator ?? null); this.requiredTestControl.updateValueAndValidity(); this.hasRequiredValidator.set(this.requiredTestControl.errors?.required ?? false); } } } private updateValidationMessages(): void { const control = this.ngControl(); const currentErrors = control?.errors; if (this.previousErrors !== currentErrors) { if (control && this.printErrors()) { this.errors.set( this.validationErrorService .resolveErrors( control.name, control.errors, this.formErrorMapper(), this.container?.formErrorMapper() ) .filter(error => error.message) ); this.previousErrors = currentErrors; } else IEif (this.errors().length) { this.errors.set([]); } } } } |