Skip to content

Commit

Permalink
fix(FileUpload): revert to not calling onChange on file remove (#2521)
Browse files Browse the repository at this point in the history
* fix: revert to not calling onChange on file remove

* Create moody-coats-clean.md
  • Loading branch information
saurabhdaware authored Feb 5, 2025
1 parent 957049c commit 3be3f0f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
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(() => {
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

0 comments on commit 3be3f0f

Please sign in to comment.