feat: support new feature in button component

This commit is contained in:
QiShaoXuan
2023-01-30 18:29:00 +08:00
parent bf6545af7a
commit 0636622158
3 changed files with 19 additions and 7 deletions

View File

@@ -15,16 +15,26 @@ export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
hoverStyle, hoverStyle,
shape = 'default', shape = 'default',
icon, icon,
iconPosition = 'start',
type = 'default', type = 'default',
children, children,
bold = false, bold = false,
loading = false, loading = false,
noBorder = false,
...props ...props
}, },
ref ref
) => { ) => {
const { iconSize } = getSize(size); const { iconSize } = getSize(size);
const iconElement =
icon &&
cloneElement(Children.only(icon), {
width: iconSize,
height: iconSize,
className: `affine-button-icon ${icon.props.className ?? ''}`,
});
return ( return (
<StyledButton <StyledButton
ref={ref} ref={ref}
@@ -38,19 +48,16 @@ export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
// @ts-ignore // @ts-ignore
type={type} type={type}
bold={bold} bold={bold}
noBorder={noBorder}
{...props} {...props}
> >
{loading ? ( {loading ? (
<Loading type={type}></Loading> <Loading type={type}></Loading>
) : ( ) : (
<> <>
{icon && {iconPosition === 'start' && iconElement}
cloneElement(Children.only(icon), {
width: iconSize,
height: iconSize,
className: `affine-button-icon ${icon.props.className ?? ''}`,
})}
{children && <span>{children}</span>} {children && <span>{children}</span>}
{iconPosition === 'end' && iconElement}
</> </>
)} )}
</StyledButton> </StyledButton>

View File

@@ -17,8 +17,10 @@ export type ButtonProps = PropsWithChildren &
hoverColor?: CSSProperties['color']; hoverColor?: CSSProperties['color'];
hoverStyle?: CSSProperties; hoverStyle?: CSSProperties;
icon?: ReactElement; icon?: ReactElement;
iconPosition?: 'start' | 'end';
shape?: 'default' | 'round' | 'circle'; shape?: 'default' | 'round' | 'circle';
type?: 'primary' | 'warning' | 'danger' | 'default'; type?: 'primary' | 'warning' | 'danger' | 'default';
bold?: boolean; bold?: boolean;
loading?: boolean; loading?: boolean;
noBorder?: boolean;
}; };

View File

@@ -149,6 +149,7 @@ export const StyledButton = styled('button', {
'hoverStyle', 'hoverStyle',
'type', 'type',
'bold', 'bold',
'noBorder',
].includes(prop); ].includes(prop);
}, },
})< })<
@@ -162,6 +163,7 @@ export const StyledButton = styled('button', {
| 'shape' | 'shape'
| 'type' | 'type'
| 'bold' | 'bold'
| 'noBorder'
> >
>( >(
({ ({
@@ -174,6 +176,7 @@ export const StyledButton = styled('button', {
bold = false, bold = false,
shape = 'default', shape = 'default',
type = 'default', type = 'default',
noBorder = false,
}) => { }) => {
const { fontSize, borderRadius, padding, height } = getSize(size); const { fontSize, borderRadius, padding, height } = getSize(size);
@@ -181,7 +184,7 @@ export const StyledButton = styled('button', {
height, height,
paddingLeft: padding, paddingLeft: padding,
paddingRight: padding, paddingRight: padding,
border: '1px solid', border: noBorder ? 'none' : '1px solid',
...displayInlineFlex('center', 'center'), ...displayInlineFlex('center', 'center'),
position: 'relative', position: 'relative',
// TODO: disabled color is not decided // TODO: disabled color is not decided