uppy/packages/@uppy/react/src/DashboardModal.js
2019-01-21 15:12:30 +01:00

87 lines
2 KiB
JavaScript

const React = require('react')
const PropTypes = require('prop-types')
const DashboardPlugin = require('@uppy/dashboard')
const basePropTypes = require('./propTypes').dashboard
const h = React.createElement
/**
* React Component that renders a Dashboard for an Uppy instance in a Modal
* dialog. Visibility of the Modal is toggled using the `open` prop.
*/
class DashboardModal extends React.Component {
componentDidMount () {
this.installPlugin()
}
componentDidUpdate (prevProps) {
if (prevProps.uppy !== this.props.uppy) {
this.uninstallPlugin(prevProps)
this.installPlugin()
}
}
componentWillUnmount () {
this.uninstallPlugin()
}
installPlugin () {
const uppy = this.props.uppy
const options = Object.assign(
{ id: 'react:DashboardModal' },
this.props,
{
onRequestCloseModal: this.props.onRequestClose
}
)
if (!options.target) {
options.target = this.container
}
delete options.uppy
uppy.use(DashboardPlugin, options)
this.plugin = uppy.getPlugin(options.id)
if (this.props.open) {
this.plugin.openModal()
}
}
uninstallPlugin (props = this.props) {
const uppy = props.uppy
uppy.removePlugin(this.plugin)
}
componentWillReceiveProps (nextProps) {
if (this.props.open && !nextProps.open) {
this.plugin.closeModal()
} else if (!this.props.open && nextProps.open) {
this.plugin.openModal()
}
}
render () {
return h('div', {
ref: (container) => {
this.container = container
}
})
}
}
DashboardModal.propTypes = Object.assign({
// Only check this prop type in the browser.
target: typeof window !== 'undefined' ? PropTypes.instanceOf(window.HTMLElement) : PropTypes.any,
open: PropTypes.bool,
onRequestClose: PropTypes.func,
closeModalOnClickOutside: PropTypes.bool,
disablePageScrollWhenModalOpen: PropTypes.bool
}, basePropTypes)
DashboardModal.defaultProps = {
}
module.exports = DashboardModal