Skip to content

Commit 7c2bfee

Browse files
layershifterlevithomason
authored andcommitted
style(Button): update typings and propTypes usage (#1216)
* style(Button): update typings and propTypes usage * style(typings): fix colors type
1 parent 5e190fe commit 7c2bfee

File tree

8 files changed

+206
-168
lines changed

8 files changed

+206
-168
lines changed

src/elements/Button/Button.js

+57-70
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import _ from 'lodash'
21
import cx from 'classnames'
2+
import _ from 'lodash'
33
import React, { Component, PropTypes } from 'react'
44

55
import {
@@ -16,37 +16,14 @@ import {
1616
} from '../../lib'
1717
import Icon from '../Icon/Icon'
1818
import Label from '../Label/Label'
19-
2019
import ButtonContent from './ButtonContent'
2120
import ButtonGroup from './ButtonGroup'
2221
import ButtonOr from './ButtonOr'
2322

2423
const debug = makeDebugger('button')
2524

26-
const _meta = {
27-
name: 'Button',
28-
type: META.TYPES.ELEMENT,
29-
props: {
30-
animated: ['fade', 'vertical'],
31-
attached: ['left', 'right', 'top', 'bottom'],
32-
color: [
33-
...SUI.COLORS,
34-
'facebook',
35-
'twitter',
36-
'google plus',
37-
'vk',
38-
'linkedin',
39-
'instagram',
40-
'youtube',
41-
],
42-
floated: SUI.FLOATS,
43-
labelPosition: ['right', 'left'],
44-
size: SUI.SIZES,
45-
},
46-
}
47-
4825
/**
49-
* A Button indicates a possible user action
26+
* A Button indicates a possible user action.
5027
* @see Form
5128
* @see Icon
5229
* @see Label
@@ -56,19 +33,19 @@ class Button extends Component {
5633
/** An element type to render as (string or function). */
5734
as: customPropTypes.as,
5835

59-
/** A button can show it is currently the active user selection */
36+
/** A button can show it is currently the active user selection. */
6037
active: PropTypes.bool,
6138

62-
/** A button can animate to show hidden content */
39+
/** A button can animate to show hidden content. */
6340
animated: PropTypes.oneOfType([
6441
PropTypes.bool,
65-
PropTypes.oneOf(_meta.props.animated),
42+
PropTypes.oneOf(['fade', 'vertical']),
6643
]),
6744

68-
/** A button can be attached to the top or bottom of other content */
69-
attached: PropTypes.oneOf(_meta.props.attached),
45+
/** A button can be attached to the top or bottom of other content. */
46+
attached: PropTypes.oneOf(['left', 'right', 'top', 'bottom']),
7047

71-
/** A basic button is less pronounced */
48+
/** A basic button is less pronounced. */
7249
basic: PropTypes.bool,
7350

7451
/** Primary content. */
@@ -87,55 +64,58 @@ class Button extends Component {
8764
),
8865
]),
8966

90-
/** A button can be circular */
91-
circular: PropTypes.bool,
92-
9367
/** Additional classes. */
9468
className: PropTypes.string,
9569

96-
/** Shorthand for primary content. */
97-
content: customPropTypes.contentShorthand,
70+
/** A button can be circular. */
71+
circular: PropTypes.bool,
9872

9973
/** A button can have different colors */
100-
color: PropTypes.oneOf(_meta.props.color),
74+
color: PropTypes.oneOf([
75+
...SUI.COLORS,
76+
'facebook', 'google plus', 'instagram', 'linkedin', 'twitter', 'vk', 'youtube',
77+
]),
10178

102-
/** A button can reduce its padding to fit into tighter spaces */
79+
/** A button can reduce its padding to fit into tighter spaces. */
10380
compact: PropTypes.bool,
10481

105-
/** A button can show it is currently unable to be interacted with */
82+
/** Shorthand for primary content. */
83+
content: customPropTypes.contentShorthand,
84+
85+
/** A button can show it is currently unable to be interacted with. */
10686
disabled: PropTypes.bool,
10787

