Skip to content

Commit 2bf9ad8

Browse files
authored
feat(progressbar): value input can handle array of staked data (#2037)
* fix(progressbar): bs4 progressbar wip * refactor(progressbar): get rid of unnecessary input params, rework bs3 stacked demo * refactor(progressbar): string setters, rename vars, remove unnecessary operations
1 parent e7795f7 commit 2bf9ad8

File tree

9 files changed

+56
-49
lines changed

9 files changed

+56
-49
lines changed
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
<bs-progress>
2-
<bar *ngFor="let baz of stacked" [value]="baz.value" [type]="baz?.type">
3-
<span *ngIf="baz.value > 5">{{baz?.value}}%</span>
4-
</bar>
5-
</bs-progress>
6-
1+
<div class="row col-lg-12">
2+
<progressbar class="progress" [value]="stacked" max="100"></progressbar>
3+
</div>
4+
<br>
75
<div class="row col-md-12">
8-
<button type="button" class="btn btn-sm btn-primary" (click)="randomStacked()" style="margin: 15px 0;">Randomize
9-
</button>
6+
<button type="button" class="btn btn-sm btn-primary" (click)="randomStacked()">Randomize</button>
107
</div>

demo/src/app/components/+progressbar/demos/bs3/stacked/stacked.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { Component } from '@angular/core';
55
templateUrl: './stacked.html'
66
})
77
export class DemoProgressbarStackedComponent {
8-
public max: number = 200;
98
public type: string;
109
public stacked: any[] = [];
1110

@@ -17,16 +16,14 @@ export class DemoProgressbarStackedComponent {
1716
let types = ['success', 'info', 'warning', 'danger'];
1817

1918
this.stacked = [];
20-
let total = 0;
2119
let n = Math.floor((Math.random() * 4) + 1);
2220
for (let i = 0; i < n; i++) {
2321
let index = Math.floor((Math.random() * 4));
24-
let value = Math.floor((Math.random() * 30) + 1);
25-
total += value;
22+
let value = Math.floor((Math.random() * 27) + 3);
2623
this.stacked.push({
2724
value,
28-
max: value, // i !== (n - 1) ? value : 100,
29-
type: types[index]
25+
type: types[index],
26+
label: value + ' %'
3027
});
3128
}
3229
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
<progress class="progress progress-striped progress-animated" [max]="max" [value]="dynamic">
1+
<progressbar class="progress progress-striped progress-animated" [max]="max" [value]="dynamic">
22
<span style="color:white; white-space:nowrap;">{{dynamic}} / {{max}}</span>
3-
</progress>
3+
</progressbar>
44

55
<small><em>No animation</em></small>
6-
<progress class="progress progress-success" [value]="dynamic" [max]="100"><b>{{dynamic}}%</b></progress>
6+
<progressbar class="progress progress-success" [value]="dynamic" [max]="100" type="success"><b>{{dynamic}}%</b></progressbar>
77

88
<small><em>Object (changes type based on value)</em></small>
9-
<progress class="progress progress-striped progress-animated active progress-{{ type }}" [value]="dynamic" [max]="max">
9+
<progressbar class="progress progress-striped progress-animated" [value]="dynamic" [max]="max" [type]="type">
1010
{{type}} <i *ngIf="showWarning">!!! Watch out !!!</i>
11-
</progress>
11+
</progressbar>
1212

1313
<button type="button" class="btn btn-sm btn-primary" (click)="random()">Randomize</button>
Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
<div class="row col-lg-12">
2-
<!-- todo: implement as component -->
3-
<progress *ngFor="let baz of stacked"
4-
class="progress progress-{{ baz?.type }}"
5-
style="float: left;"
6-
[ngStyle]="{width: baz.value + '%'}"
7-
[value]="baz.value" [max]="baz.max">
8-
<span *ngIf="baz.value > 3">{{baz?.value}}%</span>
9-
</progress>
10-
<!-- gray background -->
11-
<progress class="progress" value="0" max="100" style="width: 100%;"></progress>
2+
<progressbar class="progress" [value]="stacked" max="100"></progressbar>
123
</div>
13-
4+
<br>
145
<div class="row col-md-12">
156
<button type="button" class="btn btn-sm btn-primary" (click)="randomStacked()">Randomize</button>
167
</div>

demo/src/app/components/+progressbar/demos/bs4/stacked/stacked.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { Component } from '@angular/core';
55
templateUrl: './stacked.html'
66
})
77
export class DemoProgressbarStackedBs4Component {
8-
public max: number = 200;
98
public type: string;
109
public stacked: any[] = [];
1110

@@ -17,16 +16,14 @@ export class DemoProgressbarStackedBs4Component {
1716
let types = ['success', 'info', 'warning', 'danger'];
1817

1918
this.stacked = [];
20-
let total = 0;
2119
let n = Math.floor((Math.random() * 4) + 1);
2220
for (let i = 0; i < n; i++) {
2321
let index = Math.floor((Math.random() * 4));
24-
let value = Math.floor((Math.random() * 30) + 1);
25-
total += value;
22+
let value = Math.floor((Math.random() * 27) + 3);
2623
this.stacked.push({
2724
value,
28-
max: value, // i !== (n - 1) ? value : 100,
29-
type: types[index]
25+
type: types[index],
26+
label: value + ' %'
3027
});
3128
}
3229
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<div class="row">
22
<div class="col-sm-4">
3-
<progress class="progress" value="55" max="100"></progress>
3+
<progressbar class="progress" value="55" max="100"></progressbar>
44
</div>
55
<div class="col-sm-4">
6-
<progress class="progress progress-striped" value="22" max="100" type="warning">22%</progress>
6+
<progressbar class="progress progress-striped" value="22" max="100" type="warning">22%</progressbar>
77
</div>
88
<div class="col-sm-4">
9-
<progress class="progress progress-striped active" max="200" value="166" type="danger"><i>166 / 200</i></progress>
9+
<progressbar class="progress progress-striped active" max="200" value="166" type="danger"><i>166 / 200</i></progressbar>
1010
</div>
1111
</div>

demo/src/ng-api-doc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,8 +1443,8 @@ export const ngdoc: any = {
14431443
},
14441444
{
14451445
"name": "value",
1446-
"type": "number",
1447-
"description": "<p>current value of progress bar </p>\n"
1446+
"type": "number | any[]",
1447+
"description": "<p>current value of progress bar. Could be a number or array of objects like {&quot;value&quot;:15,&quot;type&quot;:&quot;info&quot;,&quot;label&quot;:&quot;15 %&quot;} </p>\n"
14481448
}
14491449
],
14501450
"outputs": [],

src/progressbar/bar.component.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { Component, Host, Input, OnDestroy, OnInit } from '@angular/core';
1+
import { Component, Host, HostBinding, Input, OnDestroy, OnInit } from '@angular/core';
22

33
import { ProgressDirective } from './progress.directive';
4+
import { isBs3 } from '../utils/ng2-bootstrap-config';
45

56
// todo: number pipe
67
// todo: use query from progress?
@@ -10,8 +11,8 @@ import { ProgressDirective } from './progress.directive';
1011
<div class="progress-bar"
1112
style="min-width: 0;"
1213
role="progressbar"
13-
[ngClass]="type && 'progress-bar-' + type"
14-
[ngStyle]="{width: (percent < 100 ? percent : 100) + '%', transition: transition}"
14+
[ngClass]="type && 'progress-bar-' + type + ' bg-' + type"
15+
[ngStyle]="{width: (isBs3 ? (percent < 100 ? percent : 100) + '%' : '100%'), transition: transition}"
1516
aria-valuemin="0"
1617
[attr.aria-valuenow]="value"
1718
[attr.aria-valuetext]="percent.toFixed(0) + '%'"
@@ -28,6 +29,10 @@ export class BarComponent implements OnInit, OnDestroy {
2829
public get value():number {
2930
return this._value;
3031
}
32+
@HostBinding('style.width.%') get setBarWidth(){
33+
this.recalculatePercentage();
34+
return this.isBs3 ? '' : this.percent;
35+
};
3136

3237
public set value(v:number) {
3338
if (!v && v !== 0) {
@@ -37,6 +42,9 @@ export class BarComponent implements OnInit, OnDestroy {
3742
this.recalculatePercentage();
3843
}
3944

45+
public get isBs3(): boolean {
46+
return isBs3();
47+
}
4048
public percent:number = 0;
4149
public transition:string;
4250
public progress:ProgressDirective;

src/progressbar/progressbar.component.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
import { Component, Input } from '@angular/core';
22
import { ProgressbarConfig } from './progressbar.config';
3+
import { isBs3 } from '../utils';
34

45
@Component({
56
selector: 'progressbar',
67
template: `
7-
<div progress [animate]="animate" [max]="max">
8-
<bar [type]="type" [value]="value">
8+
<div progress [animate]="animate" [max]="max" [style.width]="!isBs3 ? '100%' : 'auto'">
9+
<bar [type]="type" [value]="_value" *ngIf="!isStacked">
910
<ng-content></ng-content>
1011
</bar>
12+
<template [ngIf]="isStacked">
13+
<bar *ngFor="let item of _value" [type]="item.type" [value]="item.value">{{item.label}}</bar>
14+
</template>
1115
</div>
12-
`
16+
`,
17+
styles: [`
18+
:host {
19+
width: 100%;
20+
}
21+
`]
1322
})
1423
export class ProgressbarComponent {
1524
/** if `true` changing value of progress bar will be animated (note: not supported by Bootstrap 4) */
@@ -18,8 +27,16 @@ export class ProgressbarComponent {
1827
@Input() public max:number;
1928
/** provide one of the four supported contextual classes: `success`, `info`, `warning`, `danger` */
2029
@Input() public type:string;
21-
/** current value of progress bar */
22-
@Input() public value:number;
30+
/** current value of progress bar. Could be a number or array of objects like {"value":15,"type":"info","label":"15 %"} */
31+
@Input() public set value(value: number | any[]) {
32+
this.isStacked = Array.isArray(value);
33+
this._value = value;
34+
};
35+
public isStacked: boolean = false;
36+
public _value: number | any[];
37+
public get isBs3(): boolean {
38+
return isBs3();
39+
}
2340

2441
public constructor(config: ProgressbarConfig) {
2542
Object.assign(this, config);

0 commit comments

Comments
 (0)