/
index.js
300 lines (274 loc) · 8.36 KB
/
index.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
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
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 - present Instructure, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import classnames from 'classnames'
import keycode from 'keycode'
import { testable } from '@instructure/ui-testable'
import { themeable, ThemeablePropTypes } from '@instructure/ui-themeable'
import { omitProps, getElementType } from '@instructure/ui-react-utils'
import { isActiveElement, findDOMNode } from '@instructure/ui-dom-utils'
import { hasVisibleChildren } from '@instructure/ui-a11y-utils'
import { warn } from '@instructure/console/macro'
import { Flex } from '@instructure/ui-flex'
import { View } from '@instructure/ui-view'
import styles from './styles.css'
import theme from './theme'
const circleVariants = [
'circle-primary',
'circle-danger',
'circle-default'
]
const squareVariants = [
'circle-default',
'circle-primary',
'circle-danger',
'icon',
'icon-inverse'
]
/**
---
category: components
---
**/
@testable()
@themeable(theme, styles)
class Button extends Component {
static propTypes = {
children: PropTypes.node.isRequired,
type: PropTypes.oneOf(['button', 'submit', 'reset']),
variant: PropTypes.oneOf([
'default',
'primary',
'success',
'danger',
'light',
'ghost',
'ghost-inverse',
'link',
'link-inverse',
'circle-default',
'circle-primary',
'circle-danger',
'icon',
'icon-inverse'
]),
/**
* provides a reference to the underlying focusable (`button` or `a`) element
*/
buttonRef: PropTypes.func,
/**
* the element type to render as (will be `<a>` if href is provided)
*/
as: PropTypes.elementType,
size: PropTypes.oneOf(['small', 'medium', 'large']),
/**
* should the `<Button/>` fill the width of its container
*/
fluidWidth: PropTypes.bool,
disabled: PropTypes.bool,
readOnly: PropTypes.bool,
href: PropTypes.string,
onClick: PropTypes.func,
/**
* Valid values are `0`, `none`, `auto`, `xxx-small`, `xx-small`, `x-small`,
* `small`, `medium`, `large`, `x-large`, `xx-large`. Apply these values via
* familiar CSS-like shorthand. For example: `margin="small auto large"`.
*/
margin: ThemeablePropTypes.spacing,
/**
* Add an SVG icon to the button. Do not add icons directly as
* children.
*/
icon: PropTypes.oneOfType([PropTypes.func, PropTypes.element]),
/**
* Specify a mouse cursor to use when hovering over the button.
* The `pointer` cursor is used by default.
*/
cursor: PropTypes.string,
tabIndex: PropTypes.oneOfType([PropTypes.number, PropTypes.string])
}
static defaultProps = {
as: 'button',
type: 'button',
variant: 'default',
size: 'medium',
margin: '0',
fluidWidth: false,
buttonRef: function (button) {},
cursor: 'pointer',
disabled: undefined,
onClick: undefined,
icon: undefined,
href: undefined,
readOnly: undefined,
tabIndex: undefined
}
handleClick = e => {
const { disabled, readOnly, onClick } = this.props
if (disabled || readOnly) {
e.preventDefault()
e.stopPropagation()
} else if (typeof onClick === 'function') {
onClick(e)
}
}
handleKeyDown = e => {
const { disabled, readOnly, onClick, href } = this.props
// behave like a button when space key is pressed
if (this.elementType !== 'button' && (e.keyCode === keycode.codes.space || e.keyCode === keycode.codes.enter)) {
e.preventDefault()
e.stopPropagation()
if ((typeof onClick === 'function') && !disabled && !readOnly) {
onClick(e)
}
if (href) {
findDOMNode(this._button).click() // eslint-disable-line react/no-find-dom-node
}
}
}
get elementType () {
return getElementType(Button, this.props)
}
get focused () {
return isActiveElement(this._button)
}
focus () {
findDOMNode(this._button).focus() // eslint-disable-line react/no-find-dom-node
}
renderIcon () {
const Icon = this.props.icon
if (typeof this.props.icon === 'function') {
return <span className={styles.iconSVG}><Icon inline={false} /></span>
} else if (Icon) {
return <span className={styles.iconSVG}>{Icon}</span>
} else {
return null
}
}
buttonBorderRadius () {
if (circleVariants.includes(this.props.variant)) {
return 'circle'
} else {
return 'rounded'
}
}
buttonWidth (hasOnlyIcon) {
const {
variant,
fluidWidth
} = this.props
if (hasOnlyIcon || squareVariants.includes(variant)) {
return 'icon'
} else if (fluidWidth) {
return 'fluid'
} else {
return 'auto'
}
}
render () {
const {
as,
buttonRef,
children,
cursor,
disabled,
href,
icon,
margin,
onClick,
readOnly,
size,
type,
variant,
tabIndex
} = this.props
const IHaveVisibleChildren = hasVisibleChildren(this.props.children)
const hasTextAndIcon = IHaveVisibleChildren && this.props.icon // any button with an icon + text label
const hasOnlyIcon = !IHaveVisibleChildren && this.props.icon // any button with just an icon visible
if (process.env.NODE_ENV !== 'production') {
// show warning if icon is added as a child
if (this.hasVisibleChildren) {
React.Children.forEach(children, (child) => {
const icon = child && child.type && typeof child.type.glyphName !== 'undefined'
warn(
!icon,
`[Button] Icons as children is deprecated. Please use the 'icon' prop instead.`
)
})
}
}
// warn for unallowed view props
const passthroughProps = View.omitViewProps(
omitProps(this.props, Button.propTypes),
Button
)
return (
<View
{...passthroughProps}
className={classnames({
[styles.root]: true,
[styles[variant]]: true,
[styles[size]]: size,
[styles[`width--${this.buttonWidth(hasOnlyIcon)}`]]: true,
[styles[`borderRadius--${this.buttonBorderRadius()}`]]: true,
[styles.disabled]: disabled,
[styles['has-icon']]: icon
})}
disabled={disabled || readOnly}
onClick={this.handleClick}
onKeyDown={this.handleKeyDown}
href={href}
type={href ? null : type}
role={onClick && as !== 'button' ? 'button' : null}
tabIndex={onClick && as ? (tabIndex || '0') : tabIndex}
elementRef={c => {
this._button = c
if (typeof buttonRef === 'function') buttonRef(c)
}}
display={null}
as={this.elementType}
margin={margin}
cursor={disabled ? 'not-allowed' : cursor}
>
{hasTextAndIcon ? (
<Flex height="100%" width="100%">
<Flex.Item padding="0 x-small 0 0">{this.renderIcon()}</Flex.Item>
<Flex.Item shouldGrow shouldShrink>
<span className={styles.content}>{children}</span>
</Flex.Item>
</Flex>
) : (
// all other button layouts (icon only and text only)
<span className={styles.content}>
{icon && this.renderIcon()}
{children}
</span>
)}
</View>
)
}
}
export default Button
export { Button }