Skip to content
Merged
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
70 changes: 61 additions & 9 deletions src/components/form/avatar-input/form-avatar-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Box from "@mui/material/Box";
import Button from "@mui/material/Button";
import Typography from "@mui/material/Typography";
import { styled } from "@mui/material/styles";
import { useCallback, useState } from "react";
import React, { useCallback, useState } from "react";
import { useDropzone } from "react-dropzone";
import {
Controller,
Expand All @@ -15,10 +15,12 @@ import {
FieldValues,
} from "react-hook-form";
import { useTranslation } from "react-i18next";
import IconButton from "@mui/material/IconButton";
import ClearOutlinedIcon from "@mui/icons-material/ClearOutlined";

type AvatarInputProps = {
error?: string;
onChange: (value: FileEntity) => void;
onChange: (value: FileEntity | null) => void;
onBlur: () => void;
value?: FileEntity;
disabled?: boolean;
Expand All @@ -41,6 +43,37 @@ const AvatarInputContainer = styled(Box)(({ theme }) => ({
},
}));

const StyledWrapperAvatar = styled(Box)(() => ({
position: "relative",
width: 100,
height: 100,
}));

const StyledOverlay = styled(Box)(() => {
return {
display: "flex",
justifyContent: "center",
alignItems: "center",
borderRadius: "50%",
position: "absolute",
top: 0,
right: 0,
left: 0,
bottom: 0,
background: "rgba(0, 0, 0, 0.7)",
transition: ".5s ease",
opacity: 0,
"&:hover": {
opacity: 1,
},
};
});

const StyledAvatar = styled(Avatar)(({}) => ({
width: 100,
height: 100,
}));

function AvatarInput(props: AvatarInputProps) {
const { onChange } = props;
const { t } = useTranslation();
Expand Down Expand Up @@ -68,6 +101,13 @@ function AvatarInput(props: AvatarInputProps) {
disabled: isLoading || props.disabled,
});

const removeAvatarHandle = (
event: React.MouseEvent<HTMLButtonElement, MouseEvent>
) => {
event.stopPropagation();
onChange(null);
};

return (
<AvatarInputContainer {...getRootProps()}>
{isDragActive && (
Expand Down Expand Up @@ -95,13 +135,25 @@ function AvatarInput(props: AvatarInputProps) {
</Typography>
</Box>
)}
<Avatar
sx={{
width: 100,
height: 100,
}}
src={props.value?.path}
/>
{props?.value ? (
<StyledWrapperAvatar>
<StyledAvatar src={props.value?.path} />
<StyledOverlay>
<IconButton
disableRipple
onClick={removeAvatarHandle}
color="inherit"
>
<ClearOutlinedIcon
sx={{ width: 50, height: 50, color: "white" }}
/>
</IconButton>
</StyledOverlay>
</StyledWrapperAvatar>
) : (
<StyledAvatar src={props.value?.path} />
)}

<Box sx={{ mt: 2 }}>
<Button variant="contained" component="label" disabled={isLoading}>
{isLoading
Expand Down