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

feat(FileUpload): add onReupload prop in case of error state #2201

Merged
merged 6 commits into from
May 29, 2024
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
5 changes: 5 additions & 0 deletions .changeset/eight-elephants-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@razorpay/blade": patch
---

feat(FileUpload): add `onReupload` prop to use in case of error state
35 changes: 33 additions & 2 deletions packages/blade/src/components/FileUpload/FileUpload.web.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useCallback, useMemo, forwardRef } from 'react';
import { useState, useCallback, useMemo, useRef, forwardRef } from 'react';
import type { FileUploadProps, BladeFile, BladeFileList } from './types';
import { StyledFileUploadWrapper } from './StyledFileUploadWrapper';
import {
Expand All @@ -24,6 +24,7 @@ import type { BladeElementRef } from '~utils/types';
import { getHintType } from '~components/Input/BaseInput/BaseInput';
import { makeAccessible } from '~utils/makeAccessible';
import { formHintLeftLabelMarginLeft } from '~components/Input/BaseInput/baseInputTokens';
import { useMergeRefs } from '~utils/useMergeRefs';

const _FileUpload: React.ForwardRefRenderFunction<BladeElementRef, FileUploadProps> = (
{
Expand All @@ -33,6 +34,7 @@ const _FileUpload: React.ForwardRefRenderFunction<BladeElementRef, FileUploadPro
onChange,
onPreview,
onRemove,
onReupload,
onDismiss,
onDrop,
isDisabled,
Expand All @@ -53,6 +55,8 @@ const _FileUpload: React.ForwardRefRenderFunction<BladeElementRef, FileUploadPro
},
ref,
): React.ReactElement => {
const inputRef = useRef<HTMLInputElement | null>(null);
const mergedRef = useMergeRefs(ref, inputRef);
const { platform } = useTheme();
const [selectedFiles, setSelectedFiles] = useState<BladeFileList>(fileList ?? []);
const [errorMessage, setErrorMessage] = useState(errorText);
Expand Down Expand Up @@ -262,7 +266,7 @@ const _FileUpload: React.ForwardRefRenderFunction<BladeElementRef, FileUploadPro
onBlur: () => setIsActive(false),
...accessibilityProps,
}}
ref={ref}
ref={mergedRef}
/>

<Box
Expand Down Expand Up @@ -305,6 +309,20 @@ const _FileUpload: React.ForwardRefRenderFunction<BladeElementRef, FileUploadPro
setSelectedFiles(newFiles);
onRemove?.({ file: selectedFiles[0] });
}}
onReupload={() => {
const newFiles = selectedFiles.filter(({ id }) => id !== selectedFiles[0].id);
setSelectedFiles(newFiles);
inputRef.current?.click();

// TODO - Remove this in the next major release
// Fallback to onRemove if onReupload isn't provided to avoid breaking changes in the API
if (onReupload) {
onReupload({ file: selectedFiles[0] });
} else {
onRemove?.({ file: selectedFiles[0] });
}
setIsActive(false);
}}
onDismiss={() => {
const newFiles = selectedFiles.filter(({ id }) => id !== selectedFiles[0].id);
setSelectedFiles(newFiles);
Expand Down Expand Up @@ -353,6 +371,19 @@ const _FileUpload: React.ForwardRefRenderFunction<BladeElementRef, FileUploadPro
setSelectedFiles(newFiles);
onRemove?.({ file });
}}
onReupload={() => {
const newFiles = selectedFiles.filter(({ id }) => id !== file.id);
setSelectedFiles(newFiles);
inputRef.current?.click();
// TODO - Remove this in the next major release
// Fallback to onRemove if onReupload isn't provided to avoid breaking changes in the API
if (onReupload) {
onReupload({ file: selectedFiles[0] });
} else {
onRemove?.({ file: selectedFiles[0] });
}
setIsActive(false);
}}
onDismiss={() => {
const newFiles = selectedFiles.filter(({ id }) => id !== file.id);
setSelectedFiles(newFiles);
Expand Down
21 changes: 19 additions & 2 deletions packages/blade/src/components/FileUpload/FileUploadItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ import { memo } from 'react';
import { StyledFileUploadItemWrapper } from './StyledFileUploadItemWrapper';
import type { FileUploadItemProps } from './types';
import { FileUploadItemIcon } from './FileUploadItemIcon';
import { TrashIcon, EyeIcon, CloseIcon, CheckCircleIcon } from '~components/Icons';
import { TrashIcon, EyeIcon, CloseIcon, CheckCircleIcon, RefreshIcon } from '~components/Icons';
import { BaseBox } from '~components/Box/BaseBox';
import { Text } from '~components/Typography';
import { Divider } from '~components/Divider';
import { IconButton } from '~components/Button/IconButton';
import { ProgressBar } from '~components/ProgressBar';
import isUndefined from '~utils/lodashButBetter/isUndefined';
import { BaseLink } from '~components/Link/BaseLink';

const FileUploadItem = memo(
({
file,
onPreview,
onRemove,
onReupload,
onDismiss,
size: containerSize,
}: FileUploadItemProps): React.ReactElement => {
Expand Down Expand Up @@ -73,14 +75,29 @@ const FileUploadItem = memo(
} ${isUploading && uploadPercent ? `(${uploadPercent}%)` : ''}`}
</Text>
</BaseBox>
{status === 'error' || status === 'uploading' ? (
{status === 'uploading' ? (
<BaseBox>
<IconButton
accessibilityLabel="Remove File"
icon={CloseIcon}
onClick={() => onDismiss?.({ file })}
/>
</BaseBox>
) : status === 'error' ? (
<BaseBox display="flex" flexDirection="row" alignItems="center">
<BaseLink
marginX="spacing.1"
variant="button"
icon={RefreshIcon}
color="negative"
size="small"
onClick={() => {
onReupload?.({ file });
}}
>
Re-upload
</BaseLink>
</BaseBox>
) : (
<BaseBox display="flex" flexDirection="row" alignItems="center">
{onPreview ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const StyledFileUploadItemWrapper = styled(BaseBox)<StyledFileUploadItemWrapperP
display: 'flex',
justifyContent: 'space-between',
borderStyle: 'solid',
height: makeSize(fileUploadHeightTokens[size]),
minHeight: makeSize(fileUploadHeightTokens[size]),
width: '100%',
backgroundColor: getIn(theme.colors, fileUploadItemBackgroundColors[status].default),
transitionProperty: 'background-color',
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 @@ -81,6 +81,10 @@ type FileUploadCommonProps = {
* Callback function triggered when a file is removed
*/
onRemove?: ({ file }: { file: File }) => void;
/**
* Callback function triggered when a file upload is retried
*/
onReupload?: ({ file }: { file: File }) => void;
/**
* Callback function triggered when a file upload is dismissed
*/
Expand Down Expand Up @@ -146,7 +150,7 @@ type FileUploadProps = (FileUploadPropsWithA11yLabel | FileUploadPropsWithLabel)

type FileUploadItemProps = Pick<
FileUploadProps,
'onPreview' | 'onRemove' | 'onDismiss' | 'size'
'onPreview' | 'onRemove' | 'onDismiss' | 'onReupload' | 'size'
> & {
file: BladeFile;
};
Expand Down
Loading