Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
Added expand and collapse events to repeater item component. (#25)
Browse files Browse the repository at this point in the history
* Add `expand` and `collapse` events to `SkyRepeaterItemComponent`.

* Use `emit` instead of `next`.
  • Loading branch information
Blackbaud-SteveBrush authored Mar 7, 2019
1 parent 1934af4 commit 698a0e9
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 8 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
},
"dependencies": {},
"devDependencies": {
"@blackbaud/skyux": "2.38.0",
"@blackbaud/skyux-builder": "1.30.0",
"@skyux-sdk/builder-plugin-skyux": "1.0.0-rc.5"
"@blackbaud/skyux": "2.42.0",
"@blackbaud/skyux-builder": "1.31.1",
"@skyux-sdk/builder-plugin-skyux": "1.0.0-rc.6"
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<sky-repeater [expandMode]="expandMode">
<sky-repeater-item>
<sky-repeater-item
(collapse)="onCollapse()"
(expand)="onExpand()"
>
<sky-repeater-item-context-menu *ngIf="showContextMenu">
a
</sky-repeater-item-context-menu>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ export class RepeaterTestComponent {
public lastItemExpanded: boolean;

public lastItemSelected = false;

public onCollapse(): void {}

public onExpand(): void {}
}
24 changes: 22 additions & 2 deletions src/app/public/modules/repeater/repeater-item.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import {
ChangeDetectorRef,
Component,
Input
EventEmitter,
Input,
OnDestroy,
Output
} from '@angular/core';

import {
Expand All @@ -28,7 +31,7 @@ let nextId: number = 0;
templateUrl: './repeater-item.component.html',
animations: [skyAnimationSlide]
})
export class SkyRepeaterItemComponent {
export class SkyRepeaterItemComponent implements OnDestroy {
public contentId: string = `sky-radio-content-${++nextId}`;

public get isExpanded(): boolean {
Expand All @@ -52,6 +55,12 @@ export class SkyRepeaterItemComponent {
@Input()
public selectable: boolean = false;

@Output()
public collapse = new EventEmitter<void>();

@Output()
public expand = new EventEmitter<void>();

public slideDirection: string;

public get isCollapsible(): boolean {
Expand Down Expand Up @@ -82,6 +91,11 @@ export class SkyRepeaterItemComponent {
this.slideForExpanded(false);
}

public ngOnDestroy(): void {
this.collapse.complete();
this.expand.complete();
}

public headerClick() {
if (this.isCollapsible) {
this.updateForExpanded(!this.isExpanded, true);
Expand All @@ -101,6 +115,12 @@ export class SkyRepeaterItemComponent {
} else if (this._isExpanded !== value) {
this._isExpanded = value;

if (this._isExpanded) {
this.expand.emit();
} else {
this.collapse.emit();
}

this.repeaterService.onItemCollapseStateChange(this);
this.slideForExpanded(animate);
this.changeDetector.markForCheck();
Expand Down
33 changes: 33 additions & 0 deletions src/app/public/modules/repeater/repeater.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,39 @@ describe('Repeater item component', () => {
expect(fixture.nativeElement).toBeAccessible();
});
}));

it('should emit events when item is expanded/collapsed', fakeAsync(() => {
const fixture = TestBed.createComponent(RepeaterTestComponent);
const cmp = fixture.componentInstance;
cmp.expandMode = 'single';

const collapseSpy = spyOn(cmp, 'onCollapse').and.callThrough();
const expandSpy = spyOn(cmp, 'onExpand').and.callThrough();

fixture.detectChanges();
tick();

collapseSpy.calls.reset();
expandSpy.calls.reset();

let repeaterItems = cmp.repeater.items.toArray();
expect(repeaterItems[0].isExpanded).toBe(true);

fixture.nativeElement.querySelectorAll('.sky-chevron').item(0).click();
fixture.detectChanges();
tick();

expect(collapseSpy).toHaveBeenCalled();

collapseSpy.calls.reset();
expandSpy.calls.reset();

fixture.nativeElement.querySelectorAll('.sky-chevron').item(0).click();
fixture.detectChanges();
tick();

expect(expandSpy).toHaveBeenCalled();
}));
});

describe('with expand mode of "multiple"', () => {
Expand Down
7 changes: 6 additions & 1 deletion src/app/visual/repeater/repeater-visual.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@
id="screenshot-repeater-selectable"
>
<sky-repeater [expandMode]="'single'">
<sky-repeater-item [isExpanded]="false" [selectable]="true">
<sky-repeater-item
[isExpanded]="false"
[selectable]="true"
(collapse)="onCollapse()"
(expand)="onExpand()"
>
<sky-repeater-item-title>Title 1</sky-repeater-item-title>
<sky-repeater-item-content>Content 1</sky-repeater-item-content>
</sky-repeater-item>
Expand Down
10 changes: 9 additions & 1 deletion src/app/visual/repeater/repeater-visual.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,12 @@ import { Component } from '@angular/core';
templateUrl: './repeater-visual.component.html',
styleUrls: ['./repeater-visual.component.scss']
})
export class RepeaterVisualComponent { }
export class RepeaterVisualComponent {
public onCollapse(): void {
console.log('Collapsed.');
}

public onExpand(): void {
console.log('Expanded.');
}
}

0 comments on commit 698a0e9

Please sign in to comment.