diff --git a/projects/core/components/textfield/select.directive.ts b/projects/core/components/textfield/select.directive.ts index c419e35bf904..07a7779426aa 100644 --- a/projects/core/components/textfield/select.directive.ts +++ b/projects/core/components/textfield/select.directive.ts @@ -19,7 +19,7 @@ import {TuiTextfieldBase, TuiTextfieldDirective} from './textfield.directive'; providers: [tuiProvide(TuiTextfieldDirective, TuiSelect)], hostDirectives: [TuiNativeValidator, TuiAppearance], host: { - '[id]': 'el.id || autoId', + '[id]': 'textfield.id', '[attr.data-mode]': 'mode', '[class._empty]': 'value === ""', '(input)': '0', diff --git a/projects/core/components/textfield/textfield.component.ts b/projects/core/components/textfield/textfield.component.ts index 829aa7bed8b2..77224e2ed474 100644 --- a/projects/core/components/textfield/textfield.component.ts +++ b/projects/core/components/textfield/textfield.component.ts @@ -15,6 +15,7 @@ import { import {NgControl} from '@angular/forms'; import {WaResizeObserver} from '@ng-web-apis/resize-observer'; import {TuiNativeValidator} from '@taiga-ui/cdk/directives/native-validator'; +import {tuiInjectId} from '@taiga-ui/cdk/services'; import type {TuiContext, TuiStringHandler} from '@taiga-ui/cdk/types'; import {tuiInjectElement} from '@taiga-ui/cdk/utils/dom'; import {tuiFocusedIn} from '@taiga-ui/cdk/utils/focus'; @@ -80,12 +81,16 @@ export class TuiTextfieldComponent implements TuiDataListHost { protected side = 0; protected readonly options = inject(TUI_TEXTFIELD_OPTIONS); + protected readonly autoId = tuiInjectId(); protected readonly icons = inject(TUI_COMMON_ICONS); @ViewChild('vcr', {read: ViewContainerRef, static: true}) public readonly vcr?: ViewContainerRef; - @ContentChild(forwardRef(() => TuiTextfieldDirective), {read: ElementRef}) + @ContentChild(forwardRef(() => TuiTextfieldDirective), { + read: ElementRef, + static: true, + }) public readonly el?: ElementRef; @Input() @@ -100,7 +105,7 @@ export class TuiTextfieldComponent implements TuiDataListHost { public readonly focused = computed(() => this.open() || this.focusedIn()); public get id(): string { - return this.el?.nativeElement.id || ''; + return this.el?.nativeElement.id || this.autoId; } public get size(): TuiSizeL | TuiSizeS { diff --git a/projects/core/components/textfield/textfield.directive.ts b/projects/core/components/textfield/textfield.directive.ts index 4f04b1137e56..864e3283d724 100644 --- a/projects/core/components/textfield/textfield.directive.ts +++ b/projects/core/components/textfield/textfield.directive.ts @@ -1,6 +1,5 @@ import {computed, Directive, inject, Input, signal} from '@angular/core'; import {TuiNativeValidator} from '@taiga-ui/cdk/directives/native-validator'; -import {tuiInjectId} from '@taiga-ui/cdk/services'; import {tuiInjectElement} from '@taiga-ui/cdk/utils/dom'; import { TuiAppearance, @@ -25,7 +24,6 @@ export class TuiTextfieldBase { ); protected readonly textfield = inject(TuiTextfieldComponent); - protected readonly autoId = tuiInjectId(); protected readonly el = tuiInjectElement(); @Input() @@ -71,7 +69,7 @@ export class TuiTextfieldBase { selector: 'input[tuiTextfield]', hostDirectives: [TuiNativeValidator, TuiAppearance], host: { - '[id]': 'el.id || autoId', + '[id]': 'textfield.id', '[readOnly]': 'readOnly', '[class._empty]': 'el.value === ""', '[attr.data-mode]': 'mode',