Skip to content

Commit a80feec

Browse files
committed
feat(ExpandableListItemComponent): isExpanded property
isExpanded property support to bind the status of the list-item element; removed mandatory `item` attribute into the `expandable-list-divider` directive closes #4
1 parent 431059b commit a80feec

File tree

7 files changed

+132
-35
lines changed

7 files changed

+132
-35
lines changed

README.md

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@ Plunker DEMO available [here](https://embed.plnkr.co/uAPJq0/)
1616

1717
## Table of contents
1818

19-
* [Installation](#installation)
20-
* [Setup](#setup)
21-
* [Usage](#usage)
22-
* [Design your list](#design-your-list)
23-
* [Demo App](#demo-app)
24-
* [Contributing](#contributing)
25-
* [Changelog](#changelog)
26-
* [License](#license)
19+
* [Installation](#installation)
20+
* [Setup](#setup)
21+
* [Usage](#usage)
22+
* [Design your list](#design-your-list)
23+
* [Demo App](#demo-app)
24+
* [Contributing](#contributing)
25+
* [Options](#options)
26+
* [Properties](#properties)
27+
* [Attributes](#attributes)
28+
* [Changelog](#changelog)
29+
* [License](#license)
2730

2831
## Installation
2932

@@ -104,6 +107,7 @@ import { Component } from '@angular/core';
104107
<span title>My List</span>
105108
<a item href="http://www.goo.gl">Google</a>
106109
<a item href="http://www.goo.gl">Google</a>
110+
<expandable-list-divider></expandable-list-divider>
107111
<a item href="http://www.goo.gl">Google</a>
108112
</expandable-list-item>
109113
</expandable-list>
@@ -158,7 +162,7 @@ Now you're ready to fill your list with some items.
158162
```
159163

160164
It is important to append the html attribute `item` to each of the item you want
161-
to render in the list and it's up to you which HTML tag to use. We've just filled
165+
to be rendered in the list and it's up to you which HTML tag to use. We've just filled
162166
our list with anchor link but feel free to use any other tag.
163167

164168
### Dividers
@@ -170,15 +174,12 @@ our list with anchor link but feel free to use any other tag.
170174
<span secondary>Secondary</span>
171175
<a item href="http://www.goo.gl">Google</a>
172176
<a item href="http://www.goo.gl">Google</a>
173-
<expandable-list-divider item></expandable-list-divider>
177+
<expandable-list-divider></expandable-list-divider>
174178
<a item href="http://www.goo.gl">Google</a>
175179
</expandable-list-item>
176180
</expandable-list>
177181
```
178182

179-
We still need to postfix the `expandable-list-divider` with the `item` attribute to
180-
see that divider rendered on the page.
181-
182183
### Create more
183184

184185
Now you can create as many `<expandable-list-item>` nodes as you want inside
@@ -191,7 +192,7 @@ your `<expandable-list>`.
191192
<span secondary>Secondary</span>
192193
<a item href="http://www.goo.gl">Google</a>
193194
<a item href="http://www.goo.gl">Google</a>
194-
<expandable-list-divider item></expandable-list-divider>
195+
<expandable-list-divider></expandable-list-divider>
195196
<a item href="http://www.goo.gl">Google</a>
196197
</expandable-list-item>
197198

@@ -217,6 +218,46 @@ $ npm run demo
217218
Open your browser to [http://localhost:9007/](http://localhost:9007/)
218219
to see the application running.
219220

221+
## Options
222+
223+
## Properties
224+
225+
### isExpanded
226+
227+
| Type | Default value |
228+
| --- | --- |
229+
| boolean | false |
230+
231+
Used for interacting with the `expandable-list-item` element.
232+
233+
Example:
234+
235+
```ts
236+
@Component({
237+
selector: 'app',
238+
template: `
239+
<expandable-list class="expandable-list">
240+
<expandable-list-item [isExpanded]="listExpanded">
241+
<span title>My list</span>
242+
<a item href="http://www.goo.gl">Google</a>
243+
<a item href="http://www.goo.gl">Google</a>
244+
<a item href="http://www.goo.gl">Google</a>
245+
</expandable-list-item>
246+
</expandable-list>
247+
`
248+
})
249+
export class AppComponent {
250+
listExpanded = true;
251+
}
252+
```
253+
254+
## Attributes
255+
256+
| Name | Type | Description |
257+
| --- | --- | --- |
258+
| disabled | boolean | When applied to the `expandable-list-item`, disabled the
259+
| is-expanded | boolean | Reflect the expandable state of the item list element
260+
220261
## Contributing
221262

222263
This package is using the AngularJS commit messages as default way to contribute

config/webpack.demo.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,30 @@ module.exports = {
5151
},
5252

5353
{
54-
test: /\.(css|html)?$/,
54+
test: /\.css$/,
5555
use: [
5656
{
5757
loader: 'raw-loader'
5858
},
59+
{
60+
loader: 'postcss-loader',
61+
options: {
62+
plugins: function() {
63+
return [
64+
require('autoprefixer')({
65+
browsers: ['last 3 versions', '> 2%']
66+
})
67+
]
68+
}
69+
}
70+
},
5971
]
6072
},
73+
74+
{
75+
test: /\.html$/,
76+
use: [ 'raw-loader' ]
77+
},
6178
]
6279
},
6380

demo/app.component.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { Component } from '@angular/core';
1717
`],
1818
template: `
1919
<expandable-list class="expandable-list">
20-
<expandable-list-item *ngFor="let item of items">
20+
<expandable-list-item *ngFor="let item of items" [isExpanded]="listAExpanded">
2121
<span title>{{ item.title }}</span>
2222
<a item *ngFor="let i of item.items"
2323
[href]="i.link">
@@ -30,28 +30,30 @@ import { Component } from '@angular/core';
3030
<span secondary>disabled</span>
3131
</expandable-list-item>
3232
33-
<expandable-list-item>
33+
<expandable-list-item [isExpanded]="listCExpanded">
3434
<span title>Manual</span>
3535
<span secondary>first</span>
36-
<a href="http://www.goo.gl">Something else</a>
36+
37+
<a href="http://www.goo.gl">THIS WON'T BE RENDERED!</a>
38+
3739
<a item href="http://www.goo.gl">Google</a>
3840
<a item href="http://www.goo.gl">Google</a>
3941
<a item href="http://www.goo.gl">Google</a>
4042
41-
<expandable-list-divider item></expandable-list-divider>
43+
<expandable-list-divider></expandable-list-divider>
4244
43-
<a item href="http://www.goo.gl">Google</a>
45+
<a item href="http://www.goo.gl">Google after</a>
4446
</expandable-list-item>
4547
46-
<expandable-list-item>
48+
<expandable-list-item [isExpanded]="listBExpanded" disabled>
4749
<span title>Manual</span>
4850
<span secondary>second</span>
4951
<a href="http://www.goo.gl">Something else</a>
5052
<a item href="http://www.goo.gl">Google</a>
5153
<a item href="http://www.goo.gl">Google</a>
5254
<a item href="http://www.goo.gl">Google</a>
5355
54-
<expandable-list-divider item></expandable-list-divider>
56+
<expandable-list-divider></expandable-list-divider>
5557
5658
<a item href="http://www.goo.gl">Google</a>
5759
</expandable-list-item>
@@ -60,6 +62,9 @@ import { Component } from '@angular/core';
6062
})
6163
export class AppComponent {
6264
items: any;
65+
listAExpanded = false;
66+
listBExpanded = true;
67+
listCExpanded = false;
6368

6469
constructor() {
6570
this.items = [

src/expandable-list-item.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@
2828
[ngStyle]="{'margin-top': marginTop }"
2929
[attr.aria-hidden]="!isExpanded"
3030
#contentEl>
31-
<ng-content select="[item]"></ng-content>
31+
<ng-content select="[item], expandable-list-divider"></ng-content>
3232
</div>
3333
</div>

src/expandable-list.component.ts

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1+
/**
2+
* angular2-expandable-list
3+
*
4+
* Copyright 2017, @andreasonny83, All rights reserved.
5+
*
6+
* @author: @andreasonny83 <andreasonny83@gmail.com>
7+
*/
8+
19
import {
210
Component,
311
Directive,
412
ViewEncapsulation,
513
HostBinding,
614
Input,
715
AfterViewInit,
16+
OnChanges,
17+
SimpleChanges,
818
ElementRef,
919
ViewChild,
1020
} from '@angular/core';
@@ -42,12 +52,17 @@ export class ExpandableListDividerStyler { }
4252
'[class.expandable-list-item]': 'true',
4353
},
4454
templateUrl: './expandable-list-item.html',
45-
encapsulation: ViewEncapsulation.None
55+
encapsulation: ViewEncapsulation.None,
4656
})
47-
export class ExpandableListItemComponent implements AfterViewInit {
48-
public isExpanded: boolean;
57+
export class ExpandableListItemComponent implements AfterViewInit, OnChanges {
4958
public marginTop: string;
5059

60+
@Input('isExpanded')
61+
get isExpanded() { return this.expanded; }
62+
set isExpanded(value: boolean) {
63+
this.expanded = (value !== null && `${value}` !== 'false') ? true : false;
64+
}
65+
5166
@Input('disabled')
5267
get disabled() { return this.isDisabled; }
5368
set disabled(value: boolean) {
@@ -62,13 +77,17 @@ export class ExpandableListItemComponent implements AfterViewInit {
6277
@HostBinding('attr.disabled')
6378
private isDisabled: boolean;
6479

65-
constructor() {
66-
this.isExpanded = false;
67-
}
80+
@HostBinding('attr.is-expanded')
81+
private expanded: boolean = false;
6882

6983
public ngAfterViewInit() {
7084
this.elHeight = this.elementView.nativeElement.offsetHeight;
71-
this.marginTop = `-${this.elHeight}px`;
85+
}
86+
87+
public ngOnChanges(changes: SimpleChanges) {
88+
if ('isExpanded' in changes) {
89+
this.updateMarginTop();
90+
}
7291
}
7392

7493
public onClick() {
@@ -78,6 +97,10 @@ export class ExpandableListItemComponent implements AfterViewInit {
7897

7998
this.isExpanded = !this.isExpanded;
8099

100+
this.updateMarginTop();
101+
}
102+
103+
private updateMarginTop() {
81104
if (!this.isExpanded) {
82105
this.marginTop = `-${this.elHeight}px`;
83106
} else {

src/expandable-list.css

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99
background: #FFF;
1010
}
1111

12-
.expandable-list [disabled].expandable-list-item {
12+
.expandable-list .expandable-list-item[disabled],
13+
.expandable-list .expandable-list-item[disabled] * {
14+
pointer-events: none;
15+
cursor: default;
16+
}
17+
18+
.expandable-list .expandable-list-item[disabled].expandable-list-item {
1319
background: #eeeeee;
1420
}
1521

@@ -28,10 +34,6 @@
2834
cursor: pointer;
2935
}
3036

31-
.expandable-list [disabled].expandable-list-item .expandable-list-item-title {
32-
cursor: default;
33-
}
34-
3537
.expandable-list .expandable-list-item .expandable-list-item-title .expandable-list-item--title,
3638
.expandable-list .expandable-list-item .expandable-list-item-title .expandable-list-item--secondary {
3739
flex: 1;
@@ -110,6 +112,7 @@
110112
font-size: 16px;
111113
opacity: 0;
112114
pointer-events: none;
115+
will-change: margin-top, opacity;
113116
transition: opacity 0.15s ease-out,
114117
margin-top 0.3s ease-out;
115118
}

src/expandable-list.module.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/**
2+
* angular2-expandable-list
3+
*
4+
* Copyright 2017, @andreasonny83, All rights reserved.
5+
*
6+
* @author: @andreasonny83 <andreasonny83@gmail.com>
7+
*/
8+
19
import { NgModule } from '@angular/core';
210
import { CommonModule } from '@angular/common';
311
import {

0 commit comments

Comments
 (0)