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] Chip 컴포넌트 추가 #70

Merged
merged 17 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion apps/web/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export default function Home() {
</Breadcrumb>
</div>
<div style={{ display: 'flex', flexDirection: 'column', gap: '8px' }}>
<Chip variant="green" leftAddon={<Icon name="circle" type="fill" />}>
<Chip variant="green" leftAddon={<Chip.Icon variant="green" />}>
업로드할 글
</Chip>
<Chip variant="grey" leftAddon={<Icon name="circle" type="fill" />}>
Expand Down
66 changes: 7 additions & 59 deletions packages/ui/src/components/Chip/Chip.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,9 @@
import { ComponentPropsWithoutRef, forwardRef, ReactElement } from 'react';
import { addonRootRecipe, chipCloseButtonRecipe, chipRecipe } from './Chip.css';
import { Icon } from '../Icon/Icon';
import { Text } from '../Text/Text';
import { ChipIcon } from './ChipIcon';
import { ChipItem } from './ChipItem';

export type ButtonVariant = 'grey' | 'purple' | 'green';
export const Chip = Object.assign(ChipItem, {
Icon: ChipIcon,
});

export type ChipProps = ComponentPropsWithoutRef<'span'> & {
variant: ButtonVariant;
leftAddon?: ReactElement;
rightAddon?: ReactElement;
closable?: boolean;
onClose?: () => void;
};

export const Chip = forwardRef<HTMLSpanElement, ChipProps>(
({
className = '',
variant,
leftAddon,
rightAddon,
closable = false,
children,
onClose,
...rest
}) => {
return (
<span className={`${chipRecipe({ variant })} ${className}`} {...rest}>
{leftAddon && (
<span className={addonRootRecipe({ color: variant })}>
{leftAddon}
</span>
)}
<Text fontSize={16} fontWeight="semibold">
{children}
</Text>
{rightAddon && (
<span className={addonRootRecipe({ color: variant })}>
{rightAddon}
</span>
)}
{closable && (
<button
type="button"
aria-label="Close"
className={chipCloseButtonRecipe({ clickable: closable })}
onClick={onClose}
>
<Icon
name="x"
size="1.6rem"
type="stroke"
strokeWidth={'0.244rem'}
color={`${variant}400`}
/>
</button>
)}
</span>
);
}
);
export type { ChipProps } from './ChipItem';
export type { ChipIconProps } from './ChipIcon';
24 changes: 24 additions & 0 deletions packages/ui/src/components/Chip/ChipIcon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ColorsType } from '@repo/theme';
import { Icon, IconProps } from '../Icon/Icon';
import { ButtonVariant } from './ChipItem';

export type ChipIconProps = {
variant: ButtonVariant;
name?: IconProps['name'];
type?: IconProps['type'];
} & Omit<IconProps, 'color' | 'name' | 'type'>;

const color: Record<ButtonVariant, keyof ColorsType> = {
grey: 'grey600',
purple: 'purple800',
green: 'green800',
};

export function ChipIcon({
variant,
name = 'circle',
type = 'fill',
...rest
}: ChipIconProps) {
return <Icon name={name} type={type} color={color[variant]} {...rest} />;
}
54 changes: 54 additions & 0 deletions packages/ui/src/components/Chip/ChipItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { ComponentPropsWithoutRef, forwardRef, ReactElement } from 'react';
import { chipCloseButtonRecipe, chipRecipe } from './Chip.css';
import { Icon } from '../Icon/Icon';
import { Text } from '../Text/Text';
import { isNil } from '../../utils';

export type ButtonVariant = 'grey' | 'purple' | 'green';

export type ChipProps = ComponentPropsWithoutRef<'span'> & {
variant: ButtonVariant;
leftAddon?: ReactElement;
rightAddon?: ReactElement;
closable?: boolean;
onClose?: () => void;
};

export const ChipItem = forwardRef<HTMLSpanElement, ChipProps>(
({
className = '',
variant,
leftAddon,
rightAddon,
closable = false,
children,
onClose,
...rest
}) => {
return (
<span className={`${chipRecipe({ variant })} ${className}`} {...rest}>
{leftAddon}
Copy link
Contributor

Choose a reason for hiding this comment

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

여기다가 !isNil 다는 것이 좋지 않을까 리뷰 드렸었어요! isNotNil은 제가 다른 브랜치에 만들어놔서, 그 브랜치 반영되면 추후에 수정해둘게요!

<Text fontSize={16} fontWeight="semibold">
{children}
</Text>
{rightAddon}
{isNil(closable) && (
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

closable 로직 문제
isNil(closable) 조건은 null 또는 undefined인 경우만 닫기 버튼을 표시하므로, closablefalse일 때도 숨겨지는 문제가 있습니다. 의도대로라면 closable === true인 경우 버튼을 표시하는 로직으로 수정해야 합니다.

-{isNil(closable) && (
+{closable && (
    <button
      type="button"
      aria-label="Close"
      ...
    >
      ...
    </button>
)}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
{isNil(closable) && (
{closable && (

Copy link
Contributor

Choose a reason for hiding this comment

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

closable하지 않으면 close 버튼을 띄워주는 것이 아니라 반대가 아닌가요?.?

그리고 이미 closable은 boolean 값이라 nullish 체크를 별도로 할 필요가 없어요!

Copy link
Member Author

Choose a reason for hiding this comment

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

아아 저 방금 발견해서 수정했습니다 다시 확인 부탁드려요!

<button
type="button"
aria-label="Close"
className={chipCloseButtonRecipe({ clickable: closable })}
onClick={onClose}
>
<Icon
name="x"
size="1.6rem"
type="stroke"
strokeWidth={'0.244rem'}
color={`${variant}400`}
/>
</button>
)}
</span>
);
}
);
3 changes: 2 additions & 1 deletion packages/ui/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ChipIcon } from './Chip/ChipIcon';
export { IconButton } from './IconButton/IconButton';
export type { IconButtonProps } from './IconButton/IconButton';
export { Spacing } from './Spacing/Spacing';
Expand Down Expand Up @@ -32,7 +33,7 @@ export type {
TextFieldCounterProps,
} from './TextField/TextField';
export { Chip } from './Chip/Chip';
export type { ChipProps } from './Chip/Chip';
export type { ChipProps, ChipIconProps } from './Chip/Chip';
export { RadioCards } from './RadioCards/RadioCards';
export type {
RadioCardsProps,
Expand Down
Loading