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(Progress): clamp bar width and improve propTypes check for progress prop #4332

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 8 additions & 6 deletions src/modules/Progress/Progress.js
Expand Up @@ -23,6 +23,7 @@ class Progress extends Component {

if (!_.isUndefined(percent)) return percent
if (!_.isUndefined(total) && !_.isUndefined(value)) return (value / total) * 100
if (!_.isUndefined(value)) return value
}

computeValueText = (percent) => {
Expand All @@ -34,13 +35,9 @@ class Progress extends Component {
}

getPercent = () => {
const { precision, progress, total, value } = this.props
const { precision } = this.props
const percent = _.clamp(this.calculatePercent(), 0, 100)

if (!_.isUndefined(total) && !_.isUndefined(value) && progress === 'value') {
return (value / total) * 100
}
if (progress === 'value') return value
if (_.isUndefined(precision)) return percent
return _.round(percent, precision)
}
Expand Down Expand Up @@ -164,7 +161,12 @@ Progress.propTypes = {
precision: PropTypes.number,

/** A progress bar can contain a text value indicating current progress. */
progress: PropTypes.oneOfType([PropTypes.bool, PropTypes.oneOf(['percent', 'ratio', 'value'])]),
progress: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.oneOf(['percent']),
customPropTypes.every([PropTypes.oneOf(['value']), customPropTypes.demand(['value'])]),
customPropTypes.every([PropTypes.oneOf(['ratio']), customPropTypes.demand(['value', 'total'])]),
]),

/** A progress bar can vary in size. */
size: PropTypes.oneOf(_.without(SUI.SIZES, 'mini', 'huge', 'massive')),
Expand Down
9 changes: 9 additions & 0 deletions test/specs/modules/Progress/Progress-test.js
Expand Up @@ -85,6 +85,15 @@ describe('Progress', () => {
.find('.bar')
.should.have.style('width', '50%')
})
it('cannot have its width set >100%, when progress="value"', () => {
shallow(<Progress progress='value' value={10} total={5} />)
.find('.bar')
.should.have.style('width', '100%')

shallow(<Progress progress='value' value={200} />)
.find('.bar')
.should.have.style('width', '100%')
})
})

describe('data-percent', () => {
Expand Down