-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathButton.jsx
117 lines (103 loc) · 3.09 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
108
109
110
111
112
113
114
115
116
117
/**
* @component
*/
import classNames from 'clsx';
import PropTypes from 'prop-types';
import React, { forwardRef } from 'react';
import Icon from '../../react-chayns-icon/component/Icon';
/**
* Buttons initiate actions, can include a title or an icon and come with a set
* of predefined styles.
*/
const Button = forwardRef(
(
{
chooseButton = false,
disabled = false,
children,
className,
icon,
secondary = false,
stopPropagation = false,
onClick,
type = 'button',
...other
},
ref
) => {
const handleClick = (event) => {
if (onClick && !disabled) onClick(event);
if (stopPropagation) event.stopPropagation();
};
return (
<button
/* eslint-disable-next-line react/button-has-type */
type={type}
className={classNames(className, {
button: !chooseButton,
choosebutton: chooseButton,
'button--disabled': disabled,
'button--secondary': secondary,
'button--icon': icon && !chooseButton,
'choosebutton--icon': icon && chooseButton,
})}
onClick={handleClick}
disabled={disabled}
ref={ref}
{...other}
>
{icon && (
<span
className={classNames({
button__icon: !chooseButton,
choosebutton__icon: chooseButton,
})}
>
<Icon icon={icon} />
</span>
)}
{children}
</button>
);
}
);
export default Button;
Button.propTypes = {
/**
* String or components that are rendered inside of the button.
*/
children: PropTypes.node.isRequired,
/**
* Renders the button on the "ChooseButton"-style. Alternatively use the `ChooseButton`-component.
*/
chooseButton: PropTypes.bool,
/**
* Renders the button as disabled and disables click events.
*/
disabled: PropTypes.bool,
/**
* Will be called after the button has been clicked with the event as the first parameter.
*/
onClick: PropTypes.func,
/**
* String of classnames that should be added to the button.
*/
className: PropTypes.string,
/**
* An optional icon that is displayed on the left of the button. Supply a FontAwesome icon like this: "fa fa-plane"
*/
icon: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
/**
* Render the button in a secondary style.
*/
secondary: PropTypes.bool,
/**
* Stop the event propagation on click.
*/
stopPropagation: PropTypes.bool,
/**
* Set the type for the native button HTML element.
*/
type: PropTypes.oneOf(['button', 'submit', 'reset']),
};
Button.displayName = 'Button';