Skip to content

Commit

Permalink
Fix primefaces#2690: FileUpload onBeforeDrop callback
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware committed Apr 29, 2022
1 parent 90f433c commit 040adf8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
11 changes: 11 additions & 0 deletions api-generator/components/fileupload.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,17 @@ const FileUploadEvents = [
}
]
},
{
name: 'onBeforeDrop',
description: 'Callback to invoke before files dropped. Return false from callback to prevent drop.',
arguments: [
{
name: 'event.originalEvent',
type: 'object',
description: 'Original browser event.'
},
]
},
{
name: 'onUpload',
description: 'Callback to invoke when file upload is complete.',
Expand Down
5 changes: 5 additions & 0 deletions components/doc/fileupload/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,11 @@ const buttonOptions = {
<td>Callback to invoke before file send begins to customize the request
such as adding headers.</td>
</tr>
<tr>
<td>onBeforeDrop</td>
<td>event: DragEvent instance.</td>
<td>Callback to invoke before files dropped. Return false from callback to prevent drop.</td>
</tr>
<tr>
<td>onUpload</td>
<td>event.xhr: XmlHttpRequest instance.<br />
Expand Down
1 change: 1 addition & 0 deletions components/lib/fileupload/FileUpload.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ interface FileUploadProps {
progressBarTemplate?: FileUploadProgressBarTemplateType;
onBeforeUpload?(e: FileUploadBeforeUploadParams): void;
onBeforeSend?(e: FileUploadBeforeSendParams): void;
onBeforeDrop?(e: DragEvent): void;
onUpload?(e: FileUploadUploadParams): void;
onError?(e: FileUploadErrorParams): void;
onClear?(): void;
Expand Down
22 changes: 15 additions & 7 deletions components/lib/fileupload/FileUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,16 +278,23 @@ export const FileUpload = React.memo(React.forwardRef((props, ref) => {
}

const onDrop = (event) => {
if (!props.disabled) {
DomHandler.removeClass(contentRef.current, 'p-fileupload-highlight');
event.stopPropagation();
event.preventDefault();
if (props.disabled) {
return;
}

const files = event.dataTransfer ? event.dataTransfer.files : event.target.files;
const allowDrop = props.multiple || (ObjectUtils.isEmpty(filesState) && files && files.length === 1);
DomHandler.removeClass(contentRef.current, 'p-fileupload-highlight');
event.stopPropagation();
event.preventDefault();

allowDrop && onFileSelect(event);
// give caller a chance to stop the drop
if (props.onBeforeDrop && props.onBeforeDrop(event) === false) {
return;
}

const files = event.dataTransfer ? event.dataTransfer.files : event.target.files;
const allowDrop = props.multiple || (ObjectUtils.isEmpty(filesState) && files && files.length === 1);

allowDrop && onFileSelect(event);
}

const onSimpleUploaderClick = () => {
Expand Down Expand Up @@ -527,6 +534,7 @@ FileUpload.defaultProps = {
progressBarTemplate: null,
onBeforeUpload: null,
onBeforeSend: null,
onBeforeDrop: null,
onUpload: null,
onError: null,
onClear: null,
Expand Down

0 comments on commit 040adf8

Please sign in to comment.