UNPKG

1.98 kBJavaScriptView Raw
1const React = require('react')
2const PropTypes = require('prop-types')
3const DashboardPlugin = require('@uppy/dashboard')
4const basePropTypes = require('./propTypes').dashboard
5
6const h = React.createElement
7
8/**
9 * React Component that renders a Dashboard for an Uppy instance in a Modal
10 * dialog. Visibility of the Modal is toggled using the `open` prop.
11 */
12
13class DashboardModal extends React.Component {
14 componentDidMount () {
15 this.installPlugin()
16 }
17
18 componentDidUpdate (prevProps) {
19 if (prevProps.uppy !== this.props.uppy) {
20 this.uninstallPlugin(prevProps)
21 this.installPlugin()
22 }
23 if (prevProps.open && !this.props.open) {
24 this.plugin.closeModal()
25 } else if (!prevProps.open && this.props.open) {
26 this.plugin.openModal()
27 }
28 }
29
30 componentWillUnmount () {
31 this.uninstallPlugin()
32 }
33
34 installPlugin () {
35 const uppy = this.props.uppy
36 const options = Object.assign(
37 { id: 'react:DashboardModal' },
38 this.props,
39 {
40 onRequestCloseModal: this.props.onRequestClose
41 }
42 )
43
44 if (!options.target) {
45 options.target = this.container
46 }
47
48 delete options.uppy
49 uppy.use(DashboardPlugin, options)
50
51 this.plugin = uppy.getPlugin(options.id)
52 if (this.props.open) {
53 this.plugin.openModal()
54 }
55 }
56
57 uninstallPlugin (props = this.props) {
58 const uppy = props.uppy
59
60 uppy.removePlugin(this.plugin)
61 }
62
63 render () {
64 return h('div', {
65 ref: (container) => {
66 this.container = container
67 }
68 })
69 }
70}
71
72DashboardModal.propTypes = Object.assign({
73 // Only check this prop type in the browser.
74 target: typeof window !== 'undefined' ? PropTypes.instanceOf(window.HTMLElement) : PropTypes.any,
75 open: PropTypes.bool,
76 onRequestClose: PropTypes.func,
77 closeModalOnClickOutside: PropTypes.bool,
78 disablePageScrollWhenModalOpen: PropTypes.bool
79}, basePropTypes)
80
81DashboardModal.defaultProps = {
82}
83
84module.exports = DashboardModal