Skip to content

Commit

Permalink
feat(cdk): add tuiInjectId (#8730)
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode authored Aug 28, 2024
1 parent c9014a2 commit 6b3bbb0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
13 changes: 6 additions & 7 deletions projects/cdk/services/id.service.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import {Injectable} from '@angular/core';
import {inject, Injectable} from '@angular/core';

const TUI = 'tui_';

/**
* Generates unique ids
*/
@Injectable({
providedIn: 'root',
})
export class TuiIdService {
private static autoId = 0;

public generate(): string {
return `${TUI}${TuiIdService.autoId++}${Date.now()}`;
return `tui_${TuiIdService.autoId++}${Date.now()}`;
}
}

export function tuiInjectId(): string {
return inject(TuiIdService).generate();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {ChangeDetectionStrategy, Component, inject} from '@angular/core';
import {TuiIdService} from '@taiga-ui/cdk/services';
import {tuiInjectId} from '@taiga-ui/cdk/services';
import {tuiInjectElement} from '@taiga-ui/cdk/utils/dom';
import {
TEXTFIELD_CONTROLLER_PROVIDER,
Expand Down Expand Up @@ -27,8 +27,8 @@ import {TUI_TEXTFIELD_HOST} from '@taiga-ui/legacy/tokens';
},
})
export class TuiTextfieldComponent {
private readonly autoId = tuiInjectId();
private readonly el = tuiInjectElement<HTMLInputElement>();
private readonly idService = inject(TuiIdService);
protected readonly controller = inject(TUI_TEXTFIELD_WATCHED_CONTROLLER);
protected readonly host = inject<TuiTextfieldHost>(TUI_TEXTFIELD_HOST);

Expand All @@ -37,7 +37,7 @@ export class TuiTextfieldComponent {
}

protected get id(): string {
return this.el.id || this.idService.generate();
return this.el.id || this.autoId;
}

protected get inputMode(): string {
Expand Down

0 comments on commit 6b3bbb0

Please sign in to comment.