/
Button.tsx
67 lines (54 loc) · 2.05 KB
/
Button.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { ThemeInterface } from '@bigcommerce/big-design-theme';
import React, { Ref } from 'react';
import { MarginProps } from '../../mixins';
import { Spinner } from '../Spinner';
import { ContentWrapper, StyledButton } from './styled';
interface PrivateProps {
forwardedRef: Ref<HTMLButtonElement>;
}
export interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement>, MarginProps {
actionType?: 'normal' | 'destructive';
iconLeft?: React.ReactChild;
iconOnly?: React.ReactChild;
iconRight?: React.ReactChild;
isLoading?: boolean;
theme?: ThemeInterface;
variant?: 'primary' | 'secondary' | 'subtle';
}
class RawButton extends React.PureComponent<ButtonProps & PrivateProps> {
render() {
const { forwardedRef, ...props } = this.props;
return (
<StyledButton role="button" tabIndex={0} {...props} onClick={this.handleClick} ref={forwardedRef}>
{props.isLoading ? <Spinner overlay={false} /> : null}
<ContentWrapper isLoading={props.isLoading} theme={props.theme}>
{!props.iconOnly && props.iconLeft}
{props.iconOnly}
{!props.iconOnly && props.children}
{!props.iconOnly && props.iconRight}
</ContentWrapper>
</StyledButton>
);
}
private handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
const { disabled, isLoading } = this.props;
if (this.props.onClick && !disabled && !isLoading) {
this.props.onClick(event);
}
};
}
export const StyleableButton = React.forwardRef<HTMLButtonElement, ButtonProps>((props, ref) => (
<RawButton {...props} forwardedRef={ref} />
));
export const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(({ className, style, ...props }, ref) => (
<RawButton {...props} forwardedRef={ref} />
));
const defaultProps = {
actionType: 'normal' as 'normal',
isLoading: false,
variant: 'primary' as 'primary',
};
Button.displayName = 'Button';
Button.defaultProps = { ...defaultProps };
StyleableButton.displayName = 'StyleableButton';
StyleableButton.defaultProps = { ...defaultProps };