-
Notifications
You must be signed in to change notification settings - Fork 429
/
Copy pathindex.jsx
337 lines (311 loc) · 8.85 KB
/
index.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* Copyright (c) 2015-present, salesforce.com, inc. All rights reserved */
/* Licensed under BSD 3-Clause - see LICENSE.txt or git.io/sfdc-license */
// Implements the [Button Stateful design pattern](https://lightningdesignsystem.com/components/buttons/#flavor-stateful) in React.
// Based on SLDS v2.1.1
// ## Dependencies
// ### React
import React from 'react';
import PropTypes from 'prop-types';
// ### classNames
import classNames from 'classnames';
// ### isFunction
import isFunction from 'lodash.isfunction';
// This component's `checkProps` which issues warnings to developers about properties when in development mode (similar to React's built in development tools)
import checkProps from './check-props';
import componentDoc from './component.json';
// ## Children
import ButtonIcon from '../icon/button-icon';
import getAriaProps from '../../utilities/get-aria-props';
import { BUTTON_STATEFUL } from '../../utilities/constants';
const propTypes = {
/**
* Specifies the current state of the button. If set, the button will act as a ['controlled' component](https://facebook.github.io/react/docs/forms.html#controlled-components).
*/
active: PropTypes.bool,
/**
* **Assistive text for accessibility.**
* This object is merged with the default props object on every render.
* * `icon`: Text that is visually hidden but read aloud by screenreaders to tell the user what the icon means. This should also include the state of the button. If the button has an icon and a visible label, you can omit the <code>icon</code> prop and use the <code>label</code> prop.
*/
assistiveText: PropTypes.shape({
icon: PropTypes.string,
}),
/**
* Disables the button and adds disabled styling.
*/
disabled: PropTypes.bool,
/**
* Icon associated with the stateful button. Accepts an `Icon` component
*/
icon: PropTypes.node,
/**
* Triggered when focus is removed.
*/
onBlur: PropTypes.func,
/**
* Triggered when the button is clicked.
*/
onClick: PropTypes.func,
/**
* Triggered when component is focused.
*/
onFocus: PropTypes.func,
/**
* Triggered when a key is pressed down
*/
onKeyDown: PropTypes.func,
/**
* Triggered when a key is pressed and released
*/
onKeyPress: PropTypes.func,
/**
* Triggered when a key is released
*/
onKeyUp: PropTypes.func,
/**
* Triggered when a mouse button is pressed down
*/
onMouseDown: PropTypes.func,
/**
* Triggered when a mouse arrow hovers
*/
onMouseEnter: PropTypes.func,
/**
* If true, button scales to 100% width on small form factors.
*/
responsive: PropTypes.bool,
/**
* Initial label and icon (optional) of button.
*/
stateOne: PropTypes.object,
/**
* Selected label and icon (optional) of button.
*/
stateTwo: PropTypes.object,
/**
* Deselect label and icon (optional) of button.
*/
stateThree: PropTypes.object,
/**
* Write "-1" if you don't want the user to tab to the button.
*/
tabIndex: PropTypes.string,
/**
* Different types of buttons
*/
variant: PropTypes.oneOf([
'base',
'neutral',
'brand',
'destructive',
'icon',
'icon-filled',
]),
};
// i18n
const defaultProps = {
assistiveText: { icon: '' },
disabled: false,
iconSize: 'medium',
responsive: false,
stateOne: { iconName: 'add', label: 'Follow' },
stateTwo: { iconName: 'check', label: 'Following' },
stateThree: { iconName: 'close', label: 'Unfollow' },
};
/**
* The ButtonStateful component is a variant of the Lightning Design System Button component. It is used for buttons that have a state of unselected or selected.
* For icon buttons, use <code>variant='icon'</code>. For buttons with labels or buttons with labels and icons, pass data to the state props (ie. <code>stateOne={{iconName: 'add', label: 'Join'}}</code>).
* Although not listed in the prop table, all `aria-*` props will be added to the button element if passed in.
* If no `aria-*` props are passed in, <code>aria-live='polite'</code> is used for `icon` and `icon-filled` variants,
* and <code>aria-live='assertive'</code> is used for the remaining variants.
*/
class ButtonStateful extends React.Component {
constructor(props) {
super(props);
this.state = { active: false };
checkProps(BUTTON_STATEFUL, props, componentDoc);
}
getClassName(active) {
return classNames(this.props.className, 'slds-button', {
'slds-button_neutral':
this.props.variant !== 'icon' && this.props.variant !== 'icon-filled',
'slds-button_inverse': this.props.variant === 'inverse',
'slds-not-selected': !active,
'slds-is-selected': active,
'slds-max-small-button_stretch': this.props.responsive,
'slds-button_icon-border': this.props.variant === 'icon',
'slds-button_icon-border-filled': this.props.variant === 'icon-filled',
});
}
handleBlur = (e) => {
if (this.props.onBlur) this.props.onBlur(e);
e.currentTarget.blur();
};
handleClick = (e) => {
if (isFunction(this.props.onClick)) this.props.onClick(e);
if (typeof this.props.active !== 'boolean') {
this.setState((prevState) => ({ active: !prevState.active }));
}
};
render() {
const {
active,
disabled,
icon,
iconName,
iconSize,
id,
onFocus,
onKeyDown,
onKeyPress,
onKeyUp,
onMouseDown,
onMouseEnter,
// onMouseLeave, // This prop isn't used anywhere! But removing it would be a breaking change
stateOne,
stateTwo,
stateThree,
tabIndex,
variant,
} = this.props;
const defaultIconProps = {
disabled,
size: 'small',
className: 'slds-button__icon_stateful',
};
const iconAssistiveText =
typeof this.props.assistiveText === 'string'
? this.props.assistiveText
: {
...defaultProps.assistiveText,
...this.props.assistiveText,
}.icon;
const isActive = typeof active === 'boolean' ? active : this.state.active;
// Accept aria-* props
let ariaProps = getAriaProps(this.props);
if (variant === 'icon' || variant === 'icon-filled') {
// Default aria attribute for stateful button with icon, if none is specified
if (Object.keys(ariaProps).length === 0) {
ariaProps = { 'aria-live': 'polite' };
}
return (
<button
{...ariaProps}
className={this.getClassName(isActive)}
disabled={disabled}
id={id}
onBlur={this.handleBlur}
onClick={this.handleClick}
onFocus={onFocus}
onKeyDown={onKeyDown}
onKeyPress={onKeyPress}
onKeyUp={onKeyUp}
onMouseDown={onMouseDown}
onMouseEnter={onMouseEnter}
onMouseLeave={this.handleBlur}
tabIndex={tabIndex}
type="button"
>
{icon ? (
React.cloneElement(icon, {
...defaultIconProps,
...icon.props,
})
) : (
<ButtonIcon
disabled={disabled}
name={iconName}
size={iconSize}
className="slds-button__icon_stateful"
/>
)}
{iconAssistiveText ? (
<span className="slds-assistive-text">{iconAssistiveText}</span>
) : null}
</button>
);
}
defaultIconProps.position = 'left';
// Default aria attribute for stateful button, if none is specified
if (Object.keys(ariaProps).length === 0) {
ariaProps = { 'aria-live': 'assertive' };
}
return (
<button
{...ariaProps}
className={this.getClassName(isActive)}
disabled={disabled}
id={id}
onBlur={this.handleBlur}
onClick={this.handleClick}
onFocus={onFocus}
onKeyDown={onKeyDown}
onKeyPress={onKeyPress}
onKeyUp={onKeyUp}
onMouseEnter={onMouseEnter}
onMouseLeave={this.handleBlur}
tabIndex={tabIndex}
type="button"
>
<span className="slds-text-not-selected">
{stateOne.icon ? (
React.cloneElement(stateOne.icon, {
...defaultIconProps,
...stateOne.icon.props,
size: 'small',
})
) : (
<ButtonIcon
disabled={disabled}
name={stateOne.iconName}
size="small"
position="left"
className="slds-button__icon_stateful"
/>
)}
{stateOne.label}
</span>
<span className="slds-text-selected">
{stateTwo.icon ? (
React.cloneElement(stateTwo.icon, {
...defaultIconProps,
...stateTwo.icon.props,
size: 'small',
})
) : (
<ButtonIcon
disabled={disabled}
name={stateTwo.iconName}
size="small"
position="left"
className="slds-button__icon_stateful"
/>
)}
{stateTwo.label}
</span>
<span className="slds-text-selected-focus">
{stateThree.icon ? (
React.cloneElement(stateThree.icon, {
...defaultIconProps,
...stateThree.icon.props,
size: 'small',
})
) : (
<ButtonIcon
disabled={disabled}
name={stateThree.iconName}
size="small"
position="left"
className="slds-button__icon_stateful"
/>
)}
{stateThree.label}
</span>
</button>
);
}
}
ButtonStateful.displayName = BUTTON_STATEFUL;
ButtonStateful.propTypes = propTypes;
ButtonStateful.defaultProps = defaultProps;
export default ButtonStateful;