File size: 1,861 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
import clsx from 'clsx';
import * as React from 'react';
import ScreenReaderText from '../screen-reader-text';
import './style.scss';
export interface Props {
value: number;
total: number;
color?: string;
title?: string;
compact: boolean;
className?: string;
isPulsing: boolean;
canGoBackwards: boolean;
style?: React.CSSProperties;
}
interface State {
allTimeMax: number;
}
export default class ProgressBar extends React.PureComponent< Props, State > {
static defaultProps = {
total: 100,
compact: false,
isPulsing: false,
canGoBackwards: false,
};
static getDerivedStateFromProps( props: Props, state: State ) {
return {
allTimeMax: Math.max( state.allTimeMax, props.value ),
};
}
state: State = {
allTimeMax: this.props.value,
};
getCompletionPercentage() {
const percentage = Math.ceil(
( ( this.props.canGoBackwards ? this.props.value : this.state.allTimeMax ) /
this.props.total ) *
100
);
// The percentage should not be allowed to be more than 100
return Math.min( percentage, 100 );
}
renderBar() {
const { color, title, total, value, style } = this.props;
let styles: React.CSSProperties = { width: this.getCompletionPercentage() + '%' };
if ( color ) {
styles.backgroundColor = color;
}
if ( style ) {
styles = { ...styles, ...style };
}
return (
<div
aria-valuemax={ total }
aria-valuemin={ 0 }
aria-valuenow={ value }
aria-label="progress bar"
className="progress-bar__progress"
role="progressbar"
style={ styles }
>
{ title && <ScreenReaderText>{ title }</ScreenReaderText> }
</div>
);
}
render() {
const classes = clsx( this.props.className, 'progress-bar', {
'is-compact': this.props.compact,
'is-pulsing': this.props.isPulsing,
} );
return <div className={ classes }>{ this.renderBar() }</div>;
}
}
|