forked from folio-org/stripes-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.js
136 lines (125 loc) · 2.97 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
/* eslint-disable react/button-has-type */
import React from 'react';
import className from 'classnames';
import Link from 'react-router-dom/Link';
import PropTypes from 'prop-types';
import css from './Button.css';
import omitProps from '../../util/omitProps';
export const propTypes = {
align: PropTypes.string,
allowAnchorClick: PropTypes.bool,
autoFocus: PropTypes.bool,
bottomMargin0: PropTypes.bool,
buttonClass: PropTypes.string,
buttonRef: PropTypes.oneOfType([
PropTypes.func,
PropTypes.object
]),
buttonStyle: PropTypes.string,
children: PropTypes.oneOfType([
PropTypes.node,
PropTypes.arrayOf(PropTypes.node),
]),
fullWidth: PropTypes.bool,
href: PropTypes.string,
marginBottom0: PropTypes.bool,
onClick: PropTypes.func,
paddingSide0: PropTypes.bool,
role: PropTypes.string,
to: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
type: PropTypes.string,
};
const defaultProps = {
buttonStyle: 'default',
type: 'button',
};
function Button(props) {
function getStyle() {
const buttonBuiltIn = [];
if (/\s/.test(props.buttonStyle)) {
const csslist = props.buttonStyle.split(/\s+/);
csslist.forEach((classname) => { buttonBuiltIn.push(css[classname]); });
} else {
buttonBuiltIn.push(css[props.buttonStyle]);
}
return className(
css.button,
buttonBuiltIn,
{ [`${css.marginBottom0}`]: props.marginBottom0 },
{ [`${css.marginBottom0}`]: props.bottomMargin0 },
{ [`${css.paddingSide0}`]: props.paddingSide0 },
{ [`${css.fullWidth}`]: props.fullWidth },
{ [`${css[`align-${props.align}`]}`]: props.align },
props.buttonClass,
);
}
function handleAnchorClick(e) {
if (e && !props.allowAnchorClick) e.preventDefault();
if (props.onClick) {
props.onClick(e);
}
}
const inputCustom = omitProps(props, [
'buttonClass',
'buttonStyle',
'bottomMargin0',
'marginBottom0',
'paddingSide0',
'align',
'hollow',
'fullWidth',
'bsRole',
'bsClass',
'onClick',
'allowAnchorClick',
'buttonRef',
'type',
'to',
]);
const { children, onClick, type, to, buttonRef } = props;
const sharedProps = {
...inputCustom,
onClick,
className: getStyle(),
ref: buttonRef,
};
// Render a react router link
// if the "to" prop is provided
if (to) {
return (
<Link
to={to}
role="button"
{...sharedProps}
>
{children}
</Link>
);
}
// Render a regular anchor tag
// if the "href" prop is provided
if (props.href) {
return (
<a
role="button"
href={props.href}
{...sharedProps}
onClick={handleAnchorClick}
>
{children}
</a>
);
}
// Render a button element as default
return (
<button
type={type}
{...sharedProps}
>
{children}
</button>
);
}
Button.propTypes = propTypes;
Button.defaultProps = defaultProps;
export default Button;