Skip to content

Commit

Permalink
Fix #3134: FileUpload onBeforeSelect event (#3135)
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware authored Aug 5, 2022
1 parent fce79f8 commit fe641a7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
16 changes: 16 additions & 0 deletions api-generator/components/fileupload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down
24 changes: 15 additions & 9 deletions components/doc/fileupload/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1007,6 +1007,12 @@ const buttonOptions = {
<td>event: DragEvent instance.</td>
<td>Callback to invoke before files dropped. Return false from callback to prevent drop.</td>
</tr>
<tr>
<td>onBeforeSelect</td>
<td>event.originalEvent: Original browser event. <br />
event.target.files: List of selected files.</td>
<td>Callback to invoke before files are selected. Return false from callback to prevent selection.</td>
</tr>
<tr>
<td>onUpload</td>
<td>event.xhr: XmlHttpRequest instance.<br />
Expand All @@ -1027,7 +1033,7 @@ const buttonOptions = {
<tr>
<td>onSelect</td>
<td>event.originalEvent: Original browser event. <br />
event.files: List of selected files.</td>
event.target.files: List of selected files.</td>
<td>Callback to invoke when files are selected.</td>
</tr>
<tr>
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 @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions components/lib/fileupload/FileUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -549,6 +554,7 @@ FileUpload.defaultProps = {
onBeforeUpload: null,
onBeforeSend: null,
onBeforeDrop: null,
onBeforeSelect: null,
onUpload: null,
onError: null,
onClear: null,
Expand Down

0 comments on commit fe641a7

Please sign in to comment.