-
Notifications
You must be signed in to change notification settings - Fork 355
/
Button.tsx
152 lines (131 loc) · 3.97 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import cn from "classnames";
import { pick } from "lodash";
import React, { Ref } from "react";
import { withForwardRef, withStyles } from "coral-ui/hocs";
import { PropTypesOf } from "coral-ui/types";
import BaseButton, { BaseButtonProps } from "../BaseButton";
import styles from "./Button.css";
interface Props extends Omit<BaseButtonProps, "ref"> {
/** If set renders an anchor tag instead */
anchor?: boolean;
href?: string;
target?: string;
to?: string;
/**
* This prop can be used to add custom classnames.
* It is handled by the `withStyles `HOC.
*/
classes: typeof styles & BaseButtonProps["classes"];
/** Size of the button */
size?: "small" | "regular" | "large";
/** Color of the button */
color?: "regular" | "alert" | "mono" | "alt" | "dark" | "stream";
/** Variant of the button */
variant?:
| "regular"
| "flat"
| "outline"
| "text"
| "underlined"
| "textUnderlined";
/* button is attached to the LEFT of an input element */
adornmentLeft?: boolean;
/* button is attached to the RIGHT of an input element */
adornmentRight?: boolean;
/* button has a ButtonIcon to the left of the text */
iconLeft?: boolean;
/* button has a ButtonIcon to the right of the text */
iconRight?: boolean;
/** If set renders a full width button */
fullWidth?: boolean;
/** if set to false, button text will not be uppercase */
uppercase?: boolean;
/** If set renders active state e.g. to implement toggle buttons */
active?: boolean;
type?: "submit" | "reset" | "button";
/** Internal: Forwarded Ref */
forwardRef?: Ref<HTMLButtonElement>;
/* if true adds a link-style underline */
underline?: boolean;
}
export class Button extends React.Component<Props> {
public static defaultProps: Partial<Props> = {
size: "regular",
variant: "regular",
color: "regular",
uppercase: true,
};
public render() {
const {
active,
classes,
color,
className,
children,
size,
fullWidth,
disabled,
forwardRef,
variant,
type,
uppercase,
iconLeft,
iconRight,
adornmentLeft,
adornmentRight,
underline,
to,
...rest
} = this.props;
const rootClassName = cn(
classes.root,
{
[classes.sizeSmall]: size === "small",
[classes.sizeRegular]: size === "regular",
[classes.sizeLarge]: size === "large",
[classes.colorRegular]: color === "regular",
[classes.colorAlert]: color === "alert",
[classes.colorAlt]: color === "alt",
[classes.colorMono]: color === "mono",
[classes.colorDark]: color === "dark",
[classes.colorStream]: color === "stream",
[classes.variantRegular]: variant === "regular",
[classes.variantFlat]: variant === "flat",
[classes.variantOutline]: variant === "outline",
[classes.variantText]: variant === "text",
[classes.variantUnderlined]: variant === "underlined",
[classes.variantTextUnderlined]: variant === "textUnderlined",
[classes.uppercase]: uppercase,
[classes.fullWidth]: fullWidth,
[classes.active]: active,
[classes.disabled]: disabled,
[classes.iconLeft]: iconLeft,
[classes.iconRight]: iconRight,
[classes.adornmentLeft]: adornmentLeft,
[classes.adornmentRight]: adornmentRight,
[classes.underline]: underline,
},
className
);
return (
<BaseButton
className={rootClassName}
classes={pick(classes, "keyboardFocus", "mouseHover")}
disabled={disabled}
ref={forwardRef}
type={type}
data-variant={variant}
data-color={color}
to={to}
data-active={active}
aria-pressed={active}
{...rest}
>
{children}
</BaseButton>
);
}
}
const enhanced = withForwardRef(withStyles(styles)(Button));
export type ButtonProps = PropTypesOf<typeof enhanced>;
export default enhanced;