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

feat(Progress): Add value option, to just show the value in the bar #2383

Merged
merged 6 commits into from
Jan 10, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from 'react'
import { Progress } from 'semantic-ui-react'

const ProgressExampleProgressValue = () => (
<Progress progress='value' value={35} />
)

export default ProgressExampleProgressValue
4 changes: 4 additions & 0 deletions docs/app/Examples/modules/Progress/Content/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ const ProgressContentExamples = () => (
description='A progress element display progress as a ratio.'
examplePath='modules/Progress/Content/ProgressExampleProgressRatio'
/>
<ComponentExample
description='A progress element display progress as a value.'
examplePath='modules/Progress/Content/ProgressExampleProgressValue'
/>
</ExampleSection>
)

Expand Down
2 changes: 1 addition & 1 deletion src/modules/Progress/Progress.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export interface ProgressProps {
precision?: number;

/** A progress bar can contain a text value indicating current progress. */
progress?: boolean | 'percent' | 'ratio';
progress?: boolean | 'percent' | 'ratio' | 'value';

/** A progress bar can vary in size. */
size?: 'tiny' | 'small' | 'medium' | 'large' | 'big';
Expand Down
23 changes: 13 additions & 10 deletions src/modules/Progress/Progress.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class Progress extends Component {
/** A progress bar can contain a text value indicating current progress. */
progress: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.oneOf(['percent', 'ratio']),
PropTypes.oneOf(['percent', 'ratio', 'value']),
]),

/** A progress bar can vary in size. */
Expand All @@ -95,7 +95,6 @@ class Progress extends Component {

/** For use with total. Together, these will calculate the percent. Mutually excludes percent. */
value: customPropTypes.every([
customPropTypes.demand(['total']),
customPropTypes.disallow(['percent']),
PropTypes.oneOfType([
PropTypes.number,
Expand All @@ -119,10 +118,19 @@ class Progress extends Component {
if (!_.isUndefined(total) && !_.isUndefined(value)) return (value / total) * 100
}

computeValueText = (percent) => {
const { progress, total, value } = this.props

if (progress === 'value') return value
if (progress === 'ratio') return `${value}/${total}`
return `${percent}%`
}

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

if (progress === 'value') return value
if (_.isUndefined(precision)) return percent
return _.round(percent, precision)
}
Expand All @@ -142,17 +150,12 @@ class Progress extends Component {
}

renderProgress = (percent) => {
const {
precision,
progress,
total,
value,
} = this.props
const { precision, progress } = this.props

if (!progress && _.isUndefined(precision)) return
return (
<div className='progress'>
{ progress !== 'ratio' ? `${percent}%` : `${value}/${total}` }
{this.computeValueText(percent)}
</div>
)
}
Expand Down
6 changes: 6 additions & 0 deletions test/specs/modules/Progress/Progress-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ describe('Progress', () => {
.find('.progress')
.should.contain.text('50%')
})
it('displays the progress as text when set to "value"', () => {
shallow(<Progress progress='value' value={1} total={2} />)
.children()
.find('.progress')
.should.contain.text('1')
})
it('shows the percent complete', () => {
shallow(<Progress percent={72} progress />)
.children()
Expand Down