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(FileUpload): revert to not calling onChange on file remove #2521

Merged
merged 2 commits into from
Feb 5, 2025
Merged
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
18 changes: 18 additions & 0 deletions .changeset/moody-coats-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
"@razorpay/blade": patch
---

fix(FileUpload): revert to not calling onChange on file remove

> [!NOTE]
>
> Check the below timeline if you're upgrading from 11.34.1+ version to this version

**Timeline of FileUpload changes**

- In 11.34.1: We did not call onChange on removing of file. Only onRemove was called
- In 11:36.2: We added dispatchEvent call which started calling onChange on onRemove (since React treats `input type="file"` differently than `input type="text"` - [CodeSandbox Link](https://codesandbox.io/p/sandbox/friendly-ishizaka-yk7mm3))
- In 12.4.0: We released a fix thinking onChange call was expected behaviour and we just updated the state value for it
- **This version:** Reverts back to 11.34.1 behaviour. If you're upgrading to this version from 11.34.1 or previous versions, the behaviour will stay same. If you're upgrading from 11.34.1+ and use FileUpload component, its recommended to test out FileUpload instances.


15 changes: 3 additions & 12 deletions packages/blade/src/components/FileUpload/FileUpload.web.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useState, useCallback, useMemo, useRef, forwardRef } from 'react';
import { flushSync } from 'react-dom';
import type { FileUploadProps, BladeFile, BladeFileList } from './types';
import { StyledFileUploadWrapper } from './StyledFileUploadWrapper';
import {
Expand Down Expand Up @@ -29,7 +28,6 @@ import { useMergeRefs } from '~utils/useMergeRefs';
import { useControllableState } from '~utils/useControllable';
import { getInnerMotionRef, getOuterMotionRef } from '~utils/getMotionRefs';
import { makeAnalyticsAttribute } from '~utils/makeAnalyticsAttribute';
import { fireNativeEvent } from '~utils/fireNativeEvent';

const _FileUpload: React.ForwardRefRenderFunction<BladeElementRef, FileUploadProps> = (
{
Expand Down Expand Up @@ -163,7 +161,6 @@ const _FileUpload: React.ForwardRefRenderFunction<BladeElementRef, FileUploadPro
if (!hasValidationErrors) {
handleFilesChange(droppedFiles);
onDrop?.({ name, fileList: allFiles });
fireNativeEvent(inputRef, ['change', 'input']);
}
};

Expand Down Expand Up @@ -313,11 +310,9 @@ const _FileUpload: React.ForwardRefRenderFunction<BladeElementRef, FileUploadPro
size={size}
onRemove={() => {
const newFiles = selectedFiles.filter(({ id }) => id !== selectedFiles[0].id);
flushSync(() => {
setSelectedFiles(() => newFiles);
});
setSelectedFiles(() => newFiles);

onRemove?.({ file: selectedFiles[0] });
fireNativeEvent(inputRef, ['change', 'input']);
}}
onReupload={() => {
const newFiles = selectedFiles.filter(({ id }) => id !== selectedFiles[0].id);
Expand Down Expand Up @@ -377,11 +372,8 @@ const _FileUpload: React.ForwardRefRenderFunction<BladeElementRef, FileUploadPro
size={size}
onRemove={() => {
const newFiles = selectedFiles.filter(({ id }) => id !== file.id);
flushSync(() => {
Copy link
Member

Choose a reason for hiding this comment

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

We are also removing flushSync?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup we added it to fix the onChange args right? now we are not calling onChange on remove

setSelectedFiles(() => newFiles);
});
setSelectedFiles(() => newFiles);
onRemove?.({ file });
fireNativeEvent(inputRef, ['change', 'input']);
}}
onReupload={() => {
const newFiles = selectedFiles.filter(({ id }) => id !== file.id);
Expand All @@ -400,7 +392,6 @@ const _FileUpload: React.ForwardRefRenderFunction<BladeElementRef, FileUploadPro
const newFiles = selectedFiles.filter(({ id }) => id !== file.id);
setSelectedFiles(() => newFiles);
onDismiss?.({ file });
fireNativeEvent(inputRef, ['change', 'input']);
}}
onPreview={onPreview}
/>
Expand Down
Loading