1 | const React = require('react')
|
2 | const PropTypes = require('prop-types')
|
3 | const DashboardPlugin = require('@uppy/dashboard')
|
4 | const basePropTypes = require('./propTypes').dashboard
|
5 |
|
6 | const h = React.createElement
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | class 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 |
|
72 | DashboardModal.propTypes = Object.assign({
|
73 |
|
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 |
|
81 | DashboardModal.defaultProps = {
|
82 | }
|
83 |
|
84 | module.exports = DashboardModal
|