ProgressBar.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { createElement as h, Component } from 'react'
  2. import type { UnknownPlugin, Uppy } from '@uppy/core'
  3. import ProgressBarPlugin, { type ProgressBarOptions } from '@uppy/progress-bar'
  4. import type { Body, Meta } from '@uppy/utils/lib/UppyFile'
  5. import getHTMLProps from './getHTMLProps.ts'
  6. import nonHtmlPropsHaveChanged from './nonHtmlPropsHaveChanged.ts'
  7. interface ProgressBarProps<M extends Meta, B extends Body>
  8. extends ProgressBarOptions {
  9. uppy: Uppy<M, B>
  10. }
  11. /**
  12. * React component that renders a progress bar at the top of the page.
  13. */
  14. class ProgressBar<M extends Meta, B extends Body> extends Component<
  15. ProgressBarProps<M, B>
  16. > {
  17. private container: HTMLElement
  18. private plugin: UnknownPlugin<M, B>
  19. componentDidMount(): void {
  20. this.installPlugin()
  21. }
  22. componentDidUpdate(prevProps: ProgressBar<M, B>['props']): void {
  23. // eslint-disable-next-line react/destructuring-assignment
  24. if (prevProps.uppy !== this.props.uppy) {
  25. this.uninstallPlugin(prevProps)
  26. this.installPlugin()
  27. } else if (nonHtmlPropsHaveChanged(this.props, prevProps)) {
  28. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  29. const { uppy, ...options } = { ...this.props, target: this.container }
  30. this.plugin.setOptions(options)
  31. }
  32. }
  33. componentWillUnmount(): void {
  34. this.uninstallPlugin()
  35. }
  36. installPlugin(): void {
  37. const { uppy, fixed, hideAfterFinish, id } = this.props
  38. const options = {
  39. id: id || 'ProgressBar',
  40. fixed,
  41. hideAfterFinish,
  42. target: this.container,
  43. }
  44. uppy.use(ProgressBarPlugin, options)
  45. this.plugin = uppy.getPlugin(options.id)!
  46. }
  47. uninstallPlugin(props = this.props): void {
  48. const { uppy } = props
  49. uppy.removePlugin(this.plugin)
  50. }
  51. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  52. render() {
  53. return h('div', {
  54. className: 'uppy-Container',
  55. ref: (container: HTMLElement) => {
  56. this.container = container
  57. },
  58. ...getHTMLProps(this.props),
  59. })
  60. }
  61. }
  62. export default ProgressBar