Skip to content

Commit

Permalink
refactor(*): use methods overload for implementing classlist api
Browse files Browse the repository at this point in the history
  • Loading branch information
william-mba committed Jan 20, 2025
1 parent be4e84a commit b18fb4f
Show file tree
Hide file tree
Showing 26 changed files with 62 additions and 44 deletions.
8 changes: 4 additions & 4 deletions projects/tailwind-ng-core/src/config/tests/classlist.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('ClassList', () => {
};
const configArray = Obj.toArray(configObj);

classList.initFrom(configObj);
classList.init(configObj);

expect(classList.base).toEqual(configArray);
expect(classList.value).toEqual(configArray);
Expand All @@ -99,7 +99,7 @@ describe('ClassList', () => {
};
const configArray = Obj.toArray(configObj);

classList.setFrom(configObj);
classList.set(configObj);

expect(classList.value).toEqual(configArray);
});
Expand All @@ -119,7 +119,7 @@ describe('ClassList', () => {
}
};
const defaultConfig = Obj.toArray(configObj);
classList.setFrom(configObj);
classList.set(configObj);

const newConfigObj: ComponentConfig = {
textColor: 'text-gray-800',
Expand All @@ -136,7 +136,7 @@ describe('ClassList', () => {

const newConfig = Obj.toArray(newConfigObj);

classList.updateFrom(newConfigObj);
classList.update(newConfigObj);

defaultConfig.forEach(value => {
if (value === 'shadow-none') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ClassList, Str } from '@tailwind-ng/core';
import { TestBed } from '@angular/core/testing';
import { AvatarComponent } from './avatar.component';
import { Component, viewChild } from '@angular/core';
import { GetAvatarConfig, provideAvatarConfig } from './avatar.config';
import { GetAvatarConfig, provideAvatarConfig } from './avatar.component.config';

describe('AvatarComponent', () => {
beforeEach(async () => {
Expand Down Expand Up @@ -51,7 +51,7 @@ describe('AvatarComponent', () => {
const config = GetAvatarConfig();
const classList = new ClassList();

classList.setFrom({ b: config.base, s: config[component.size] });
classList.set({ b: config.base, s: config[component.size] });

expect(component.classList.base).toEqual(classList.base);
expect(component.classList.value).toEqual(classList.value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class AvatarComponent extends AvatarBase {

protected override onInit(): void {
this.config$.subscribe(config => {
this.classList.setFrom({ b: config.base, s: config[this.size] });
this.classList.set({ b: config.base, s: config[this.size] });
});
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @angular-eslint/component-selector, @angular-eslint/component-class-suffix */
import { BadgeComponent } from './badge.component';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { GetBadgeConfig, provideBadgeConfig } from './badge.config';
import { GetBadgeConfig, provideBadgeConfig } from './badge.component.config';
import { Component, viewChild } from '@angular/core';
import { ClassList, Str } from '@tailwind-ng/core';

Expand Down Expand Up @@ -49,7 +49,7 @@ describe('BadgeComponent', () => {
const config = GetBadgeConfig();
const classList = new ClassList();

classList.setFrom({ b: config.base, s: config[component.size] });
classList.set({ b: config.base, s: config[component.size] });

expect(component.classList.base).toEqual(classList.base);
expect(component.classList.value).toEqual(classList.value);
Expand Down
2 changes: 1 addition & 1 deletion projects/tailwind-ng/src/elements/badge/badge.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class BadgeComponent extends BadgeBase {

protected override onInit(): void {
this.config$.subscribe(config => {
this.classList.setFrom({ b: config.base, s: config[this.size] });
this.classList.set({ b: config.base, s: config[this.size] });
});
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @angular-eslint/component-selector, @angular-eslint/component-class-suffix */
import { ButtonGroupComponent } from './button-group.component';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { GetButtonGroupConfig, provideButtonGroupConfig } from './button-group.config';
import { GetButtonGroupConfig, provideButtonGroupConfig } from './button-group.component.config';
import { ClassList } from '@tailwind-ng/core';
import { Component, viewChild } from '@angular/core';
import { ButtonComponent } from '../button/button.component';
Expand Down Expand Up @@ -30,7 +30,7 @@ describe('ButtonGroupComponent', () => {

it('should set classList', () => {
const classList = new ClassList();
classList.setFrom(GetButtonGroupConfig());
classList.set(GetButtonGroupConfig());

expect(component.classList.base).toEqual(classList.base);
expect(component.classList.value).toEqual(classList.value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ButtonGroupBase } from '@tailwind-ng/core';
export class ButtonGroupComponent extends ButtonGroupBase {
protected override onInit(): void {
this.config$.subscribe((config) => {
this.classList.setFrom(config);
this.classList.set(config);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { SizeOption, ClassList, Str, ButtonVariant } from '@tailwind-ng/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ButtonComponent } from './button.component';
import { GetButtonConfig, provideButtonConfig } from './button.config';
import { GetButtonConfig, provideButtonConfig } from './button.component.config';
import { Component, viewChild } from '@angular/core';

describe('ButtonComponent', () => {
Expand Down Expand Up @@ -77,7 +77,7 @@ describe('ButtonComponent', () => {
const config = GetButtonConfig();
const classList = new ClassList();

classList.setFrom({
classList.set({
...config.primary,
...config[component.size]
});
Expand Down Expand Up @@ -178,7 +178,7 @@ describe('ButtonComponent', () => {
it('should set classList', () => {
const config = GetButtonConfig();
const classList = new ClassList();
classList.setFrom({
classList.set({
...config.secondary,
...config[component.size]
});
Expand Down
24 changes: 21 additions & 3 deletions projects/tailwind-ng/src/elements/button/button.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ButtonBase, ButtonVariant, ComboboxBase, DialogBase, DropdownBase, Popu
* @ngx-tailwind Button component
*/
@Component({
selector: 'tw-button, [tw-button], [twButton]',
selector: 'tw-button, button[tw-button], button[twButton]',
exportAs: 'twButton',
template: '<ng-content />',
encapsulation: ViewEncapsulation.None,
Expand Down Expand Up @@ -39,7 +39,7 @@ export class ButtonComponent extends ButtonBase {

protected override onInit(): void {
this.config$.subscribe((config) => {
this.classList.setFrom({
this.classList.set({
// The order of destructuring is important here.
// Because we want next object properties value to
// override the value of identical properties from objects destructured before.
Expand Down Expand Up @@ -85,9 +85,27 @@ export class ButtonComponent extends ButtonBase {
});

if (this.popup) {
this.nativeElement.addEventListener('click', () => this.popup?.toggle(), true);
this.nativeElement.addEventListener('click', this.onClick.bind(this), true);
}
this.nativeElement.addEventListener('keydown', this.onKeyDown.bind(this), true);

this.destroyRef.onDestroy(() => {
if (this.popup) {
this.nativeElement.removeEventListener('click', this.onClick.bind(this), true);
}
this.nativeElement.removeEventListener('keydown', this.onKeyDown.bind(this), true);
});
}

protected onClick(event: MouseEvent): void {
if (this.isDisabled) {
event.preventDefault();
event.stopImmediatePropagation();
return;
}
if (this.popup) {
this.popup.toggle();
}
}

protected onKeyDown(event: KeyboardEvent): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { DropdownComponent } from './dropdown.component';
import { provideDropdownConfig } from './dropdown.config';
import { provideDropdownConfig } from './dropdown.component.config';

describe('DropdownComponent', () => {
let component: DropdownComponent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export class DropdownComponent extends DropdownBase {
@Input() position: OverlayPosition = { top: 'top-2', right: 'right-0' };

protected override onInit(): void {
this.classList.initFrom(this.position);
this.config$.subscribe(config => this.classList.setFrom(config));
this.classList.init(this.position);
this.config$.subscribe(config => this.classList.set(config));

this.opened.subscribe(() => {
this.updatePositionIfNeeded();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Str } from '@tailwind-ng/core';
import { TestBed } from '@angular/core/testing';
import { IconDirective } from './icon.directive';
import { Component, ElementRef } from '@angular/core';
import { GetIconConfig, provideIconConfig } from './icon.config';
import { GetIconConfig, provideIconConfig } from './icon.directive.config';
import { By } from '@angular/platform-browser';

describe('IconDirective', () => {
Expand Down
4 changes: 2 additions & 2 deletions projects/tailwind-ng/src/elements/icon/icon.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export class IconDirective extends IconBase {

protected override onInit(): void {
this.config$.subscribe((config) => {
this.classList.initFrom(config[this.size]);
this.classList.setFrom(config.base);
this.classList.init(config[this.size]);
this.classList.set(config.base);
if (!config.source[this.key]) {
console.error(new Error(`Icon with key "${this.key}" does not exists in the icons source config.`));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DropdownComponent } from './../../../elements/dropdown/dropdown.compone
import { TestBed } from '@angular/core/testing';
import { ComboboxItemComponent } from './combobox-item.component';
import { Component, ElementRef, viewChildren } from '@angular/core';
import { GetComboboxItemConfig, provideComboboxItemConfig } from './combobox-item.config';
import { GetComboboxItemConfig, provideComboboxItemConfig } from './combobox-item.component.config';
import { InputDirective } from '../../input/input.directive';
import { USERS_STUB } from '../combobox.component.spec';
import { ReactiveFormsModule } from '@angular/forms';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class ComboboxItemComponent extends ComboboxItemBase implements ComboboxI

protected override onInit(): void {
this.config$.subscribe(config => {
this.classList.setFrom(config);
this.classList.set(config);
});

// Select the item if it is the default value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Component, ElementRef, viewChild, viewChildren } from '@angular/core';
import { ComboboxModule } from './combobox.module';
import { ButtonComponent } from '../../elements/button/button.component';
import { ReactiveFormsModule } from '@angular/forms';
import { provideIconConfig } from '../../elements/icon/icon.config';
import { provideIconConfig } from '../../elements/icon/icon.directive.config';
import { ComboboxItemComponent } from './combobox-item/combobox-item.component';

interface User {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { InputRadioDirective } from './input-radio.directive';
import { TestBed } from '@angular/core/testing';
import { GetInputRadioConfig, provideInputRadioConfig } from './input-radio.config';
import { GetInputRadioConfig, provideInputRadioConfig } from './input-radio.directive.config';
import { ClassList } from '@tailwind-ng/core';
import { Component, viewChild } from '@angular/core';

Expand Down Expand Up @@ -45,6 +45,6 @@ describe('InputRadioDirective', () => {
const appFixture = TestBed.createComponent(TestAppComponent);
const testApp = appFixture.componentInstance;
appFixture.detectChanges();
expect(testApp.input().classList.value).toEqual(new ClassList().setFrom(GetInputRadioConfig()).value);
expect(testApp.input().classList.value).toEqual(new ClassList().set(GetInputRadioConfig()).value);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { InputRadioBase } from '@tailwind-ng/core';
export class InputRadioDirective extends InputRadioBase {
protected override onInit(): void {
this.config$.subscribe(config => {
this.classList.setFrom(config);
this.classList.set(config);
});
}
}
4 changes: 2 additions & 2 deletions projects/tailwind-ng/src/forms/input/input.directive.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { InputDirective } from './input.directive';
import { TestBed } from '@angular/core/testing';
import { GetInputConfig, provideInputConfig } from './input.config';
import { GetInputConfig, provideInputConfig } from './input.directive.config';
import { ClassList } from '@tailwind-ng/core';
import { Component, viewChild } from '@angular/core';

Expand Down Expand Up @@ -45,6 +45,6 @@ describe('InputDirective', () => {
const appFixture = TestBed.createComponent(TestAppComponent);
const testApp = appFixture.componentInstance;
appFixture.detectChanges();
expect(testApp.input().classList.value).toEqual(new ClassList().setFrom(GetInputConfig()).value);
expect(testApp.input().classList.value).toEqual(new ClassList().set(GetInputConfig()).value);
});
});
2 changes: 1 addition & 1 deletion projects/tailwind-ng/src/forms/input/input.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { InputBase } from '@tailwind-ng/core';
export class InputDirective extends InputBase {
protected override onInit(): void {
this.config$.subscribe(config => {
this.classList.setFrom(config);
this.classList.set(config);
});
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ToggleComponent } from './toggle.component';
import { GetToggleConfig, provideToggleConfig } from './toggle.config';
import { GetToggleConfig, provideToggleConfig } from './toggle.component.config';
import { ClassList } from '@tailwind-ng/core';

describe('ToggleComponent', () => {
Expand Down Expand Up @@ -36,6 +36,6 @@ describe('ToggleComponent', () => {
});

it('should set classlist', () => {
expect(component.classList.value).toEqual(new ClassList().setFrom(GetToggleConfig()).value);
expect(component.classList.value).toEqual(new ClassList().set(GetToggleConfig()).value);
});
});
2 changes: 1 addition & 1 deletion projects/tailwind-ng/src/forms/toggle/toggle.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class ToggleComponent extends ToggleBase implements Toggle {

protected override onInit(): void {
this.config$.subscribe(config => {
this.classList.setFrom(config);
this.classList.set(config);
});
this.nativeElement.addEventListener('click', this.toggle.bind(this), { passive: true, capture: true });
this.nativeElement.addEventListener('keydown', this.onKeydown.bind(this), true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class DialogBackdrop extends BaseDirective implements ObservableConfig {

override onInit() {
this.config$.subscribe(config => {
this.classList.setFrom(config.backdrop);
this.classList.set(config.backdrop);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class DialogContainer extends BaseDirective implements ObservableConfig {

protected override onInit() {
this.config$.subscribe(config => {
this.classList.setFrom(config.container);
this.classList.set(config.container);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class DialogScrim extends BaseDirective implements ObservableConfig {

protected override onInit(): void {
this.config$.subscribe(config => {
this.classList.setFrom(config.scrim);
this.classList.set(config.scrim);
});
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @angular-eslint/component-selector */
import { By } from '@angular/platform-browser';
import { fakeAsync, TestBed, tick } from '@angular/core/testing';
import { GetDialogConfig, provideDialogConfig } from './dialog.config';
import { GetDialogConfig, provideDialogConfig } from './dialog.component.config';
import { DialogComponent } from './dialog.component';
import { DialogModule } from './dialog.module';
import { Component, ElementRef, viewChild } from '@angular/core';
Expand Down Expand Up @@ -321,7 +321,7 @@ describe('DialogComponent', () => {
const testComponent = testFixture.componentInstance;
const container = testComponent.container();
testFixture.detectChanges();
const classList = new ClassList().setFrom(GetDialogConfig().container!);
const classList = new ClassList().set(GetDialogConfig().container!);
expect(container.classList.value).toEqual(classList.value);
});

Expand Down Expand Up @@ -392,7 +392,7 @@ describe('DialogComponent', () => {
const component: TestComponent = fixture.debugElement.componentInstance;
fixture.detectChanges();

const classList = new ClassList().setFrom(GetDialogConfig().backdrop!);
const classList = new ClassList().set(GetDialogConfig().backdrop!);
expect(component.backdrop().classList.value).toEqual(classList.value);
});

Expand Down Expand Up @@ -433,7 +433,7 @@ describe('DialogComponent', () => {

describe('Scrim', () => {
it('should set classList', () => {
const classList = new ClassList().setFrom(GetDialogConfig().scrim!);
const classList = new ClassList().set(GetDialogConfig().scrim!);

TestBed.resetTestingModule();
TestBed.configureTestingModule({
Expand Down

0 comments on commit b18fb4f

Please sign in to comment.