108-
/** A button can be aligned to the left or right of its container */
109-
floated: PropTypes.oneOf(_meta.props.floated),
88+
/** A button can be aligned to the left or right of its container. */
89+
floated: PropTypes.oneOf(SUI.FLOATS),
11090

111-
/** A button can take the width of its container */
91+
/** A button can take the width of its container. */
11292
fluid: PropTypes.bool,
11393

114-
/** Add an Icon by name, props object, or pass an <Icon /> */
94+
/** Add an Icon by name, props object, or pass an <Icon />. */
11595
icon: customPropTypes.some([
11696
PropTypes.bool,
11797
PropTypes.string,
11898
PropTypes.object,
11999
PropTypes.element,
120100
]),
121101

122-
/** A button can be formatted to appear on dark backgrounds */
102+
/** A button can be formatted to appear on dark backgrounds. */
123103
inverted: PropTypes.bool,
124104

125-
/** A labeled button can format a Label or Icon to appear on the left or right */
126-
labelPosition: PropTypes.oneOf(_meta.props.labelPosition),
127-
128-
/** Add a Label by text, props object, or pass a <Label /> */
105+
/** Add a Label by text, props object, or pass a <Label />. */
129106
label: customPropTypes.some([
130107
PropTypes.string,
131108
PropTypes.object,
132109
PropTypes.element,
133110
]),
134111

135-
/** A button can show a loading indicator */
112+
/** A labeled button can format a Label or Icon to appear on the left or right. */
113+
labelPosition: PropTypes.oneOf(['right', 'left']),
114+
115+
/** A button can show a loading indicator. */
136116
loading: PropTypes.bool,
137117

138-
/** A button can hint towards a negative consequence */
118+
/** A button can hint towards a negative consequence. */
139119
negative: PropTypes.bool,
140120

141121
/**
@@ -145,33 +125,37 @@ class Button extends Component {
145125
*/
146126
onClick: PropTypes.func,
147127

148-
/** A button can hint towards a positive consequence */
128+
/** A button can hint towards a positive consequence. */
149129
positive: PropTypes.bool,
150130

151-
/** A button can be formatted to show different levels of emphasis */
131+
/** A button can be formatted to show different levels of emphasis. */
152132
primary: PropTypes.bool,
153133

154-
/** A button can be formatted to show different levels of emphasis */
134+
/** A button can be formatted to show different levels of emphasis. */
155135
secondary: PropTypes.bool,
156136

157-
/** A button can have different sizes */
158-
size: PropTypes.oneOf(_meta.props.size),
137+
/** A button can have different sizes. */
138+
size: PropTypes.oneOf(SUI.SIZES),
159139

160140
/** A button can receive focus. */
161141
tabIndex: PropTypes.oneOfType([
162-
PropTypes.string,
163142
PropTypes.number,
143+
PropTypes.string,
164144
]),
165145

166-
/** A button can be formatted to toggle on and off */
146+
/** A button can be formatted to toggle on and off. */
167147
toggle: PropTypes.bool,
168148
}
169149

170150
static defaultProps = {
171151
as: 'button',
172152
}
173153

174-
static _meta = _meta
154+
static _meta = {
155+
name: 'Button',
156+
type: META.TYPES.ELEMENT,
157+
}
158+
175159
static Content = ButtonContent
176160
static Group = ButtonGroup
177161
static Or = ButtonOr
@@ -187,6 +171,14 @@ class Button extends Component {
187171
if (onClick) onClick(e, this.props)
188172
}
189173

