Skip to content

Commit

Permalink
refactor(core): Appearance switch to signals and data-mode
Browse files Browse the repository at this point in the history
Signed-off-by: waterplea <alexander@inkin.ru>
  • Loading branch information
waterplea committed Jun 6, 2024
1 parent 8dee38b commit 4ae59cf
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
2 changes: 1 addition & 1 deletion projects/cdk/classes/signal-control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export abstract class TuiControl<T> implements ControlValueAccessor {

public readonly mode = computed(() =>
// eslint-disable-next-line no-nested-ternary
this.readOnly() ? 'readonly' : this.invalid() ? 'invalid' : null,
this.readOnly() ? 'readonly' : this.invalid() ? 'invalid' : 'valid',
);

constructor() {
Expand Down
15 changes: 10 additions & 5 deletions projects/cdk/directives/hovered/hovered.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type {Signal} from '@angular/core';
import {inject, Injectable, NgZone} from '@angular/core';
import {ChangeDetectorRef, inject, Injectable, NgZone} from '@angular/core';
import {toSignal} from '@angular/core/rxjs-interop';
import {TUI_FALSE_HANDLER, TUI_TRUE_HANDLER} from '@taiga-ui/cdk/constants';
import {tuiTypedFromEvent, tuiZoneOptimized} from '@taiga-ui/cdk/observables';
import {tuiTypedFromEvent, tuiWatch, tuiZoneOptimized} from '@taiga-ui/cdk/observables';
import {TUI_IS_MOBILE} from '@taiga-ui/cdk/tokens';
import {tuiInjectElement, tuiIsElement} from '@taiga-ui/cdk/utils';
import {distinctUntilChanged, filter, map, merge, Observable, of} from 'rxjs';
Expand Down Expand Up @@ -36,7 +36,12 @@ export class TuiHoveredService extends Observable<boolean> {
}

export function tuiHovered(): Signal<boolean> {
return toSignal(inject(TUI_IS_MOBILE) ? of(false) : inject(TuiHoveredService), {
initialValue: false,
});
return toSignal(
inject(TUI_IS_MOBILE)
? of(false)
: inject(TuiHoveredService).pipe(tuiWatch(inject(ChangeDetectorRef))),
{
initialValue: false,
},
);
}
19 changes: 17 additions & 2 deletions projects/core/components/textfield/textfield.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import {TUI_TEXTFIELD_OPTIONS} from './textfield.options';
'[id]': 'el.id || id',
'[readOnly]': 'readOnly',
'[class._empty]': 'el.value === ""',
'[attr.data-mode]': 'readOnly ? "readonly" : null',
'[attr.data-invalid]': 'invalid',
'[attr.data-mode]': 'mode',
'(input)': '0',
'(focusin)': '0',
'(focusout)': '0',
Expand All @@ -42,6 +41,22 @@ export class TuiTextfieldDirective implements DoCheck {
@Input()
public state: TuiInteractiveState | null = null;

public get mode(): string | null {
if (this.readOnly) {
return 'readonly';
}

if (this.invalid === false) {
return 'valid';
}

if (this.invalid) {
return 'invalid';
}

return null;
}

public ngDoCheck(): void {
this.appearance.tuiAppearance = this.options.appearance;
this.appearance.tuiAppearanceFocus = this.focused ?? this.textfield.focused;
Expand Down
4 changes: 2 additions & 2 deletions projects/core/components/textfield/textfield.style.less
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@
transform: translateY(-0.7em);
}

&:not(:disabled):not([data-mode='readonly'])[data-invalid='true'] ~ label,
&:invalid:not(:disabled):not([data-mode='readonly']):not([data-invalid='false']) ~ label {
&:not(:disabled)[data-mode='invalid'] ~ label,
&:invalid:not(:disabled):not([data-mode]) ~ label {
color: var(--tui-negative);
}

Expand Down
4 changes: 2 additions & 2 deletions projects/core/styles/theme/appearance/textfield.less
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
box-shadow: none;
});

&:valid[data-invalid='true'],
&:invalid:not([data-invalid='false']) {
&[data-mode='invalid'],
&:invalid:not([data-mode]) {
outline-color: var(--tui-negative);
}

Expand Down

0 comments on commit 4ae59cf

Please sign in to comment.