Skip to content

Commit

Permalink
refactor: datatable-selection and datatable-scroller as directive
Browse files Browse the repository at this point in the history
BREAKING CHANGE: replaced `DataTableSelectionComponent` with `DataTableSelectionDirective` and `ScrollerComponent` with `ScrollerDirective`. This components were used internally and now replaced as directives.
  • Loading branch information
chintankavathia committed Nov 21, 2024
1 parent ab910c3 commit 23addae
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 266 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { DataTableBodyComponent } from './body.component';
import { DataTableBodyRowComponent } from './body-row.component';
import { DataTableRowWrapperComponent } from './body-row-wrapper.component';
import { DataTableBodyCellComponent } from './body-cell.component';
import { DataTableSelectionComponent } from './selection.component';
import { DataTableSelectionDirective } from './selection.directive';
import { DataTableSummaryRowComponent } from './summary/summary-row.component';
import { ProgressBarComponent } from './progress-bar.component';
import { ScrollerComponent } from './scroller.component';
import { ScrollerDirective } from './scroller.directive';
import { ScrollbarHelper } from '../../services/scrollbar-helper.service';

describe('DataTableBodyComponent', () => {
Expand All @@ -21,10 +21,10 @@ describe('DataTableBodyComponent', () => {
DataTableBodyRowComponent,
DataTableRowWrapperComponent,
DataTableBodyCellComponent,
DataTableSelectionComponent,
DataTableSelectionDirective,
DataTableSummaryRowComponent,
ProgressBarComponent,
ScrollerComponent
ScrollerDirective
],
providers: [ScrollbarHelper]
});
Expand Down
380 changes: 189 additions & 191 deletions projects/ngx-datatable/src/lib/components/body/body.component.ts

Large diffs are not rendered by default.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { ScrollerDirective } from './scroller.directive';
import { Component, ViewChild } from '@angular/core';

@Component({
selector: 'test-fixture-component',
template: ` <datatable-scroller #scroller="scroller" /> `,
standalone: true,
imports: [ScrollerDirective]
})
class TestFixtureComponent {
@ViewChild(ScrollerDirective, { static: true }) scroller!: ScrollerDirective;
}

describe('ScrollerDirective', () => {
let fixture: ComponentFixture<TestFixtureComponent>;
let component: TestFixtureComponent;

// provide our implementations or mocks to the dependency injector
beforeEach(() => {
TestBed.configureTestingModule({
imports: [TestFixtureComponent, ScrollerDirective]
});
});

beforeEach(waitForAsync(() => {
TestBed.compileComponents().then(() => {
fixture = TestBed.createComponent(TestFixtureComponent);
component = fixture.componentInstance;
});
}));

describe('fixture', () => {
it('should have a directive instance', () => {
expect(component.scroller).toBeTruthy();
});
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
ChangeDetectionStrategy,
Component,
Directive,
ElementRef,
EventEmitter,
HostBinding,
Expand All @@ -12,16 +11,15 @@ import {
Renderer2
} from '@angular/core';

@Component({
@Directive({
selector: 'datatable-scroller',
template: ` <ng-content></ng-content> `,
exportAs: 'scroller',
standalone: true,
host: {
class: 'datatable-scroll'
},
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: true
}
})
export class ScrollerComponent implements OnInit, OnDestroy {
export class ScrollerDirective implements OnInit, OnDestroy {
private renderer = inject(Renderer2);

@Input() scrollbarV = false;
Expand Down Expand Up @@ -50,7 +48,7 @@ export class ScrollerComponent implements OnInit, OnDestroy {
// manual bind so we don't always listen
if (this.scrollbarV || this.scrollbarH) {
const renderer = this.renderer;
this.parentElement = renderer.parentNode(renderer.parentNode(this.element));
this.parentElement = renderer.parentNode(this.element);
this._scrollEventListener = this.onScrolled.bind(this);
this.parentElement.addEventListener('scroll', this._scrollEventListener);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { DataTableSelectionDirective } from './selection.directive';
import { Component, ViewChild } from '@angular/core';

@Component({
selector: 'test-fixture-component',
template: ` <div datatable-selection></div> `,
standalone: true,
imports: [DataTableSelectionDirective]
})
class TestFixtureComponent {
@ViewChild(DataTableSelectionDirective, { static: true }) selector!: DataTableSelectionDirective;
}

describe('DataTableSelectionDirective', () => {
let fixture: ComponentFixture<TestFixtureComponent>;
let component: TestFixtureComponent;

// provide our implementations or mocks to the dependency injector
beforeEach(() => {
TestBed.configureTestingModule({
imports: [TestFixtureComponent, DataTableSelectionDirective]
});
});

beforeEach(waitForAsync(() => {
TestBed.compileComponents().then(() => {
fixture = TestBed.createComponent(TestFixtureComponent);
component = fixture.componentInstance;
});
}));

describe('fixture', () => {
it('should have a component instance', () => {
expect(component.selector).toBeTruthy();
});
});
});
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core';
import { Directive, EventEmitter, Input, Output } from '@angular/core';
import { selectRows, selectRowsBetween } from '../../utils/selection';
import { Keys } from '../../utils/keys';
import { ActivateEvent, SelectionType } from '../../types/public.types';

@Component({
selector: 'datatable-selection',
template: ` <ng-content></ng-content> `,
changeDetection: ChangeDetectionStrategy.OnPush,
@Directive({
selector: '[datatable-selection]',
exportAs: 'selector',
standalone: true
})
export class DataTableSelectionComponent<TRow = any> {
export class DataTableSelectionDirective<TRow = any> {
@Input() rows: TRow[];
@Input() selected: TRow[];
@Input() selectEnabled: boolean;
Expand Down
4 changes: 2 additions & 2 deletions projects/ngx-datatable/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export * from './lib/components/body/body.component';
export * from './lib/components/body/body-cell.component';
export * from './lib/components/body/body-row.component';
export * from './lib/components/body/progress-bar.component';
export * from './lib/components/body/scroller.component';
export * from './lib/components/body/scroller.directive';
export * from './lib/components/body/body-row-wrapper.component';
export * from './lib/components/body/selection.component';
export * from './lib/components/body/selection.directive';
export * from './lib/components/body/body-group-header.directive';
export * from './lib/components/body/body-group-header-template.directive';
export * from './lib/components/body/summary/summary-row.component';
Expand Down

0 comments on commit 23addae

Please sign in to comment.