Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Textfield has change detection problems for [filler] #9243

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 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,20 @@ 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 +107,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 +116,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 +134,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),
nsbarsukov marked this conversation as resolved.
Show resolved Hide resolved
{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()"
/>
Loading