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 | 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 59x 7x 7x 7x 4x 7x 7x 4x 11x 6x 11x 4x 4x 7x 2x 11x 3x 3x 1x 7x 7x | /** * Copyright (c) Siemens 2016 - 2025 * SPDX-License-Identifier: MIT */ import { Directive, DoCheck, ElementRef, HostListener, inject, Injector, input, OnDestroy, signal } from '@angular/core'; import { NgControl, ValidationErrors } from '@angular/forms'; import { SiTooltipService, TooltipRef } from '@siemens/element-ng/tooltip'; import { SiFormContainerComponent } from '../si-form-container/si-form-container.component'; import { SiFormError } from '../si-form-item/si-form-item.component'; import { SiFormValidationErrorMapper } from '../si-form-validation-error.model'; import { SiFormValidationErrorService } from '../si-form-validation-error.service'; import { SI_FORM_VALIDATION_TOOLTIP_DATA, SiFormValidationTooltipComponent } from './si-form-validation-tooltip.component'; /** * Directive to show a tooltip with validation errors of a form control. * * In general, `si-form-item` should be used. * This directive is intended only for cases where the tooltip cannot be shown inline. * This is typically the case for tables and lists where the validation message would break the layout. * * @example * ```html * <input siFormValidationTooltip [formControl]="control" /> * ``` */ @Directive({ selector: '[siFormValidationTooltip]', providers: [SiTooltipService], host: { '[attr.aria-describedby]': 'describedBy' } }) export class SiFormValidationTooltipDirective implements OnDestroy, DoCheck { private static idCounter = 0; private tooltipService = inject(SiTooltipService); private formValidationService = inject(SiFormValidationErrorService); private formContainer = inject(SiFormContainerComponent, { optional: true }); readonly formErrorMapper = input<SiFormValidationErrorMapper>(); private ngControl = inject(NgControl); private elementRef = inject(ElementRef); private tooltipRef?: TooltipRef; private readonly errors = signal<SiFormError[]>([]); private currentErrors: ValidationErrors | null = null; private touched: boolean | null = null; // Use a counter to track how many events are matched that keep the tooltip active. // Active means we are listening to error changes. private activationCount = 0; protected readonly describedBy = `__si-form-validation-tooltip-${SiFormValidationTooltipDirective.idCounter++}`; ngDoCheck(): void { if ( this.activationCount && (this.currentErrors !== this.ngControl.errors || this.touched !== this.ngControl.touched) ) { this.currentErrors = this.ngControl.errors; this.touched = this.ngControl.touched; this.updateErrors(); } } ngOnDestroy(): void { this.destroyTooltip(); } @HostListener('focus') @HostListener('mouseenter') protected increaseActivation(): void { this.activationCount++; if (this.activationCount === 1) { this.updateErrors(); } } private updateErrors(): void { const errors = this.formValidationService .resolveErrors( this.ngControl.name, this.currentErrors, this.formErrorMapper(), this.formContainer?.formErrorMapper() ) .filter(error => !!error.message); if (!this.tooltipRef && errors.length && this.ngControl.touched) { this.tooltipRef = this.tooltipService.createTooltip({ placement: 'auto', element: this.elementRef, describedBy: this.describedBy, injector: Injector.create({ providers: [{ provide: SI_FORM_VALIDATION_TOOLTIP_DATA, useValue: this.errors }] }) }); this.tooltipRef.show(SiFormValidationTooltipComponent); } else if (this.tooltipRef && (!errors.length || this.ngControl.pristine)) { this.destroyTooltip(); } this.errors.set(errors); } @HostListener('blur') @HostListener('mouseleave') protected decreaseActivation(): void { this.activationCount--; if (!this.activationCount) { this.destroyTooltip(); } } private destroyTooltip(): void { this.tooltipRef?.destroy(); this.tooltipRef = undefined; } } |