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

62 lines
1.2 KiB
JavaScript

const React = require('react')
const DashboardPlugin = require('@uppy/dashboard')
const basePropTypes = require('./propTypes').dashboard
const h = React.createElement
/**
* React Component that renders a Dashboard for an Uppy instance. This component
* renders the Dashboard inline, so you can put it anywhere you want.
*/
class Dashboard 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:Dashboard' },
this.props,
{ target: this.container }
)
delete options.uppy
uppy.use(DashboardPlugin, options)
this.plugin = uppy.getPlugin(options.id)
}
uninstallPlugin (props = this.props) {
const uppy = props.uppy
uppy.removePlugin(this.plugin)
}
render () {
return h('div', {
ref: (container) => {
this.container = container
}
})
}
}
Dashboard.propTypes = basePropTypes
Dashboard.defaultProps = {
inline: true
}
module.exports = Dashboard