refactor: refactor button with icon component

This commit is contained in:
jinhojang6 2023-02-21 23:26:36 +09:00
parent 8d338fc577
commit f5057099f1
No known key found for this signature in database
GPG Key ID: 0E7AA62CB0D9E6F3
2 changed files with 35 additions and 13 deletions

View File

@ -1,15 +1,42 @@
import { Meta, Story } from '@storybook/react'
import { useStorybookIconComponent } from '../../utils/storybook.utils'
import { Button, ButtonProps } from './Button'
export default {
title: 'Button',
component: Button,
argTypes: {
size: {
type: {
name: 'enum',
value: ['small', 'medium', 'large'],
},
defaultValue: 'large',
},
icon: {
type: {
name: 'enum',
value: useStorybookIconComponent.options,
},
},
},
} as Meta
export const Root: Story<ButtonProps> = (args) => (
<Button {...args}>Button</Button>
)
export const Root: Story<ButtonProps & { icon: string }> = ({
icon,
...args
}) => {
const IconComponent = useStorybookIconComponent(icon)
return (
<Button
{...args}
icon={IconComponent && <IconComponent color="primary"></IconComponent>}
>
Button
</Button>
)
}
Root.args = {
disabled: false,
withIcon: false,
}

View File

@ -1,16 +1,15 @@
import clsx from 'clsx'
import React from 'react'
import { PickIcon } from '../Icons'
import { buttonClasses } from './Button.classes'
export type ButtonProps = React.ButtonHTMLAttributes<HTMLButtonElement> & {
size?: 'large' | 'medium' | 'small'
withIcon?: boolean
icon: React.ReactNode
}
export const Button: React.FC<ButtonProps> & {
classes: typeof buttonClasses
} = ({ size = 'medium', withIcon = 'false', children, ...props }) => {
} = ({ size = 'medium', icon, children, ...props }) => {
return (
<>
<button
@ -20,15 +19,11 @@ export const Button: React.FC<ButtonProps> & {
buttonClasses.root,
buttonClasses[size],
props.disabled && buttonClasses.disabled,
withIcon && buttonClasses.withIcon,
icon && buttonClasses.withIcon,
)}
>
<span className={buttonClasses.text}>{children}</span>
{withIcon && (
<span className={buttonClasses.icon}>
{<PickIcon color="primary" />}
</span>
)}
{icon && <span className={buttonClasses.icon}>{icon}</span>}
</button>
</>
)