-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathButton.jsx
107 lines (99 loc) · 3.07 KB
/
Button.jsx
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
import React from 'react';
import PropTypes from 'prop-types';
import { Button as CarbonButton, Loading } from '@carbon/react';
import classnames from 'classnames';
import { settings } from '../../constants/Settings';
import deprecate from '../../internal/deprecate';
const ButtonKinds = [
'primary',
'secondary',
'tertiary',
'ghost',
'danger',
'danger--primary',
'danger--ghost',
'danger--tertiary',
];
const { iotPrefix } = settings;
const propTypes = {
/** Show loading spinner, only new prop */
loading: PropTypes.bool,
/** Disable the button will be auto disabled when loading */
disabled: PropTypes.bool,
/** Button label */
children: PropTypes.node,
/** click handler */
onClick: PropTypes.func, // eslint-disable-line react/require-default-props
className: PropTypes.string,
/** primary, secondary, etc from carbon */
kind: PropTypes.oneOf([...ButtonKinds, 'icon-selection']),
/** display green border to denote a recommended button to select, to be used with kind: 'icon-selection' */
recommended: PropTypes.bool,
/** Specify if the button is an icon-only button */
hasIconOnly: PropTypes.bool,
/** Toggle selected styling for buttons of kind=icon-selection */
selected: PropTypes.bool,
// TODO: remove deprecated testID prop in v3
// eslint-disable-next-line react/require-default-props
testID: deprecate(
PropTypes.string,
`The 'testID' prop has been deprecated. Please use 'testId' instead.`
),
/** Id that can be used for testing */
testId: PropTypes.string,
};
const defaultProps = {
loading: false,
disabled: false,
className: null,
kind: 'primary',
children: null,
recommended: false,
hasIconOnly: false,
selected: false,
testId: 'Button',
};
const Button = React.forwardRef((props, ref) => {
const {
children,
loading,
disabled,
className,
onClick,
kind,
recommended,
hasIconOnly,
selected,
// TODO: remove deprecated testID prop in v3
testID,
testId,
...other
} = props;
return (
<CarbonButton
{...other}
ref={ref}
// TODO: remove deprecated testID prop in v3
data-testid={testID || testId}
kind={kind === 'icon-selection' ? 'ghost' : kind}
hasIconOnly={kind === 'icon-selection' ? true : hasIconOnly}
onClick={onClick}
className={classnames(className, `${iotPrefix}--btn`, {
[`${iotPrefix}--btn-icon-selection`]: kind === 'icon-selection',
[`${iotPrefix}--btn-icon-selection--recommended`]:
kind === 'icon-selection' && !disabled && recommended,
[`${iotPrefix}--btn-icon-selection--selected`]: kind === 'icon-selection' && selected,
})}
disabled={disabled || (loading !== undefined && loading !== false)}
>
{loading ? <Loading small withOverlay={false} /> : null}
{kind === 'icon-selection' && !disabled && recommended ? (
<div className={`${iotPrefix}--btn-icon-selection--recommended_marker`} />
) : null}
{children}
</CarbonButton>
);
});
Button.propTypes = propTypes;
Button.defaultProps = defaultProps;
export default Button;