Skip to content

Commit

Permalink
feat(progressbar): add stacked progressbar (#4621)
Browse files Browse the repository at this point in the history
  • Loading branch information
bastienmoulia committed Nov 17, 2023
1 parent 8deee2e commit 1982133
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<ngb-progressbar-stacked>
<ngb-progressbar type="danger" [value]="20"></ngb-progressbar>
<ngb-progressbar type="warning" [value]="50"></ngb-progressbar>
<ngb-progressbar type="success" [value]="30"></ngb-progressbar>
</ngb-progressbar-stacked>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Component } from '@angular/core';
import { NgbProgressbarModule } from '@ng-bootstrap/ng-bootstrap';

@Component({
selector: 'ngbd-progressbar-stacked',
standalone: true,
imports: [NgbProgressbarModule],
templateUrl: './progressbar-stacked.html',
})
export class NgbdProgressbarStacked {}
8 changes: 8 additions & 0 deletions demo/src/app/components/progressbar/progressbar.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { NgbdProgressbarConfig } from './demos/config/progressbar-config';
import { NgbdProgressbarHeight } from './demos/height/progressbar-height';
import { NgbdProgressbarLabels } from './demos/labels/progressbar-labels';
import { NgbdProgressbarShowvalue } from './demos/showvalue/progressbar-showvalue';
import { NgbdProgressbarStacked } from './demos/stacked/progressbar-stacked';
import { NgbdProgressbarStriped } from './demos/striped/progressbar-striped';
import { NgbdProgressbarTextTypes } from './demos/texttypes/progressbar-texttypes';
import { Routes } from '@angular/router';
Expand Down Expand Up @@ -53,6 +54,13 @@ const DEMOS = [
code: 'progressbar/demos/height/progressbar-height.ts',
markup: 'progressbar/demos/height/progressbar-height.html',
},
{
fragment: 'stacked',
title: 'Progress bars stacked',
type: NgbdProgressbarStacked,
code: 'progressbar/demos/stacked/progressbar-stacked.ts',
markup: 'progressbar/demos/stacked/progressbar-stacked.html',
},
{
fragment: 'config',
title: 'Global configuration of progress bars',
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,12 @@ export {
NgbPaginationPages,
} from './pagination/pagination.module';
export { NgbPopover, NgbPopoverConfig, NgbPopoverModule } from './popover/popover.module';
export { NgbProgressbar, NgbProgressbarConfig, NgbProgressbarModule } from './progressbar/progressbar.module';
export {
NgbProgressbar,
NgbProgressbarConfig,
NgbProgressbarModule,
NgbProgressbarStacked,
} from './progressbar/progressbar.module';
export { NgbRating, NgbRatingConfig, NgbRatingModule } from './rating/rating.module';
export {
NgbScrollSpy,
Expand Down
8 changes: 4 additions & 4 deletions src/progressbar/progressbar.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { NgModule } from '@angular/core';

import { NgbProgressbar } from './progressbar';
import { NgbProgressbar, NgbProgressbarStacked } from './progressbar';

export { NgbProgressbar } from './progressbar';
export { NgbProgressbar, NgbProgressbarStacked } from './progressbar';
export { NgbProgressbarConfig } from './progressbar-config';

@NgModule({
imports: [NgbProgressbar],
exports: [NgbProgressbar],
imports: [NgbProgressbar, NgbProgressbarStacked],
exports: [NgbProgressbar, NgbProgressbarStacked],
})
export class NgbProgressbarModule {}
13 changes: 11 additions & 2 deletions src/progressbar/progressbar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createGenericTestComponent } from '../test/common';

import { Component } from '@angular/core';

import { NgbProgressbar } from './progressbar';
import { NgbProgressbar, NgbProgressbarStacked } from './progressbar';
import { NgbProgressbarConfig } from './progressbar-config';

const createTestComponent = (html: string) =>
Expand Down Expand Up @@ -301,6 +301,15 @@ describe('ngb-progressbar', () => {

expect(getAriaLabel(fixture.nativeElement)).toBe('flupke');
});

it('should set width on progress when inside <ngb-progressbar-stacked>', () => {
const html =
'<ngb-progressbar-stacked><ngb-progressbar [value]="50"></ngb-progressbar></ngb-progressbar-stacked>';
const fixture = createTestComponent(html);

expect(getBarWidth(fixture.nativeElement)).toBe('');
expect(getProgress(fixture.nativeElement).style.width).toBe('50%');
});
});

describe('Custom config', () => {
Expand Down Expand Up @@ -356,7 +365,7 @@ describe('ngb-progressbar', () => {
});
});

@Component({ selector: 'test-cmp', standalone: true, imports: [NgbProgressbar], template: '' })
@Component({ selector: 'test-cmp', standalone: true, imports: [NgbProgressbar, NgbProgressbarStacked], template: '' })
class TestComponent {
value = 10;
max = 50;
Expand Down
21 changes: 20 additions & 1 deletion src/progressbar/progressbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { PercentPipe } from '@angular/common';
'aria-valuemin': '0',
'[attr.aria-valuemax]': 'max',
'[attr.aria-label]': 'ariaLabel',
'[style.width.%]': 'stacked ? getPercentValue() : null',
},
template: `
<div
Expand All @@ -27,7 +28,7 @@ import { PercentPipe } from '@angular/common';
}}"
[class.progress-bar-animated]="animated"
[class.progress-bar-striped]="striped"
[style.width.%]="getPercentValue()"
[style.width.%]="!stacked ? getPercentValue() : null"
>
@if (showValue) {
<span i18n="@@ngb.progressbar.value">{{ getValue() / max | percent }}</span>
Expand All @@ -38,6 +39,7 @@ import { PercentPipe } from '@angular/common';
})
export class NgbProgressbar {
private _config = inject(NgbProgressbarConfig);
public stacked = inject(NgbProgressbarStacked, { optional: true });
private _max: number;

/**
Expand Down Expand Up @@ -122,3 +124,20 @@ export class NgbProgressbar {
return (100 * this.getValue()) / this.max;
}
}

/**
* A directive that allow to stack progress bars.
*
* @since 16.0.0
*/
@Component({
selector: 'ngb-progressbar-stacked',
standalone: true,
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
host: {
class: 'progress-stacked',
},
template: `<ng-content></ng-content>`,
})
export class NgbProgressbarStacked {}

0 comments on commit 1982133

Please sign in to comment.