DropZone 是一个 WordPress 组件,用于创建覆盖父元素全尺寸的拖放区域,支持文件、HTML 内容或其他 HTML 拖放事件的处理。
import { useState } from 'react';
import { DropZone } from '@wordpress/components';
const MyDropZone = () => {
const [ hasDropped, setHasDropped ] = useState( false );
return (
<div>
{ hasDropped ? 'Dropped!' : 'Drop something here' }
<DropZone
onFilesDrop={ () => setHasDropped( true ) }
onHTMLDrop={ () => setHasDropped( true ) }
onDrop={ () => setHasDropped( true ) }
/>
</div>
);
}DropZone is a component creating a drop zone area taking the full size of its parent element. It supports dropping files, HTML content or any other HTML drop event.
import { useState } from 'react';
import { DropZone } from '@wordpress/components';
const MyDropZone = () => {
const [ hasDropped, setHasDropped ] = useState( false );
return (
<div>
{ hasDropped ? 'Dropped!' : 'Drop something here' }
<DropZone
onFilesDrop={ () => setHasDropped( true ) }
onHTMLDrop={ () => setHasDropped( true ) }
onDrop={ () => setHasDropped( true ) }
/>
</div>
);
}
The component accepts the following props:
A CSS class to give to the wrapper element.
StringundefinedA string to be shown within the drop zone area.
StringDrop files to uploadThe function is called when dropping a file into the DropZone. It receives an array of dropped files as an argument.
FunctionnoopThe function is called when dropping HTML into the DropZone. It receives the HTML being dropped as an argument.
FunctionnoopThe function is generic drop handler called if the onFilesDrop or onHTMLDrop are not called. It receives the drop event object as an argument.
Functionnoop