Add icon to button

This commit is contained in:
Bartłomiej Głownia 2022-03-02 10:56:05 +01:00 committed by Matthew Russell
parent 3f490f03ca
commit 0e9a3c37c5
2 changed files with 30 additions and 0 deletions

View File

@ -37,3 +37,17 @@ Inline.args = {
children: 'Inline', children: 'Inline',
variant: 'inline', variant: 'inline',
}; };
export const IconPrepend = Template.bind({});
IconPrepend.args = {
children: 'Icon prepend',
prependIconName: 'search',
variant: 'accent',
};
export const IconAppend = Template.bind({});
IconAppend.args = {
children: 'Icon append',
appendIconName: 'search',
variant: 'accent',
};

View File

@ -1,4 +1,5 @@
import classNames from 'classnames'; import classNames from 'classnames';
import { Icon, IconName } from '../icon';
/* eslint-disable-next-line */ /* eslint-disable-next-line */
export interface ButtonProps { export interface ButtonProps {
@ -9,6 +10,8 @@ export interface ButtonProps {
variant?: 'primary' | 'secondary' | 'accent' | 'inline'; variant?: 'primary' | 'secondary' | 'accent' | 'inline';
disabled?: boolean; disabled?: boolean;
className?: string; className?: string;
prependIconName?: IconName;
appendIconName?: IconName;
} }
export function Button({ export function Button({
@ -18,6 +21,8 @@ export function Button({
onClick, onClick,
disabled, disabled,
className, className,
prependIconName,
appendIconName,
}: ButtonProps) { }: ButtonProps) {
const ButtonTag: keyof JSX.IntrinsicElements = tag; const ButtonTag: keyof JSX.IntrinsicElements = tag;
const effectiveClassName = classNames( const effectiveClassName = classNames(
@ -75,13 +80,24 @@ export function Button({
}, },
className className
); );
let icon;
const iconName = prependIconName || appendIconName;
if (iconName !== undefined) {
const iconClassName = classNames(['fill-current'], {
'mr-8': prependIconName,
'ml-8': appendIconName,
});
icon = <Icon name={iconName} className={iconClassName} size={16} />;
}
return ( return (
<ButtonTag <ButtonTag
onClick={onClick} onClick={onClick}
className={effectiveClassName} className={effectiveClassName}
disabled={disabled} disabled={disabled}
> >
{prependIconName && icon}
{children} {children}
{appendIconName && icon}
</ButtonTag> </ButtonTag>
); );
} }