uppy/packages/@uppy/react/src/FileInput.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

81 lines
1.9 KiB
TypeScript

import { createElement as h, Component } from 'react'
import type { UnknownPlugin, Uppy } from '@uppy/core'
import FileInputPlugin from '@uppy/file-input'
import type { Body, Meta } from '@uppy/utils/lib/UppyFile'
import type { Locale } from '@uppy/utils/lib/Translator'
interface FileInputProps<M extends Meta, B extends Body> {
uppy: Uppy<M, B>
locale?: Locale
pretty?: boolean
inputName?: string
}
/**
* React component that renders an area in which files can be dropped to be
* uploaded.
*/
class FileInput<M extends Meta, B extends Body> extends Component<
FileInputProps<M, B>
> {
// Must be kept in sync with @uppy/file-input/src/FileInput.jsx
static defaultProps = {
locale: undefined,
pretty: true,
inputName: 'files[]',
}
private container: HTMLElement
private plugin: UnknownPlugin<M, B>
componentDidMount(): void {
this.installPlugin()
}
componentDidUpdate(prevProps: FileInputProps<M, B>): void {
// eslint-disable-next-line react/destructuring-assignment
if (prevProps.uppy !== this.props.uppy) {
this.uninstallPlugin(prevProps)
this.installPlugin()
}
}
componentWillUnmount(): void {
this.uninstallPlugin()
}
installPlugin(): void {
const { uppy, locale, pretty, inputName } = this.props
const options = {
id: 'react:FileInput',
locale,
pretty,
inputName,
target: this.container,
}
uppy.use(FileInputPlugin, 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
},
})
}
}
export default FileInput