-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add FieldUploadPreview component on AccountProfileForm
- Loading branch information
Fabien Essid
committed
Dec 21, 2023
1 parent
f050519
commit 82df320
Showing
7 changed files
with
193 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import React, { FC, useCallback, useEffect, useState } from 'react'; | ||
|
||
import { Box, Flex, FlexProps, IconButton } from '@chakra-ui/react'; | ||
import { useFormContext, useFormFields } from '@formiz/core'; | ||
import { LuX } from 'react-icons/lu'; | ||
|
||
import { FieldUploadValue } from '@/components/FieldUpload'; | ||
|
||
const ImagePreview = ({ | ||
image, | ||
onClick, | ||
}: { | ||
image: string; | ||
onClick: React.MouseEventHandler<HTMLButtonElement>; | ||
}) => { | ||
return ( | ||
<Box | ||
position="relative" | ||
bg="gray.200" | ||
height="100%" | ||
aspectRatio="1" | ||
overflow="hidden" | ||
rounded="md" | ||
> | ||
<Box as="img" width="100%" height="100%" objectFit="cover" src={image} /> | ||
<IconButton | ||
position="absolute" | ||
top="1" | ||
right="1" | ||
icon={<LuX fontSize="sm" />} | ||
aria-label="Remove" | ||
rounded="full" | ||
minWidth="6" | ||
minHeight="6" | ||
width="6" | ||
height="6" | ||
onClick={onClick} | ||
/> | ||
</Box> | ||
); | ||
}; | ||
|
||
export type FieldUploadPreviewProps = FlexProps & { | ||
uploaderName: string; | ||
}; | ||
|
||
export const FieldUploadPreview: FC< | ||
React.PropsWithChildren<FieldUploadPreviewProps> | ||
> = ({ uploaderName, ...rest }) => { | ||
const fields = useFormFields({ | ||
fields: [uploaderName] as const, // To get the uploader values | ||
}); | ||
const form = useFormContext(); | ||
|
||
const [fileToPreview, setFileToPreview] = useState<string>(); | ||
|
||
const previewFile = useCallback(async () => { | ||
const uploaderFieldValue = fields?.[uploaderName] | ||
?.value as FieldUploadValue; | ||
|
||
if (!uploaderFieldValue || !uploaderFieldValue?.name) { | ||
setFileToPreview(undefined); | ||
return; | ||
} | ||
|
||
const hasUserUploadedAFile = uploaderFieldValue.file; | ||
const hasDefaultFileSet = uploaderFieldValue.name && !hasUserUploadedAFile; | ||
|
||
if (hasDefaultFileSet) { | ||
setFileToPreview(uploaderFieldValue.name); | ||
return; | ||
} | ||
|
||
const uploadedFileToPreview = await new Promise<string>( | ||
(resolve, reject) => { | ||
const reader = new FileReader(); | ||
reader.onloadend = () => resolve(reader.result?.toString() ?? ''); | ||
reader.onerror = reject; | ||
if (uploaderFieldValue.file) { | ||
reader.readAsDataURL(uploaderFieldValue.file); | ||
} | ||
} | ||
); | ||
|
||
setFileToPreview(uploadedFileToPreview); | ||
}, [fields, uploaderName]); | ||
|
||
useEffect(() => { | ||
previewFile(); | ||
}, [previewFile]); | ||
|
||
return ( | ||
fileToPreview && ( | ||
<Flex | ||
mt={2} | ||
background="white" | ||
height="32" | ||
width="full" | ||
rounded="md" | ||
boxShadow="sm" | ||
border="1px solid" | ||
borderColor="gray.200" | ||
alignItems="center" | ||
p={4} | ||
{...rest} | ||
> | ||
<ImagePreview | ||
image={fileToPreview} | ||
onClick={() => { | ||
form.setValues({ [uploaderName]: null }); | ||
}} | ||
/> | ||
</Flex> | ||
) | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters