uppy/packages/@uppy/react/src/DashboardModal.js
Andrew 9a9860ac8f
Allowed HTML Attributes to be passed via props (#2891)
* Allowed HTML Attributes to be passed via props

This fix still needs some work, as certain attributes could be passed to Uppy or as an HTML attribute, such as target

When this is fixed, this will resolve #2403

* Removed shared attributes

I removed the ability to use certain attributes ('width', 'height', and 'target') and pass them down as Props to the div due to that fact that Uppy relies on them

* Add typescript types for the HTML attributes

* Moved common.js to getHTMLProps.js

* Fixed import

* Converted `possibleStandardNames` to an Array

* Fix import

* Fix tests
2021-06-14 13:11:37 +01:00

89 lines
2 KiB
JavaScript

const React = require('react')
const PropTypes = require('prop-types')
const DashboardPlugin = require('@uppy/dashboard')
const basePropTypes = require('./propTypes').dashboard
const getHTMLProps = require('./getHTMLProps')
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 {
constructor (props) {
super(props)
this.validProps = getHTMLProps(props)
}
componentDidMount () {
this.installPlugin()
}
componentDidUpdate (prevProps) {
if (prevProps.uppy !== this.props.uppy) {
this.uninstallPlugin(prevProps)
this.installPlugin()
}
if (prevProps.open && !this.props.open) {
this.plugin.closeModal()
} else if (!prevProps.open && this.props.open) {
this.plugin.openModal()
}
}
componentWillUnmount () {
this.uninstallPlugin()
}
installPlugin () {
const { uppy } = this.props
const options = {
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.removePlugin(this.plugin)
}
render () {
return h('div', {
ref: (container) => {
this.container = container
},
...this.validProps,
})
}
}
DashboardModal.propTypes = { // 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