-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathProgress.js
More file actions
194 lines (156 loc) · 5.47 KB
/
Progress.js
File metadata and controls
194 lines (156 loc) · 5.47 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import cx from 'clsx'
import _ from 'lodash'
import PropTypes from 'prop-types'
import React, { Component } from 'react'
import {
childrenUtils,
createHTMLDivision,
customPropTypes,
getElementType,
getUnhandledProps,
SUI,
useKeyOnly,
useValueAndKey,
} from '../../lib'
/**
* A progress bar shows the progression of a task.
*/
class Progress extends Component {
calculatePercent = () => {
const { percent, total, value } = this.props
if (!_.isUndefined(percent)) return percent
if (!_.isUndefined(total) && !_.isUndefined(value)) return (value / total) * 100
if (!_.isUndefined(value)) return value
}
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 percent = _.clamp(this.calculatePercent(), 0, 100)
if (_.isUndefined(precision)) return percent
return _.round(percent, precision)
}
isAutoSuccess = () => {
const { autoSuccess, percent, total, value } = this.props
return autoSuccess && (percent >= 100 || value >= total)
}
renderLabel = () => {
const { children, content, label } = this.props
if (!childrenUtils.isNil(children)) return <div className='label'>{children}</div>
if (!childrenUtils.isNil(content)) return <div className='label'>{content}</div>
return createHTMLDivision(label, {
autoGenerateKey: false,
defaultProps: { className: 'label' },
})
}
renderProgress = (percent) => {
const { precision, progress } = this.props
if (!progress && _.isUndefined(precision)) return
return <div className='progress'>{this.computeValueText(percent)}</div>
}
render() {
const {
active,
attached,
className,
color,
disabled,
error,
indicating,
inverted,
size,
success,
warning,
} = this.props
const classes = cx(
'ui',
color,
size,
useKeyOnly(active || indicating, 'active'),
useKeyOnly(disabled, 'disabled'),
useKeyOnly(error, 'error'),
useKeyOnly(indicating, 'indicating'),
useKeyOnly(inverted, 'inverted'),
useKeyOnly(success || this.isAutoSuccess(), 'success'),
useKeyOnly(warning, 'warning'),
useValueAndKey(attached, 'attached'),
'progress',
className,
)
const rest = getUnhandledProps(Progress, this.props)
const ElementType = getElementType(Progress, this.props)
const percent = this.getPercent() || 0
return (
<ElementType {...rest} className={classes} data-percent={Math.floor(percent)}>
<div className='bar' style={{ width: `${percent}%` }}>
{this.renderProgress(percent)}
</div>
{this.renderLabel()}
</ElementType>
)
}
}
Progress.propTypes = {
/** An element type to render as (string or function). */
as: PropTypes.elementType,
/** A progress bar can show activity. */
active: PropTypes.bool,
/** A progress bar can attach to and show the progress of an element (i.e. Card or Segment). */
attached: PropTypes.oneOf(['top', 'bottom']),
/** Whether success state should automatically trigger when progress completes. */
autoSuccess: PropTypes.bool,
/** Primary content. */
children: PropTypes.node,
/** Additional classes. */
className: PropTypes.string,
/** A progress bar can have different colors. */
color: PropTypes.oneOf(SUI.COLORS),
/** Shorthand for primary content. */
content: customPropTypes.contentShorthand,
/** A progress bar be disabled. */
disabled: PropTypes.bool,
/** A progress bar can show a error state. */
error: PropTypes.bool,
/** An indicating progress bar visually indicates the current level of progress of a task. */
indicating: PropTypes.bool,
/** A progress bar can have its colors inverted. */
inverted: PropTypes.bool,
/** Can be set to either to display progress as percent or ratio. */
label: customPropTypes.itemShorthand,
/** Current percent complete. */
percent: customPropTypes.every([
customPropTypes.disallow(['total', 'value']),
PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
]),
/** Decimal point precision for calculated progress. */
precision: PropTypes.number,
/** A progress bar can contain a text value indicating current progress. */
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')),
/** A progress bar can show a success state. */
success: PropTypes.bool,
/** For use with value. Together, these will calculate the percent. Mutually excludes percent. */
total: customPropTypes.every([
customPropTypes.demand(['value']),
customPropTypes.disallow(['percent']),
PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
]),
/** For use with total. Together, these will calculate the percent. Mutually excludes percent. */
value: customPropTypes.every([
customPropTypes.disallow(['percent']),
PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
]),
/** A progress bar can show a warning state. */
warning: PropTypes.bool,
}
export default Progress