ProgressBar.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { createElement as h, Component } from 'react'
  2. import PropTypes from 'prop-types'
  3. import ProgressBarPlugin from '@uppy/progress-bar'
  4. import { uppy as uppyPropType } from './propTypes.js'
  5. import getHTMLProps from './getHTMLProps.js'
  6. import nonHtmlPropsHaveChanged from './nonHtmlPropsHaveChanged.js'
  7. /**
  8. * React component that renders a progress bar at the top of the page.
  9. */
  10. class ProgressBar extends Component {
  11. componentDidMount () {
  12. this.installPlugin()
  13. }
  14. componentDidUpdate (prevProps) {
  15. // eslint-disable-next-line react/destructuring-assignment
  16. if (prevProps.uppy !== this.props.uppy) {
  17. this.uninstallPlugin(prevProps)
  18. this.installPlugin()
  19. } else if (nonHtmlPropsHaveChanged(this.props, prevProps)) {
  20. const options = { ...this.props, target: this.container }
  21. delete options.uppy
  22. this.plugin.setOptions(options)
  23. }
  24. }
  25. componentWillUnmount () {
  26. this.uninstallPlugin()
  27. }
  28. installPlugin () {
  29. const { uppy, fixed, hideAfterFinish } = this.props
  30. const options = {
  31. id: 'react:ProgressBar',
  32. fixed,
  33. hideAfterFinish,
  34. target: this.container,
  35. }
  36. delete options.uppy
  37. uppy.use(ProgressBarPlugin, options)
  38. this.plugin = uppy.getPlugin(options.id)
  39. }
  40. uninstallPlugin (props = this.props) {
  41. const { uppy } = props
  42. uppy.removePlugin(this.plugin)
  43. }
  44. render () {
  45. return h('div', {
  46. className: 'uppy-Container',
  47. ref: (container) => {
  48. this.container = container
  49. },
  50. ...getHTMLProps(this.props),
  51. })
  52. }
  53. }
  54. ProgressBar.propTypes = {
  55. uppy: uppyPropType.isRequired,
  56. fixed: PropTypes.bool,
  57. hideAfterFinish: PropTypes.bool,
  58. }
  59. // Must be kept in sync with @uppy/progress-bar/src/ProgressBar.jsx
  60. ProgressBar.defaultProps = {
  61. fixed: false,
  62. hideAfterFinish: true,
  63. }
  64. export default ProgressBar