-
Notifications
You must be signed in to change notification settings - Fork 843
/
button.js
177 lines (154 loc) Β· 3.24 KB
/
button.js
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import {
EuiLoadingSpinner
} from '../loading';
import {
checkHrefAndOnClick,
getSecureRelForTarget,
} from '../../services';
import {
ICON_TYPES,
EuiIcon,
} from '../icon';
const colorToClassNameMap = {
primary: 'euiButton--primary',
secondary: 'euiButton--secondary',
warning: 'euiButton--warning',
danger: 'euiButton--danger',
ghost: 'euiButton--ghost',
};
export const COLORS = Object.keys(colorToClassNameMap);
const sizeToClassNameMap = {
s: 'euiButton--small',
l: 'euiButton--large',
};
export const SIZES = Object.keys(sizeToClassNameMap);
const iconSideToClassNameMap = {
left: null,
right: 'euiButton--iconRight',
};
export const ICON_SIDES = Object.keys(iconSideToClassNameMap);
export const EuiButton = ({
children,
className,
iconType,
iconSide,
color,
size,
fill,
isDisabled,
isLoading,
href,
target,
rel,
onClick,
type,
buttonRef,
...rest
}) => {
// If in the loading state, force disabled to true
isDisabled = isLoading ? true : isDisabled;
const classes = classNames(
'euiButton',
colorToClassNameMap[color],
sizeToClassNameMap[size],
iconSideToClassNameMap[iconSide],
className,
{
'euiButton--fill': fill,
},
);
// Add an icon to the button if one exists.
let buttonIcon;
if (isLoading) {
buttonIcon = (
<EuiLoadingSpinner
className="euiButton__spinner"
size="m"
/>
);
} else if (iconType) {
buttonIcon = (
<EuiIcon
className="euiButton__icon"
type={iconType}
size="m"
aria-hidden="true"
/>
);
}
if (href) {
const secureRel = getSecureRelForTarget(target, rel);
return (
<a
className={classes}
href={href}
target={target}
rel={secureRel}
ref={buttonRef}
{...rest}
>
<span className="euiButton__content">
{buttonIcon}
<span className="euiButton__text">{children}</span>
</span>
</a>
);
} else {
return (
<button
disabled={isDisabled}
className={classes}
onClick={onClick}
type={type}
ref={buttonRef}
{...rest}
>
<span className="euiButton__content">
{buttonIcon}
<span className="euiButton__text">{children}</span>
</span>
</button>
);
}
};
EuiButton.propTypes = {
children: PropTypes.node,
className: PropTypes.string,
/**
* See EuiIcon
*/
iconType: PropTypes.oneOf(ICON_TYPES),
iconSide: PropTypes.oneOf(ICON_SIDES),
/**
* Add more focus to an action
*/
fill: PropTypes.bool,
/**
* Define the color of the button
*/
color: PropTypes.oneOf(COLORS),
size: PropTypes.oneOf(SIZES),
isDisabled: PropTypes.bool,
href: checkHrefAndOnClick,
target: PropTypes.string,
rel: PropTypes.string,
onClick: PropTypes.func,
/**
* Adds/swaps for loading spinner & disables
*/
isLoading: PropTypes.bool,
/**
* Standard HTML attribute
*/
type: PropTypes.string,
buttonRef: PropTypes.func,
};
EuiButton.defaultProps = {
type: 'button',
iconSide: 'left',
color: 'primary',
fill: false,
};