174+
computeTabIndex = ElementType => {
175+
const { disabled, tabIndex } = this.props
176+
177+
if (!_.isNil(tabIndex)) return tabIndex
178+
if (disabled) return -1
179+
if (ElementType === 'div') return 0
180+
}
181+
190182
render() {
191183
const {
192184
active,
@@ -212,7 +204,6 @@ class Button extends Component {
212204
primary,
213205
secondary,
214206
size,
215-
tabIndex,
216207
toggle,
217208
} = this.props
218209

@@ -224,13 +215,10 @@ class Button extends Component {
224215
color,
225216
size,
226217
useKeyOnly(active, 'active'),
227-
useKeyOrValueAndKey(animated, 'animated'),
228-
useKeyOrValueAndKey(attached, 'attached'),
229218
useKeyOnly(basic, 'basic'),
230219
useKeyOnly(circular, 'circular'),
231220
useKeyOnly(compact, 'compact'),
232221
useKeyOnly(disabled, 'disabled'),
233-
useValueAndKey(floated, 'floated'),
234222
useKeyOnly(fluid, 'fluid'),
235223
useKeyOnly(icon === true || icon && (labelPosition || !children && !content), 'icon'),
236224
useKeyOnly(inverted, 'inverted'),
@@ -240,22 +228,21 @@ class Button extends Component {
240228
useKeyOnly(primary, 'primary'),
241229
useKeyOnly(secondary, 'secondary'),
242230
useKeyOnly(toggle, 'toggle'),
231+
useKeyOrValueAndKey(animated, 'animated'),
232+
useKeyOrValueAndKey(attached, 'attached'),
233+
useValueAndKey(floated, 'floated'),
243234
)
244235
const rest = getUnhandledProps(Button, this.props)
245236
const ElementType = getElementType(Button, this.props, () => {
246237
if (!_.isNil(label) || !_.isNil(attached)) return 'div'
247238
})
248-
249-
let computedTabIndex
250-
if (!_.isNil(tabIndex)) computedTabIndex = tabIndex
251-
else if (disabled) computedTabIndex = -1
252-
else if (ElementType === 'div') computedTabIndex = 0
239+
const tabIndex = this.computeTabIndex(ElementType)
253240

254241
if (!_.isNil(children)) {
255242
const classes = cx('ui', baseClasses, labeledClasses, 'button', className)
256243
debug('render children:', { classes })
257244
return (
258-
<ElementType {...rest} className={classes} tabIndex={computedTabIndex} onClick={this.handleClick}>
245+
<ElementType {...rest} className={classes} tabIndex={tabIndex} onClick={this.handleClick}>
259246
{children}
260247
</ElementType>
261248
)
@@ -285,7 +272,7 @@ class Button extends Component {
285272
const classes = cx('ui', labeledClasses, baseClasses, 'button', className)
286273
debug('render icon && !label:', { classes })
287274
return (
288-
<ElementType {...rest} className={classes} tabIndex={computedTabIndex} onClick={this.handleClick}>
275+
<ElementType {...rest} className={classes} tabIndex={tabIndex} onClick={this.handleClick}>
289276
{Icon.create(icon)} {content}
290277
</ElementType>
291278
)
@@ -295,7 +282,7 @@ class Button extends Component {
295282
debug('render default:', { classes })
296283

297284
return (
298-
<ElementType {...rest} className={classes} tabIndex={computedTabIndex} onClick={this.handleClick}>
285+
<ElementType {...rest} className={classes} tabIndex={tabIndex} onClick={this.handleClick}>
299286
{content}
300287
</ElementType>
301288
)

src/elements/Button/ButtonContent.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@ import {
1010
} from '../../lib'
1111

1212
/**
13-
* Used in some Button types, such as `animated`
13+
* Used in some Button types, such as `animated`.
1414
*/
1515
function ButtonContent(props) {
16-
const { children, className, hidden, visible } = props
16+
const {
17+
children,
18+
className,
19+
hidden,
20+
visible,
21+
} = props
1722
const classes = cx(
1823
useKeyOnly(visible, 'visible'),
1924
useKeyOnly(hidden, 'hidden'),
@@ -36,16 +41,16 @@ ButtonContent.propTypes = {
3641
/** An element type to render as (string or function). */
3742
as: customPropTypes.as,
3843

39-
/** Additional classes. */
40-
className: PropTypes.string,
41-
4244
/** Primary content. */
4345
children: PropTypes.node,
4446

45-
/** Initially hidden, visible on hover */
47+
/** Additional classes. */
48+
className: PropTypes.string,
49+
50+
/** Initially hidden, visible on hover. */
4651
hidden: PropTypes.bool,
4752

48-
/** Initially visible, hidden on hover */
53+
/** Initially visible, hidden on hover. */
4954
visible: PropTypes.bool,
5055
}
5156

0 commit comments

Comments
 (0)