Skip to content

Commit

Permalink
feat(core): onExpanded
Browse files Browse the repository at this point in the history
onExpanded event is triggered when the list status changes

Closes closes #7
  • Loading branch information
andreasonny83 committed Sep 10, 2017
1 parent 749d4fc commit 22aeb11
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 100 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,32 @@ export class AppComponent {
| disabled | boolean | When applied to the `expandable-list-item`, disabled the
| is-expanded | boolean | Reflect the expandable state of the item list element

## Events

| Name | Type | Description |
| --- | --- | --- |
| onExpanded | boolean | Triggered when the list expansion status changes

Example:

```ts
@Component({
selector: 'app',
template: `
<expandable-list class="expandable-list">
<expandable-list-item [isExpanded]="listExpanded"
(onExpanded)="listExpanded = $event">
<span title>My list</span>
<a item href="http://www.goo.gl">Google</a>
</expandable-list-item>
</expandable-list>
`
})
export class AppComponent {
listExpanded = true;
}
```

## Angular 4

This module will work with Angular 4 projects but requires `@angular/animations`
Expand Down
22 changes: 22 additions & 0 deletions demo/app.component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
:host {
background: rgb(184, 218, 215);
display: block;
height: 100%;
padding-top: 50px;
box-sizing: border-box;
}

.content-wrapper {
max-width: 600px;
margin: 0 auto;
}

.button {
margin: 10px 0;
border: 1px solid #333;
background: #eee;
color: #000;
font-size: 14px;
border-radius: 4px;
padding: 5px 15px;
}
50 changes: 50 additions & 0 deletions demo/app.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<div class="content-wrapper">
<button class="button"
(click)="toggleMe()"> Toggle List</button>
<p>List status: {{ listAExpanded ? 'Expanded' : 'Collapsed' }}</p>

<expandable-list class="expandable-list">
<expandable-list-item *ngFor="let item of items"
[isExpanded]="listAExpanded"
(onExpanded)="updateStatus($event)">
<span title>{{ item.title }}</span>
<a item *ngFor="let i of item.items"
[href]="i.link">
{{ i.title }}
</a>
</expandable-list-item>

<expandable-list-item disabled>
<span title>Manual</span>
<span secondary>disabled</span>
</expandable-list-item>

<expandable-list-item [isExpanded]="listCExpanded">
<span title>Manual</span>
<span secondary>first</span>

<a href="http://www.goo.gl">THIS WON'T BE RENDERED!</a>

<a item href="http://www.goo.gl">Google</a>
<a item href="http://www.goo.gl">Google</a>
<a item href="http://www.goo.gl">Google</a>

<expandable-list-divider></expandable-list-divider>

<a item href="http://www.goo.gl">Google after</a>
</expandable-list-item>

<expandable-list-item [isExpanded]="listBExpanded" disabled>
<span title>Manual</span>
<span secondary>second</span>
<a href="http://www.goo.gl">Something else</a>
<a item href="http://www.goo.gl">Google</a>
<a item href="http://www.goo.gl">Google</a>
<a item href="http://www.goo.gl">Google</a>

<expandable-list-divider></expandable-list-divider>

<a item href="http://www.goo.gl">Google</a>
</expandable-list-item>
</expandable-list>
</div>
141 changes: 45 additions & 96 deletions demo/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,107 +1,56 @@
import { Component } from '@angular/core';
import { Component, OnInit } from '@angular/core';

@Component({
selector: 'app',
styles: [`
:host {
background: rgb(184, 218, 215);
display: block;
height: 100%;
padding-top: 50px;
box-sizing: border-box;
}
.expandable-list {
max-width: 600px;
margin: 0 auto;
}
`],
template: `
<expandable-list class="expandable-list">
<expandable-list-item *ngFor="let item of items" [isExpanded]="listAExpanded">
<span title>{{ item.title }}</span>
<a item *ngFor="let i of item.items"
[href]="i.link">
{{ i.title }}
</a>
</expandable-list-item>
<expandable-list-item disabled>
<span title>Manual</span>
<span secondary>disabled</span>
</expandable-list-item>
<expandable-list-item [isExpanded]="listCExpanded">
<span title>Manual</span>
<span secondary>first</span>
<a href="http://www.goo.gl">THIS WON'T BE RENDERED!</a>
<a item href="http://www.goo.gl">Google</a>
<a item href="http://www.goo.gl">Google</a>
<a item href="http://www.goo.gl">Google</a>
<expandable-list-divider></expandable-list-divider>
<a item href="http://www.goo.gl">Google after</a>
</expandable-list-item>
<expandable-list-item [isExpanded]="listBExpanded" disabled>
<span title>Manual</span>
<span secondary>second</span>
<a href="http://www.goo.gl">Something else</a>
<a item href="http://www.goo.gl">Google</a>
<a item href="http://www.goo.gl">Google</a>
<a item href="http://www.goo.gl">Google</a>
<expandable-list-divider></expandable-list-divider>
<a item href="http://www.goo.gl">Google</a>
</expandable-list-item>
</expandable-list>
`
styleUrls: ['./app.component.css'],
templateUrl: './app.component.html',
})
export class AppComponent {
export class AppComponent implements OnInit {
items: any;
listAExpanded = false;
listBExpanded = true;
listCExpanded = false;

constructor() {
this.items = [
{
title: 'Spices',
items: [
{
title: 'salt',
link: 'https://en.wikipedia.org/wiki/Salt'
},
{
title: 'Black pepper',
link: 'https://en.wikipedia.org/wiki/Black_pepper'
},
]
},
{
title: 'Cats',
items: [
{
title: 'Siberian',
link: 'https://en.wikipedia.org/wiki/Siberian_cat'
},
{
title: 'Maine Coon',
link: 'https://en.wikipedia.org/wiki/Maine_Coon'
},
{
title: 'American Bobtail',
link: 'https://en.wikipedia.org/wiki/American_Bobtail'
},
{
title: 'British Longhair',
link: 'https://en.wikipedia.org/wiki/British_Longhair'
},
]
},
];
constructor() { }

ngOnInit(): void {
this.items = [{
title: 'Spices',
items: [
{
title: 'salt',
link: 'https://en.wikipedia.org/wiki/Salt'
}, {
title: 'Black pepper',
link: 'https://en.wikipedia.org/wiki/Black_pepper'
}
]
}, {
title: 'Cats',
items: [
{
title: 'Siberian',
link: 'https://en.wikipedia.org/wiki/Siberian_cat'
}, {
title: 'Maine Coon',
link: 'https://en.wikipedia.org/wiki/Maine_Coon'
}, {
title: 'American Bobtail',
link: 'https://en.wikipedia.org/wiki/American_Bobtail'
}, {
title: 'British Longhair',
link: 'https://en.wikipedia.org/wiki/British_Longhair'
}
]
}];
}

toggleMe(): void {
this.listAExpanded = !this.listAExpanded;
}

updateStatus(event: boolean) {
console.log(event);
this.listAExpanded = event;
}
}
1 change: 0 additions & 1 deletion demo/main.map

This file was deleted.

22 changes: 19 additions & 3 deletions src/expandable-list.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('ExpandableListModule', () => {
expect(itemContent).toBeTruthy();
});

it('The title should contatin a label and a drop-down button', () => {
it('the title should contatin a label and a drop-down button', () => {
fixture.detectChanges();
let itemTitle = listItemDe.query(By.css('.expandable-list-item-title .expandable-list-item--title'));
let titleLabel = itemTitle.query(By.css('span'));
Expand Down Expand Up @@ -101,7 +101,7 @@ describe('ExpandableListModule', () => {
});

it('user interaction', () => {
let testComponent = fixture.debugElement.componentInstance;
let testComponent: TestApp = fixture.debugElement.componentInstance;

fixture.detectChanges();
let itemTitle = listItemDe.query(By.css('.expandable-list-item-title'));
Expand All @@ -124,6 +124,20 @@ describe('ExpandableListModule', () => {
.toBe('false', 'list item element collapsed');
});

it('should trigger an event when the status change', () => {
let testComponent: TestApp = fixture.debugElement.componentInstance;
let itemTitle = listItemDe.query(By.css('.expandable-list-item-title'));

fixture.detectChanges();

expect(testComponent.listExpanded).toBe(false);

itemTitle.nativeElement.click();
fixture.detectChanges();

expect(testComponent.listExpanded).toBe(true);
});

it('user interaction on a disabled list', () => {
let fixture2 = TestBed.createComponent(TestApp2);
let listItem2De = fixture2.debugElement.query(By.css('expandable-list-item'));
Expand All @@ -148,7 +162,9 @@ describe('ExpandableListModule', () => {
selector: 'test-app',
template: `
<expandable-list class="expandable-list">
<expandable-list-item [isExpanded]="listExpanded" [disabled]="listDisabled">
<expandable-list-item [isExpanded]="listExpanded"
[disabled]="listDisabled"
(onExpanded)="listExpanded = $event">
<span title>Title</span>
<span secondary>secondary</span>
<a href="http://www.goo.gl">Something else</a>
Expand Down
11 changes: 11 additions & 0 deletions src/expandable-list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import {
ViewEncapsulation,
HostBinding,
Input,
Output,
AfterViewInit,
OnChanges,
SimpleChanges,
ElementRef,
ViewChild,
EventEmitter,
} from '@angular/core';

@Component({
Expand Down Expand Up @@ -69,6 +71,9 @@ export class ExpandableListItemComponent implements AfterViewInit, OnChanges {
this.isDisabled = (value !== null && `${value}` !== 'false') ? true : null;
}

@Output()
public onExpanded: EventEmitter<boolean>;

private elHeight: number;

@ViewChild('contentEl')
Expand All @@ -84,6 +89,10 @@ export class ExpandableListItemComponent implements AfterViewInit, OnChanges {
this.elHeight = this.elementView.nativeElement.offsetHeight;
}

constructor() {
this.onExpanded = new EventEmitter<boolean>();
}

public ngOnChanges(changes: SimpleChanges) {
if ('isExpanded' in changes) {
this.updateMarginTop();
Expand All @@ -106,5 +115,7 @@ export class ExpandableListItemComponent implements AfterViewInit, OnChanges {
} else {
this.marginTop = '0';
}

this.onExpanded.emit(this.isExpanded);
}
}

0 comments on commit 22aeb11

Please sign in to comment.