-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Wrapper.ts
59 lines (47 loc) · 1.48 KB
/
Wrapper.ts
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
import { createElement as h, Component } from 'react'
import type { Body, Meta } from '@uppy/utils/lib/UppyFile'
import type { UIPlugin, Uppy } from '@uppy/core'
import PropTypes from 'prop-types'
import { uppy as uppyPropType } from './propTypes.ts'
interface UppyWrapperProps<M extends Meta, B extends Body> {
uppy: Uppy<M, B>
plugin: string
}
class UppyWrapper<M extends Meta, B extends Body> extends Component<
UppyWrapperProps<M, B>
> {
static propTypes = {
uppy: uppyPropType.isRequired,
plugin: PropTypes.string.isRequired,
}
private container: HTMLDivElement
componentDidMount(): void {
this.installPlugin()
}
componentDidUpdate(prevProps: UppyWrapperProps<M, B>): void {
const { uppy } = this.props
if (prevProps.uppy !== uppy) {
this.uninstallPlugin(prevProps)
this.installPlugin()
}
}
componentWillUnmount(): void {
this.uninstallPlugin()
}
private refContainer = (container: UppyWrapper<M, B>['container']) => {
this.container = container
}
installPlugin(): void {
const { plugin, uppy } = this.props
const pluginObj = uppy.getPlugin(plugin) as UIPlugin<any, M, B>
pluginObj.mount(this.container, pluginObj)
}
uninstallPlugin({ uppy } = this.props): void {
const { plugin } = this.props
;(uppy.getPlugin(plugin) as UIPlugin<any, M, B>).unmount()
}
render(): ReturnType<typeof h> {
return h('div', { className: 'uppy-Container', ref: this.refContainer })
}
}
export default UppyWrapper