Skip to content

Commit

Permalink
feat(table): add isExpanded input for nxExpandableTableRow (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
snoopy-cat authored and GitHub Enterprise committed Mar 25, 2021
1 parent 982e017 commit 5916a1a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Component, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
import { BooleanInput, coerceBooleanProperty } from '@angular/cdk/coercion';
import { Component, ChangeDetectionStrategy, ChangeDetectorRef, Input } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { NxExpandable } from './toggle-button.component';

Expand All @@ -17,6 +18,20 @@ import { NxExpandable } from './toggle-button.component';
changeDetection: ChangeDetectionStrategy.OnPush
})
export class NxExpandableTableRowComponent implements NxExpandable {

private _isExpanded: boolean;

/* Whether the row is expanded. */
@Input()
set isExpanded(value: boolean) {
this._isExpanded = coerceBooleanProperty(value);
this.expanded.next(this._isExpanded);
this._changeDetectorRef.markForCheck();
}
get isExpanded() {
return this._isExpanded;
}

expanded: BehaviorSubject<boolean> = new BehaviorSubject(false);

constructor(private _changeDetectorRef: ChangeDetectorRef) {}
Expand Down Expand Up @@ -51,4 +66,6 @@ export class NxExpandableTableRowComponent implements NxExpandable {
this._changeDetectorRef.markForCheck();
}
}

static ngAcceptInputType_isExpanded: BooleanInput;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChangeDetectionStrategy, Component, Type, ViewChild, DebugElement, Directive } from '@angular/core';
import { ChangeDetectionStrategy, Component, Type, ViewChild, DebugElement, Directive, ChangeDetectorRef } from '@angular/core';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { NxTableModule } from '../table.module';
import { NxExpandableTableRowComponent } from './expandable-table-row.component';
Expand All @@ -7,7 +7,10 @@ import { By } from '@angular/platform-browser';

@Directive()
abstract class ExpandableTableRowTest {
isExpanded: boolean = true;
@ViewChild(NxExpandableTableRowComponent) expandableTableRowInstance: NxExpandableTableRowComponent;

constructor(public cdr: ChangeDetectorRef) {}
}

describe(NxExpandableTableRowComponent.name, () => {
Expand All @@ -27,7 +30,8 @@ describe(NxExpandableTableRowComponent.name, () => {
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [
BasicExpandableTableRowComponent
BasicExpandableTableRowComponent,
ConfigurableExpandableTableRowComponent
],
imports: [NxTableModule]
}).compileComponents();
Expand All @@ -51,7 +55,7 @@ describe(NxExpandableTableRowComponent.name, () => {
});
});

describe('programatic', () => {
describe('programmatic', () => {
beforeEach(() => {
createTestComponent(BasicExpandableTableRowComponent);
});
Expand Down Expand Up @@ -92,6 +96,22 @@ describe(NxExpandableTableRowComponent.name, () => {
});
});

describe('isExpanded input', () => {
beforeEach(() => {
createTestComponent(ConfigurableExpandableTableRowComponent);
});

it('sets expanded', () => {
expect(expandableTableRowInstance.expanded.value).toBe(true);
});

it('updates expanded on isExpanded change', () => {
testInstance.isExpanded = false;
testInstance.cdr.detectChanges();
expect(expandableTableRowInstance.expanded.value).toBe(false);
});
});

describe('a11y', () => {
it('has no accessibility violations', (done) => {
createTestComponent(BasicExpandableTableRowComponent);
Expand All @@ -111,3 +131,11 @@ describe(NxExpandableTableRowComponent.name, () => {
changeDetection: ChangeDetectionStrategy.OnPush
})
class BasicExpandableTableRowComponent extends ExpandableTableRowTest { }

@Component({
template: `
<tr nxExpandableTableRow [isExpanded]="isExpanded">example content</tr>
`,
changeDetection: ChangeDetectionStrategy.OnPush
})
class ConfigurableExpandableTableRowComponent extends ExpandableTableRowTest {}

0 comments on commit 5916a1a

Please sign in to comment.