uppy/packages/@uppy/react/src/DragDrop.ts
Murderlon 82a5999260
Merge branch 'main' into 4.x
* main: (22 commits)
  @uppy/xhr-upload: refactor to use `fetcher` (#5074)
  docs: use StackBlitz for all examples/issue template (#5125)
  Update yarn.lock
  Add svelte 5 as peer dep (#5122)
  Bump docker/setup-buildx-action from 2 to 3 (#5124)
  Bump actions/checkout from 3 to 4 (#5123)
  Remove JSX global type everywhere (#5117)
  Revert "@uppy/core: reference updated i18n in Restricter"
  @uppy/core: reference updated i18n in Restricter
  @uppy/utils: improve return type of `dataURItoFile` (#5112)
  @uppy/drop-target: change drop event type to DragEvent (#5107)
  @uppy/image-editor: fix label definitions (#5111)
  meta: bump Prettier version (#5114)
  @uppy/provider-views: bring back "loaded X files..." (#5097)
  @uppy/dashboard: fix type of trigger option (#5106)
  meta: fix linter
  @uppy/form: fix `submitOnSuccess` and `triggerUploadOnSubmit` combination (#5058)
  Bump docker/build-push-action from 3 to 5 (#5105)
  Bump akhileshns/heroku-deploy from 3.12.12 to 3.13.15 (#5102)
  Bump docker/login-action from 2 to 3 (#5101)
  ...
2024-04-29 15:56:33 +02:00

80 lines
2.1 KiB
TypeScript

import { createElement as h, Component } from 'react'
import type { UnknownPlugin, Uppy } from '@uppy/core'
import DragDropPlugin, { type DragDropOptions } from '@uppy/drag-drop'
import type { Body, Meta } from '@uppy/utils/lib/UppyFile'
import getHTMLProps from './getHTMLProps.ts'
import nonHtmlPropsHaveChanged from './nonHtmlPropsHaveChanged.ts'
interface DragDropProps<M extends Meta, B extends Body>
extends DragDropOptions {
uppy: Uppy<M, B>
}
/**
* React component that renders an area in which files can be dropped to be
* uploaded.
*/
class DragDrop<M extends Meta, B extends Body> extends Component<
DragDropProps<M, B>
> {
private container: HTMLElement
private plugin: UnknownPlugin<M, B>
componentDidMount(): void {
this.installPlugin()
}
componentDidUpdate(prevProps: DragDrop<M, B>['props']): void {
// eslint-disable-next-line react/destructuring-assignment
if (prevProps.uppy !== this.props.uppy) {
this.uninstallPlugin(prevProps)
this.installPlugin()
} else if (nonHtmlPropsHaveChanged(this.props, prevProps)) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { uppy, ...options } = { ...this.props, target: this.container }
this.plugin.setOptions(options)
}
}
componentWillUnmount(): void {
this.uninstallPlugin()
}
installPlugin(): void {
const { uppy, locale, inputName, width, height, note } = this.props
const options = {
id: 'react:DragDrop',
locale,
inputName,
width,
height,
note,
target: this.container,
}
uppy.use(DragDropPlugin, options)
this.plugin = uppy.getPlugin(options.id)!
}
uninstallPlugin(props = this.props): void {
const { uppy } = props
uppy.removePlugin(this.plugin)
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
render() {
return h('div', {
className: 'uppy-Container',
ref: (container: HTMLElement) => {
this.container = container
},
...getHTMLProps(this.props),
})
}
}
export default DragDrop