Skip to content

Commit

Permalink
fix(core): Textfield has change detection problems for [filler]
Browse files Browse the repository at this point in the history
  • Loading branch information
nsbarsukov committed Sep 27, 2024
1 parent 8e78f6f commit 1ac4207
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
41 changes: 23 additions & 18 deletions projects/core/components/textfield/textfield.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
forwardRef,
inject,
Input,
signal,
ViewChild,
ViewContainerRef,
ViewEncapsulation,
Expand Down Expand Up @@ -65,6 +66,8 @@ import {TuiWithTextfieldDropdown} from './textfield-dropdown.directive';
},
})
export class TuiTextfieldComponent<T> implements TuiDataListHost<T> {
// TODO: refactor to signal inputs after Angular update
private readonly filler = signal('');
private readonly autoId = tuiInjectId();
private readonly el = tuiInjectElement();
private readonly open = tuiDropdownOpen();
Expand All @@ -81,6 +84,21 @@ export class TuiTextfieldComponent<T> implements TuiDataListHost<T> {

protected readonly icons = inject(TUI_COMMON_ICONS);

protected computedFiller = computed(() => {
const value = this.directive?.nativeValue() || '';
const filledValue = value + this.filler().slice(value.length);

return filledValue.length > value.length ? filledValue : '';
});

protected showFiller = computed(() =>
Boolean(
this.focused() &&
this.computedFiller() &&
(this.directive?.nativeValue() || !this.input?.nativeElement.placeholder),
),
);

@ViewChild('vcr', {read: ViewContainerRef, static: true})
public readonly vcr?: ViewContainerRef;

Expand All @@ -90,9 +108,6 @@ export class TuiTextfieldComponent<T> implements TuiDataListHost<T> {
})
public readonly input?: ElementRef<HTMLInputElement>;

@Input()
public filler = '';

@Input()
public stringify: TuiStringHandler<T> = String;

Expand All @@ -102,6 +117,11 @@ export class TuiTextfieldComponent<T> implements TuiDataListHost<T> {
public readonly focused = computed(() => this.open() || this.focusedIn());
public readonly options = inject(TUI_TEXTFIELD_OPTIONS);

@Input('filler')
public set fillerSetter(filler: string) {
this.filler.set(filler);
}

public get id(): string {
return this.input?.nativeElement.id || this.autoId;
}
Expand All @@ -115,21 +135,6 @@ export class TuiTextfieldComponent<T> implements TuiDataListHost<T> {
this.open.set(false);
}

protected get computedFiller(): string {
const value = this.input?.nativeElement.value || '';
const filler = value + this.filler.slice(value.length);

return filler.length > value.length ? filler : '';
}

protected get showFiller(): boolean {
return (
this.focused() &&
!!this.computedFiller &&
(!!this.input?.nativeElement.value || !this.input?.nativeElement.placeholder)
);
}

protected get hasLabel(): boolean {
return Boolean(this.label?.nativeElement?.childNodes.length);
}
Expand Down
7 changes: 7 additions & 0 deletions projects/core/components/textfield/textfield.directive.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {computed, Directive, inject, Input, type OnChanges, signal} from '@angular/core';
import {toSignal} from '@angular/core/rxjs-interop';
import {TuiNativeValidator} from '@taiga-ui/cdk/directives/native-validator';
import {tuiInjectElement} from '@taiga-ui/cdk/utils/dom';
import {
Expand All @@ -9,6 +10,7 @@ import {
tuiAppearanceState,
} from '@taiga-ui/core/directives/appearance';
import type {TuiInteractiveState} from '@taiga-ui/core/types';
import {fromEvent} from 'rxjs';

import {TuiTextfieldComponent} from './textfield.component';
import {TUI_TEXTFIELD_OPTIONS} from './textfield.options';
Expand All @@ -35,6 +37,11 @@ export class TuiTextfieldBase<T> implements OnChanges {
@Input()
public invalid: boolean | null = null;

public nativeValue = toSignal(
fromEvent(this.el, 'input', () => this.el.value),
{initialValue: this.el.value},
);

@Input('focused')
public set focusedSetter(focused: boolean | null) {
this.focused.set(focused);
Expand Down
4 changes: 2 additions & 2 deletions projects/core/components/textfield/textfield.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
</ng-container>
</span>
<input
*ngIf="showFiller"
*ngIf="showFiller()"
aria-hidden="true"
disabled
class="t-filler"
[value]="computedFiller"
[value]="computedFiller()"
/>

0 comments on commit 1ac4207

Please sign in to comment.