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

SKRF-141 design: 아바타 공통 컴포넌트 구현-리액트 아이콘 미사용 #19

Merged
merged 9 commits into from
Oct 25, 2023
Empty file removed src/componenets/common/index.ts
Empty file.
30 changes: 30 additions & 0 deletions src/components/common/Avatar/Avatar.style.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { AvatarShapeType } from '@/types/user';
import { getAvatarSize } from '@/utils/getAvatarSize';
import styled from '@emotion/styled';

const AvatarContainerStyled = styled.div`
position: relative;
display: inline-flex;
`;

const ProfileImageStyled = styled.img<AvatarShapeType>`
position: relative;
width: ${({ avatarShape }) => getAvatarSize(avatarShape)};
height: ${({ avatarShape }) => getAvatarSize(avatarShape)};
border-radius: ${({ avatarShape }) => (avatarShape === 'rectangle' ? '1.7rem' : '50%')};
object-fit: cover;
cursor: ${({ avatarShape }) => (avatarShape === 'large' ? 'default' : 'pointer')};
`;

const EditButtonStyled = styled.div<AvatarShapeType>`
width: ${({ avatarShape }) => (avatarShape === 'large' ? '4rem' : '2rem')};
height: ${({ avatarShape }) => (avatarShape === 'large' ? '4rem' : '2rem')};
position: absolute;
right: 2%;
bottom: 2%;
border-radius: 50%;
cursor: pointer;
border: 1px solid;
`;

export { getAvatarSize, AvatarContainerStyled, ProfileImageStyled, EditButtonStyled };
44 changes: 44 additions & 0 deletions src/components/common/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { AvatarShapeType } from '@/types/user';
import { getAvatarSize } from '@/utils/getAvatarSize';

import { AvatarContainerStyled, EditButtonStyled, ProfileImageStyled } from './Avatar.style';

interface AvatarProps {
avatarShape: AvatarShapeType['avatarShape'];
profileImageSrc?: string;
isEditable?: boolean;
onClick?: () => void;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

@colorkite10 p1;
채연님도 onClick에 대한 타입 정의때문에 고민 많이 하셨을 것 같아요!

Suggested change
interface AvatarProps {
avatarShape: AvatarShapeType['avatarShape'];
profileImageSrc?: string;
isEditable?: boolean;
onClick?: () => void;
}
interface AvatarProps extends React.HTMLAttributes<HTMLDivElement> {
avatarShape: AvatarShapeType['avatarShape'];
profileImageSrc?: string;
isEditable?: boolean;
}

이렇게 하면 onClick의 타입을 HTMLDivElement에서 상속받아 사용할 수 있습니다!

1.HTMLDivElement 설명 https://developer.mozilla.org/ko/docs/Web/API/HTMLDivElement
2.HTMLDIvElement에 선언된 타입들
https://microsoft.github.io/PowerBI-JavaScript/interfaces/_node_modules_typedoc_node_modules_typescript_lib_lib_dom_d_.htmldivelement.html#onclick

Copy link
Collaborator

Choose a reason for hiding this comment

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

오옹~ 저도 활용해야겠군요! 꿀팁 감사합니다


const Avatar = ({ avatarShape, profileImageSrc, isEditable, onClick }: AvatarProps) => {
const width = getAvatarSize(avatarShape);

return (
<div>
<AvatarContainerStyled onClick={onClick}>
{profileImageSrc ? (
<ProfileImageStyled
avatarShape={avatarShape}
src={profileImageSrc}
alt="profile image"
style={{ width: `${width}`, height: `${width}` }}
/>
) : (
<ProfileImageStyled
src="https://picsum.photos/200/300"
avatarShape={avatarShape}
style={{ width: `${width}`, height: `${width}` }}
alt="이 부분은 추후 수정 예정입니다."
/>
)}
{isEditable && (
<EditButtonStyled avatarShape={avatarShape}>
<span>편집아이콘추가예정</span>
</EditButtonStyled>
)}
</AvatarContainerStyled>
</div>
);
};

export default Avatar;
2 changes: 1 addition & 1 deletion src/pages/RegisterPage/RegisterPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import InputForm from '@/componenets/common/InputForm/InputForm';
import InputForm from '@/components/common/InputForm/InputForm';

import { Container, SubmitBtn, Title } from './RegisterPage.style';

Expand Down
9 changes: 9 additions & 0 deletions src/types/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
interface User {
accessToken: string;
}

interface AvatarShapeType {
avatarShape: 'rectangle' | 'normal' | 'large' | 'small' | 'medium';
}

export { User, AvatarShapeType };
18 changes: 18 additions & 0 deletions src/utils/getAvatarSize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { AvatarShapeType } from '@/types/user';

export const getAvatarSize = (avatarShape: AvatarShapeType['avatarShape']) => {
switch (avatarShape) {
case 'small':
return '1.5rem';
case 'normal':
return '4rem';
case 'rectangle':
return '4.5rem';
case 'medium':
return '6.875rem';
case 'large':
return '16.3rem';
default:
return '4rem';
}
};