DragDrop.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { createElement as h, Component } from 'react'
  2. import PropTypes from 'prop-types'
  3. import DragDropPlugin from '@uppy/drag-drop'
  4. import * as propTypes from './propTypes.js'
  5. import getHTMLProps from './getHTMLProps.js'
  6. import nonHtmlPropsHaveChanged from './nonHtmlPropsHaveChanged.js'
  7. /**
  8. * React component that renders an area in which files can be dropped to be
  9. * uploaded.
  10. */
  11. class DragDrop extends Component {
  12. componentDidMount () {
  13. this.installPlugin()
  14. }
  15. componentDidUpdate (prevProps) {
  16. // eslint-disable-next-line react/destructuring-assignment
  17. if (prevProps.uppy !== this.props.uppy) {
  18. this.uninstallPlugin(prevProps)
  19. this.installPlugin()
  20. } else if (nonHtmlPropsHaveChanged(this.props, prevProps)) {
  21. const options = { ...this.props, target: this.container }
  22. delete options.uppy
  23. this.plugin.setOptions(options)
  24. }
  25. }
  26. componentWillUnmount () {
  27. this.uninstallPlugin()
  28. }
  29. installPlugin () {
  30. const {
  31. uppy,
  32. locale,
  33. inputName,
  34. width,
  35. height,
  36. note,
  37. } = this.props
  38. const options = {
  39. id: 'react:DragDrop',
  40. locale,
  41. inputName,
  42. width,
  43. height,
  44. note,
  45. target: this.container,
  46. }
  47. delete options.uppy
  48. uppy.use(DragDropPlugin, options)
  49. this.plugin = uppy.getPlugin(options.id)
  50. }
  51. uninstallPlugin (props = this.props) {
  52. const { uppy } = props
  53. uppy.removePlugin(this.plugin)
  54. }
  55. render () {
  56. return h('div', {
  57. className: 'uppy-Container',
  58. ref: (container) => {
  59. this.container = container
  60. },
  61. ...getHTMLProps(this.props),
  62. })
  63. }
  64. }
  65. DragDrop.propTypes = {
  66. uppy: propTypes.uppy.isRequired,
  67. locale: propTypes.locale,
  68. inputName: PropTypes.string,
  69. width: PropTypes.string,
  70. height: PropTypes.string,
  71. note: PropTypes.string,
  72. }
  73. // Must be kept in sync with @uppy/drag-drop/src/DragDrop.jsx.
  74. DragDrop.defaultProps = {
  75. locale: null,
  76. inputName: 'files[]',
  77. width: '100%',
  78. height: '100%',
  79. note: null,
  80. }
  81. export default DragDrop