Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(progressbar): display progressbar correctly for invalid max value #3400

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/progressbar/progressbar.spec.ts
Expand Up @@ -58,6 +58,48 @@ describe('ngb-progressbar', () => {
expect(progressCmp.getPercentValue()).toBe(20);
});

it('should calculate the percentage (custom max size of null)', () => {
progressCmp.max = null;

progressCmp.value = 25;
expect(progressCmp.getPercentValue()).toBe(25);
});

it('should calculate the percentage (custom max size of undefined)', () => {
progressCmp.max = undefined;

progressCmp.value = 25;
expect(progressCmp.getPercentValue()).toBe(25);
});

it('should calculate the percentage (custom max size of zero)', () => {
progressCmp.max = 0;

progressCmp.value = 25;
expect(progressCmp.getPercentValue()).toBe(25);
});

it('should calculate the percentage (custom negative max size)', () => {
progressCmp.max = -10;

progressCmp.value = 25;
expect(progressCmp.getPercentValue()).toBe(25);
});

it('should calculate the percentage (custom max size of positive infinity)', () => {
progressCmp.max = Number.POSITIVE_INFINITY;

progressCmp.value = 25;
expect(progressCmp.getPercentValue()).toBe(25);
});

it('should calculate the percentage (custom max size of negative infinity)', () => {
progressCmp.max = Number.NEGATIVE_INFINITY;

progressCmp.value = 25;
expect(progressCmp.getPercentValue()).toBe(25);
});

it('should set the value to 0 for negative numbers', () => {
progressCmp.value = -20;
expect(progressCmp.getValue()).toBe(0);
Expand Down
13 changes: 11 additions & 2 deletions src/progressbar/progressbar.ts
@@ -1,5 +1,5 @@
import {Component, Input, ChangeDetectionStrategy} from '@angular/core';
import {getValueInRange} from '../util/util';
import {getValueInRange, isNumber} from '../util/util';
import {NgbProgressbarConfig} from './progressbar-config';

/**
Expand All @@ -19,10 +19,19 @@ import {NgbProgressbarConfig} from './progressbar-config';
`
})
export class NgbProgressbar {
private _max: number;

/**
* The maximal value to be displayed in the progress bar.
*
* Should be a positive number. Will default to 100 otherwise.
*/
@Input() max: number;
@Input()
set max(max: number) {
this._max = !isNumber(max) || max <= 0 ? 100 : max;
}

get max(): number { return this._max; }

/**
* If `true`, the stripes on the progress bar are animated.
Expand Down