-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathButton.js
More file actions
138 lines (123 loc) · 4.17 KB
/
Button.js
File metadata and controls
138 lines (123 loc) · 4.17 KB
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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import Icon from './Icon';
import Spinner from './Spinner';
export default class Button extends Component {
constructor() {
super();
this.onClick = this.onClick.bind(this);
}
onClick(e) {
// Safari, FF to trigger focus event on click
this.node.focus();
const { onClick } = this.props;
if (onClick) onClick(e);
}
renderIcon(iconSize, inv) {
const { icon, iconAlign, type } = this.props;
const inverse = inv || /\-?inverse$/.test(type);
return <ButtonIcon icon={ icon } align={ iconAlign } size={ iconSize } inverse={ inverse } />;
}
renderIconMore() {
const { iconMore, icon, iconAlign, label, children } = this.props;
const adjoining = icon && (iconAlign === 'right' || !(label || children));
const iconMoreSize = this.props.iconMoreSize || adjoining ? 'x-small' : 'small';
return <ButtonIcon icon={ iconMore } align='right' size={ iconMoreSize } />;
}
render() {
const {
className, type, size, icon, iconAlign, iconMore, selected, alt, label, loading,
iconSize, inverse, htmlType = 'button', children, buttonRef, ...props
} = this.props;
delete props.inverse;
const typeClassName = type ? `slds-button--${type}` : null;
const btnClassNames = classnames(
className,
'slds-button',
typeClassName,
{
'slds-is-selected': selected,
[`slds-button--${size}`]: size && !/^icon-/.test(type),
[`slds-button--icon-${size}`]: /^(x-small|small)$/.test(size) && /^icon-/.test(type),
}
);
delete props.component;
delete props.items;
return (
<button
ref={(node) => {
this.node = node;
if (buttonRef) buttonRef(node);
}}
className={ btnClassNames }
type={ htmlType }
{ ...props }
onClick={this.onClick}
>
{ icon && iconAlign !== 'right' ? this.renderIcon(iconSize, inverse) : null }
{ children || label }
{ icon && iconAlign === 'right' ? this.renderIcon(iconSize, inverse) : null }
{ iconMore ? this.renderIconMore() : null }
{ alt ? <span className='slds-assistive-text'>{ alt }</span> : null }
{ loading ? <Spinner /> : null }
</button>
);
}
}
export const BUTTON_TYPES = [
'neutral',
'brand',
'destructive',
'inverse',
'icon-bare',
'icon-container',
'icon-inverse',
'icon-more',
'icon-border',
'icon-border-filled',
];
const BUTTON_SIZES = ['x-small', 'small', 'medium', 'large'];
const ICON_SIZES = ['x-small', 'small', 'medium', 'large'];
const ICON_ALIGNS = ['left', 'right'];
Button.propTypes = {
className: PropTypes.string,
label: PropTypes.node,
alt: PropTypes.string,
type: PropTypes.oneOf(BUTTON_TYPES),
size: PropTypes.oneOf(BUTTON_SIZES),
htmlType: PropTypes.string,
selected: PropTypes.bool,
inverse: PropTypes.bool,
loading: PropTypes.bool,
icon: PropTypes.string,
iconSize: PropTypes.oneOf(ICON_SIZES),
iconAlign: PropTypes.oneOf(ICON_ALIGNS),
iconMore: PropTypes.string,
iconMoreSize: PropTypes.oneOf(ICON_SIZES),
children: PropTypes.node,
onClick: PropTypes.func,
buttonRef: PropTypes.func,
};
export const ButtonIcon = ({ icon, align, size, inverse, className, style, ...props }) => {
const alignClassName = ICON_ALIGNS.indexOf(align) >= 0 ? `slds-button__icon--${align}` : null;
const sizeClassName = ICON_SIZES.indexOf(size) >= 0 ? `slds-button__icon--${size}` : null;
const inverseClassName = inverse ? 'slds-button__icon--inverse' : null;
const iconClassNames = classnames('slds-button__icon', alignClassName, sizeClassName,
inverseClassName, className);
const iconStyle = { ...style, pointerEvents: 'none' };
return (
<Icon
className={ iconClassNames } icon={ icon } textColor={ null } style={ iconStyle }
{ ...props }
/>
);
};
ButtonIcon.propTypes = {
className: PropTypes.string,
icon: PropTypes.string,
align: PropTypes.oneOf(['left', 'right']),
size: PropTypes.oneOf(['x-small', 'small', 'medium', 'large']),
inverse: PropTypes.bool,
style: PropTypes.object, // eslint-disable-line react/forbid-prop-types
};