uppy/packages/@uppy/react/src/StatusBar.ts
Merlijn Vos 6e65cd8c51
Import types consistently from @uppy/core (#5589)
* Import types consistently from @uppy/core

* Add new eslint rule

* Clean up exports
2025-01-09 11:03:43 +01:00

91 lines
2.3 KiB
TypeScript

import { createElement as h, Component } from 'react'
import type { UnknownPlugin, Uppy, Body, Meta } from '@uppy/core'
import StatusBarPlugin, { type StatusBarOptions } from '@uppy/status-bar'
import getHTMLProps from './getHTMLProps.js'
import nonHtmlPropsHaveChanged from './nonHtmlPropsHaveChanged.js'
interface StatusBarProps<M extends Meta, B extends Body>
extends StatusBarOptions {
uppy: Uppy<M, B>
}
/**
* React component that renders a status bar containing upload progress and speed,
* processing progress and pause/resume/cancel controls.
*/
class StatusBar<M extends Meta, B extends Body> extends Component<
StatusBarProps<M, B>
> {
private container!: HTMLElement
private plugin!: UnknownPlugin<M, B>
componentDidMount(): void {
this.installPlugin()
}
componentDidUpdate(prevProps: StatusBar<M, B>['props']): void {
// eslint-disable-next-line react/destructuring-assignment
if (prevProps.uppy !== this.props.uppy) {
this.uninstallPlugin(prevProps)
this.installPlugin()
} else if (nonHtmlPropsHaveChanged(this.props, prevProps)) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { uppy, ...options } = { ...this.props, target: this.container }
this.plugin.setOptions(options)
}
}
componentWillUnmount(): void {
this.uninstallPlugin()
}
installPlugin(): void {
const {
uppy,
hideUploadButton,
hideRetryButton,
hidePauseResumeButton,
hideCancelButton,
showProgressDetails,
hideAfterFinish,
doneButtonHandler,
id,
} = this.props
const options = {
id: id || 'StatusBar',
hideUploadButton,
hideRetryButton,
hidePauseResumeButton,
hideCancelButton,
showProgressDetails,
hideAfterFinish,
doneButtonHandler,
target: this.container,
}
uppy.use(StatusBarPlugin, options)
this.plugin = uppy.getPlugin(options.id)!
}
uninstallPlugin(props = this.props): void {
const { uppy } = props
uppy.removePlugin(this.plugin)
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
render() {
return h('div', {
className: 'uppy-Container',
ref: (container: HTMLElement) => {
this.container = container
},
...getHTMLProps(this.props),
})
}
}
export default StatusBar