-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathButton.tsx
More file actions
65 lines (60 loc) · 1.56 KB
/
Button.tsx
File metadata and controls
65 lines (60 loc) · 1.56 KB
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
import React, { MouseEvent } from 'react';
import { Spinner } from '../Spinner/Spinner';
import { Typography } from '../Typography';
import { StyledButton } from './ButtonStyle';
export type ButtonSize = 'default' | 'slim' | 'large';
export type IconPosition = 'left' | 'right';
export interface ButtonProps
extends React.ButtonHTMLAttributes<HTMLButtonElement> {
size?: ButtonSize;
children?: React.ReactNode;
/** Indicate if the button is currently selected */
active?: boolean;
fullWidth?: boolean;
icon?: React.ReactNode;
iconPosition?: IconPosition;
loading?: boolean;
}
export const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
function Button(
{
children,
icon,
iconPosition = 'left',
active,
fullWidth,
loading,
...otherProps
}: ButtonProps,
ref
) {
const content =
typeof children === 'string' ? (
<Typography variant="button"> {children}</Typography>
) : (
children
);
const renderIcon = (position: IconPosition) => {
if (icon && position === iconPosition) {
return icon;
}
return null;
};
return (
<StyledButton
ref={ref}
data-active={active}
data-full-width={fullWidth}
data-loading={loading}
data-icon={!!icon}
{...otherProps}
onMouseUp={(e: MouseEvent<HTMLButtonElement>) => e.currentTarget.blur()}
>
{loading && <Spinner />}
{renderIcon('left')}
{content}
{renderIcon('right')}
</StyledButton>
);
}
);