Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(blade): File upload does not fire change input event when file is dropped [DSSUP-128] #2402

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sixty-rice-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@razorpay/blade': patch
---

fix(blade): File upload does not fire change , input event when file is dropped
18 changes: 17 additions & 1 deletion packages/blade/src/components/FileUpload/FileUpload.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const _FileUpload: React.ForwardRefRenderFunction<BladeElementRef, FileUploadPro
onReupload,
onDismiss,
onDrop,
onInput,
isDisabled,
isRequired,
necessityIndicator,
Expand Down Expand Up @@ -146,6 +147,17 @@ const _FileUpload: React.ForwardRefRenderFunction<BladeElementRef, FileUploadPro
setIsActive(false);
};

const handleFileChangeEvents = ({
fileList,
name,
}: {
fileList: BladeFileList;
name?: string;
}): void => {
onChange?.({ name, fileList });
onInput?.({ name, fileList });
};

const handleDrop = (event: React.DragEvent): void => {
event.preventDefault();
setIsActive(false);
Expand All @@ -158,6 +170,7 @@ const _FileUpload: React.ForwardRefRenderFunction<BladeElementRef, FileUploadPro
if (!hasValidationErrors) {
handleFilesChange(droppedFiles);
onDrop?.({ name, fileList: allFiles });
handleFileChangeEvents({ name, fileList: allFiles });
}
};

Expand All @@ -169,7 +182,7 @@ const _FileUpload: React.ForwardRefRenderFunction<BladeElementRef, FileUploadPro

if (!hasValidationErrors) {
handleFilesChange(inputFiles);
onChange?.({ name, fileList: allFiles });
handleFileChangeEvents({ name, fileList: allFiles });
}

// Reset the input value to allow re-selecting the same file
Expand Down Expand Up @@ -422,6 +435,9 @@ const _FileUpload: React.ForwardRefRenderFunction<BladeElementRef, FileUploadPro
onChange={({ fileList }) => {
setSelectedFile(fileList[0]);
}}
onInput={({ fileList }) => {
console.log(fileList);
}}
onDrop={({ fileList }) => {
setSelectedFile(fileList[0]);
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,14 @@ const CustomPreviewTemplate: StoryFn<typeof FileUploadComponent> = (args) => {
{...args}
fileList={uploadedFiles}
onChange={({ fileList }) => handleFileChange({ fileList })}
onDrop={({ fileList }) => handleFileChange({ fileList })}
onDrop={({ fileList }) => console.log('onDrop', fileList)}
onPreview={({ file }) => {
setIsOpen(true);
setImageFileSource(URL.createObjectURL(file));
}}
onInput={({ fileList }) => {
console.log('onInput', fileList);
}}
/>
<Button
type="submit"
Expand Down
12 changes: 6 additions & 6 deletions packages/blade/src/components/FileUpload/docs/stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function App(): React.ReactElement {
setSelectedFile(fileList[0]);
}}
onDrop={({ fileList }: FileUploadProps['onDrop']) => {
setSelectedFile(fileList[0]);
console.log('onDrop', fileList);
}}
isRequired
necessityIndicator="required"
Expand Down Expand Up @@ -253,7 +253,7 @@ const MultiFileUploadStory = `
setSelectedFiles(fileList);
}}
onDrop={({ fileList }: FileUploadProps['onDrop']) => {
setSelectedFiles(fileList);
console.log("onDrop", fileList);
}}
isRequired
necessityIndicator="required"
Expand Down Expand Up @@ -400,7 +400,7 @@ import {
accept=".jpg, .jpeg, .png"
fileList={uploadedFiles}
onChange={({ fileList }: FileUploadProps['onChange']) => handleFileChange({ fileList })}
onDrop={({ fileList }: FileUploadProps['onDrop']) => handleFileChange({ fileList })}
onDrop={({ fileList }: FileUploadProps['onDrop']) => {console.log("onDrop",fileList)})}
isRequired
necessityIndicator="required"
/>
Expand Down Expand Up @@ -579,7 +579,6 @@ const AutoFileUploadWithProgressStory = `
accept=".jpg, .jpeg, .png"
fileList={uploadedFiles}
onChange={handleFileChange}
onDrop={handleFileChange}
isRequired
necessityIndicator="required"
/>
Expand Down Expand Up @@ -772,8 +771,9 @@ import {
onChange={({ fileList }: { fileList: BladeFileList }) =>
handleFileChange({ fileList })
}
onDrop={({ fileList }: { fileList: BladeFileList }) =>
handleFileChange({ fileList })
onDrop={({ fileList }: { fileList: BladeFileList }) => {
console.log("onDrop", fileList);
}
}
onPreview={({ file }: { file: BladeFile }) => {
setIsOpen(true);
Expand Down
6 changes: 5 additions & 1 deletion packages/blade/src/components/FileUpload/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ type FileUploadCommonProps = {
*/
maxSize?: number;
/**
* Callback function triggered when files are selected
* Callback function triggered when files are selected or when a file is dropped into the upload area
*/
onChange?: ({ name, fileList }: { name?: string; fileList: BladeFileList }) => void;
/**
* Callback function triggered when files are selected or when a file is dropped into the upload area
*/
onInput?: ({ name, fileList }: { name?: string; fileList: BladeFileList }) => void;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems same as onChange , are we planning to deprecate the onChange prop in favor of onInput? If so, we need to update the description accordingly.

Copy link
Author

@tewarig tewarig Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case of FileUpload, both onChange and onInput should exhibit the same behavior.
CodePen example demonstrates this.

Currently, however, the FileUpload component lacks an onInput prop, so it does not trigger the onInput event when a file is dropped. we need to ensure that native input events are triggered in FileUpload for Analytics SDK .
https://razorpay.atlassian.net/browse/DSSUP-128

/**
* Callback function triggered when the preview icon is clicked
*/
Expand Down
Loading