Skip to content

Commit

Permalink
refactor(cdk): small refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
waterplea committed Oct 2, 2020
1 parent c6d7291 commit 071b7f6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 43 deletions.
21 changes: 6 additions & 15 deletions projects/cdk/abstract/control.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {ChangeDetectorRef, HostBinding, Input, OnDestroy, OnInit} from '@angular/core';
import {AbstractControl, ControlValueAccessor, NgControl, NgModel} from '@angular/forms';
import {tuiAssert} from '@taiga-ui/cdk/classes';
import {EMPTY_FUNCTION} from '@taiga-ui/cdk/constants';
import {tuiDefaultProp} from '@taiga-ui/cdk/decorators';
import {TuiValidation} from '@taiga-ui/cdk/enums';
import {fallbackValue} from '@taiga-ui/cdk/utils/miscellaneous';
Expand All @@ -16,9 +17,9 @@ export abstract class AbstractTuiControl<T>
implements OnDestroy, OnInit, ControlValueAccessor {
private previousInternalValue?: T;

private onTouched?: () => void;
private onTouched = EMPTY_FUNCTION;

private onChange?: (value: T) => void;
private onChange = EMPTY_FUNCTION;

protected readonly fallbackValue = this.getFallbackValue();

Expand Down Expand Up @@ -56,11 +57,7 @@ export abstract class AbstractTuiControl<T>
return TuiValidation.Normal;
}

if (this.pseudoValidation !== null) {
return this.pseudoValidation;
}

return this.validation;
return this.pseudoValidation === null ? this.validation : this.pseudoValidation;
}

get validation(): TuiValidation {
Expand Down Expand Up @@ -204,18 +201,12 @@ export abstract class AbstractTuiControl<T>
}

private controlMarkAsTouched() {
if (this.onTouched) {
this.onTouched();
}

this.onTouched();
this.checkControlUpdate();
}

private controlSetValue(value: T) {
if (this.onChange) {
this.onChange(value);
}

this.onChange(value);
this.checkControlUpdate();
}

Expand Down
32 changes: 7 additions & 25 deletions projects/cdk/abstract/interactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,46 +71,28 @@ export abstract class AbstractTuiInteractive {

@HostBinding('class._hovered')
get computedHovered(): boolean {
if (this.computedDisabled) {
return false;
}

return fallbackValue(this.pseudoHovered, this.hovered);
return !this.computedDisabled && fallbackValue(this.pseudoHovered, this.hovered);
}

@HostBinding('class._pressed')
get computedPressed(): boolean {
if (this.computedDisabled) {
return false;
}

return fallbackValue(this.pseudoPressed, this.pressed);
return !this.computedDisabled && fallbackValue(this.pseudoPressed, this.pressed);
}

get computedFocusable(): boolean {
if (this.computedDisabled) {
return false;
}

return this.focusable || this.focused;
return !this.computedDisabled && (this.focusable || this.focused);
}

@HostBinding('class._focused')
get computedFocused(): boolean {
if (this.computedDisabled) {
return false;
}

return fallbackValue(this.pseudoFocused, this.focused);
return !this.computedDisabled && fallbackValue(this.pseudoFocused, this.focused);
}

@HostBinding('class._focus-visible')
get computedFocusVisible(): boolean {
if (this.computedDisabled) {
return false;
}

return fallbackValue(this.pseudoFocused, this.focusVisible);
return (
!this.computedDisabled && fallbackValue(this.pseudoFocused, this.focusVisible)
);
}

get id(): string {
Expand Down
5 changes: 2 additions & 3 deletions projects/cdk/classes/assert.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {isDevMode} from '@angular/core';

const NOOP = () => {};
import {EMPTY_FUNCTION} from '@taiga-ui/cdk/constants';

/**
* This class is needed for its getter functionality, so we check
Expand All @@ -11,7 +10,7 @@ export class TuiAssertHelper {

get assert(): (assertion: boolean, ...args: any[]) => void {
return !this.bootstrapped || !isDevMode()
? NOOP
? EMPTY_FUNCTION
: Function.prototype.bind.call(console.assert, console);
}
}
Expand Down

0 comments on commit 071b7f6

Please sign in to comment.