From fe641a7de80c00780e434d094d070c09d4343701 Mon Sep 17 00:00:00 2001 From: Melloware Date: Fri, 5 Aug 2022 08:10:58 -0400 Subject: [PATCH] Fix #3134: FileUpload onBeforeSelect event (#3135) --- api-generator/components/fileupload.js | 16 +++++++++++++++ components/doc/fileupload/index.js | 24 ++++++++++++++--------- components/lib/fileupload/FileUpload.d.ts | 1 + components/lib/fileupload/FileUpload.js | 6 ++++++ 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/api-generator/components/fileupload.js b/api-generator/components/fileupload.js index 089cbb7250..c24edca263 100644 --- a/api-generator/components/fileupload.js +++ b/api-generator/components/fileupload.js @@ -225,6 +225,22 @@ const FileUploadEvents = [ }, ] }, + { + name: 'onBeforeSelect', + description: 'Callback to invoke before files are selected. Return false from callback to prevent selection.', + arguments: [ + { + name: 'event.originalEvent', + type: 'object', + description: 'Original browser event.' + }, + { + name: 'event.target.files', + type: 'any', + description: 'List of selected files.' + } + ] + }, { name: 'onUpload', description: 'Callback to invoke when file upload is complete.', diff --git a/components/doc/fileupload/index.js b/components/doc/fileupload/index.js index 9f38665ca5..f49dab5669 100644 --- a/components/doc/fileupload/index.js +++ b/components/doc/fileupload/index.js @@ -126,11 +126,11 @@ export class FileUploadDemo extends Component { } async customBase64Uploader(event) { - // convert file to base64 encoded + // convert file to base64 encoded const file = event.files[0]; const reader = new FileReader(); let blob = await fetch(file.objectURL).then(r => r.blob()); //blob:url - reader.readAsDataURL(blob); + reader.readAsDataURL(blob); reader.onloadend = function () { const base64data = reader.result; console.log(base64data); @@ -273,11 +273,11 @@ export const FileUploadDemo = () => { } const customBase64Uploader = async (event) => { - // convert file to base64 encoded + // convert file to base64 encoded const file = event.files[0]; const reader = new FileReader(); let blob = await fetch(file.objectURL).then(r => r.blob()); //blob:url - reader.readAsDataURL(blob); + reader.readAsDataURL(blob); reader.onloadend = function () { const base64data = reader.result; console.log(base64data); @@ -418,11 +418,11 @@ export const FileUploadDemo = () => { } const customBase64Uploader = async (event) => { - // convert file to base64 encoded + // convert file to base64 encoded const file = event.files[0]; const reader = new FileReader(); let blob = await fetch(file.objectURL).then(r => r.blob()); //blob:url - reader.readAsDataURL(blob); + reader.readAsDataURL(blob); reader.onloadend = function () { const base64data = reader.result; console.log(base64data); @@ -568,11 +568,11 @@ const FileUploadDemo = () => { } const customBase64Uploader = async (event) => { - // convert file to base64 encoded + // convert file to base64 encoded const file = event.files[0]; const reader = new FileReader(); let blob = await fetch(file.objectURL).then(r => r.blob()); //blob:url - reader.readAsDataURL(blob); + reader.readAsDataURL(blob); reader.onloadend = function () { const base64data = reader.result; console.log(base64data); @@ -1007,6 +1007,12 @@ const buttonOptions = { event: DragEvent instance. Callback to invoke before files dropped. Return false from callback to prevent drop. + + onBeforeSelect + event.originalEvent: Original browser event.
+ event.target.files: List of selected files. + Callback to invoke before files are selected. Return false from callback to prevent selection. + onUpload event.xhr: XmlHttpRequest instance.
@@ -1027,7 +1033,7 @@ const buttonOptions = { onSelect event.originalEvent: Original browser event.
- event.files: List of selected files. + event.target.files: List of selected files. Callback to invoke when files are selected. diff --git a/components/lib/fileupload/FileUpload.d.ts b/components/lib/fileupload/FileUpload.d.ts index a4f2a67995..2b1ba265fd 100644 --- a/components/lib/fileupload/FileUpload.d.ts +++ b/components/lib/fileupload/FileUpload.d.ts @@ -115,6 +115,7 @@ interface FileUploadProps { onBeforeUpload?(e: FileUploadBeforeUploadParams): void; onBeforeSend?(e: FileUploadBeforeSendParams): void; onBeforeDrop?(e: DragEvent): void; + onBeforeSelect?(e: FileUploadSelectParams): void; onUpload?(e: FileUploadUploadParams): void; onError?(e: FileUploadErrorParams): void; onClear?(): void; diff --git a/components/lib/fileupload/FileUpload.js b/components/lib/fileupload/FileUpload.js index af8f3ca9d4..29b4cf1eb6 100644 --- a/components/lib/fileupload/FileUpload.js +++ b/components/lib/fileupload/FileUpload.js @@ -71,6 +71,11 @@ export const FileUpload = React.memo(React.forwardRef((props, ref) => { } const onFileSelect = (event) => { + // give caller a chance to stop the selection + if (props.onBeforeSelect && props.onBeforeSelect(event) === false) { + return; + } + if (event.type !== 'drop' && isIE11() && duplicateIEEvent.current) { duplicateIEEvent.current = false; return; @@ -549,6 +554,7 @@ FileUpload.defaultProps = { onBeforeUpload: null, onBeforeSend: null, onBeforeDrop: null, + onBeforeSelect: null, onUpload: null, onError: null, onClear: null,