Skip to content

Commit 6b47f0b

Browse files
layershifterharel
authored andcommitted
style(Statistic): update typings and propTypes usage (Semantic-Org#1232)
* style(Statistic): update typings and propTypes usage * style(Statistic): update typings and propTypes usage
1 parent 45f5157 commit 6b47f0b

File tree

7 files changed

+97
-49
lines changed

7 files changed

+97
-49
lines changed

src/views/Statistic/Statistic.js

+21-16
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, { PropTypes } from 'react'
44

55
import {
@@ -16,26 +16,36 @@ import StatisticLabel from './StatisticLabel'
1616
import StatisticValue from './StatisticValue'
1717

1818
/**
19-
* A statistic emphasizes the current value of an attribute
19+
* A statistic emphasizes the current value of an attribute.
2020
*/
2121
function Statistic(props) {
22-
const { children, className, color, floated, horizontal, inverted, label, size, text, value } = props
22+
const {
23+
children,
24+
className,
25+
color,
26+
floated,
27+
horizontal,
28+
inverted,
29+
label,
30+
size,
31+
text,
32+
value,
33+
} = props
34+
2335
const classes = cx(
2436
'ui',
2537
color,
38+
size,
2639
useValueAndKey(floated, 'floated'),
2740
useKeyOnly(horizontal, 'horizontal'),
2841
useKeyOnly(inverted, 'inverted'),
29-
size,
30-
className,
3142
'statistic',
43+
className,
3244
)
3345
const rest = getUnhandledProps(Statistic, props)
3446
const ElementType = getElementType(Statistic, props)
3547

36-
if (!_.isNil(children)) {
37-
return <ElementType {...rest} className={classes}>{children}</ElementType>
38-
}
48+
if (!_.isNil(children)) return <ElementType {...rest} className={classes}>{children}</ElementType>
3949

4050
return (
4151
<ElementType {...rest} className={classes}>
@@ -48,11 +58,6 @@ function Statistic(props) {
4858
Statistic._meta = {
4959
name: 'Statistic',
5060
type: META.TYPES.VIEW,
51-
props: {
52-
color: SUI.COLORS,
53-
floated: SUI.FLOATS,
54-
size: _.without(SUI.SIZES, 'big', 'massive', 'medium'),
55-
},
5661
}
5762

5863
Statistic.propTypes = {
@@ -66,10 +71,10 @@ Statistic.propTypes = {
6671
className: PropTypes.string,
6772

6873
/** A statistic can be formatted to be different colors. */
69-
color: PropTypes.oneOf(Statistic._meta.props.color),
74+
color: PropTypes.oneOf(SUI.COLORS),
7075

7176
/** A statistic can sit to the left or right of other content. */
72-
floated: PropTypes.oneOf(Statistic._meta.props.floated),
77+
floated: PropTypes.oneOf(SUI.FLOATS),
7378

7479
/** A statistic can present its measurement horizontally. */
7580
horizontal: PropTypes.bool,
@@ -81,7 +86,7 @@ Statistic.propTypes = {
8186
label: customPropTypes.contentShorthand,
8287

8388
/** A statistic can vary in size. */
84-
size: PropTypes.oneOf(Statistic._meta.props.size),
89+
size: PropTypes.oneOf(_.without(SUI.SIZES, 'big', 'massive', 'medium')),
8590

8691
/** Format the StatisticValue with smaller font size to fit nicely beside number values. */
8792
text: PropTypes.bool,

src/views/Statistic/StatisticGroup.js

+19-13
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,35 @@ import {
1313
} from '../../lib'
1414
import Statistic from './Statistic'
1515

16+
/**
17+
* A group of statistics.
18+
*/
1619
function StatisticGroup(props) {
17-
const { children, className, color, horizontal, inverted, items, size, widths } = props
20+
const {
21+
children,
22+
className,
23+
color,
24+
horizontal,
25+
inverted,
26+
items,
27+
size,
28+
widths,
29+
} = props
30+
1831
const classes = cx(
1932
'ui',
2033
color,
34+
size,
2135
useKeyOnly(horizontal, 'horizontal'),
2236
useKeyOnly(inverted, 'inverted'),
2337
useWidthProp(widths),
24-
size,
2538
'statistics',
2639
className,
2740
)
2841
const rest = getUnhandledProps(StatisticGroup, props)
2942
const ElementType = getElementType(StatisticGroup, props)
3043

31-
if (!_.isNil(children)) {
32-
return <ElementType {...rest} className={classes}>{children}</ElementType>
33-
}
44+
if (!_.isNil(children)) return <ElementType {...rest} className={classes}>{children}</ElementType>
3445

3546
const itemsJSX = _.map(items, item => (
3647
<Statistic key={item.childKey || [item.label, item.title].join('-')} {...item} />
@@ -43,11 +54,6 @@ StatisticGroup._meta = {
4354
name: 'StatisticGroup',
4455
type: META.TYPES.VIEW,
4556
parent: 'Statistic',
46-
props: {
47-
color: SUI.COLORS,
48-
size: _.without(SUI.SIZES, 'big', 'massive', 'medium'),
49-
widths: SUI.WIDTHS,
50-
},
5157
}
5258

5359
StatisticGroup.propTypes = {
@@ -61,7 +67,7 @@ StatisticGroup.propTypes = {
6167
className: PropTypes.string,
6268

6369
/** A statistic group can be formatted to be different colors. */
64-
color: PropTypes.oneOf(StatisticGroup._meta.props.color),
70+
color: PropTypes.oneOf(SUI.COLORS),
6571

6672
/** A statistic group can present its measurement horizontally. */
6773
horizontal: PropTypes.bool,
@@ -73,10 +79,10 @@ StatisticGroup.propTypes = {
7379
items: customPropTypes.collectionShorthand,
7480

7581
/** A statistic group can vary in size. */
76-
size: PropTypes.oneOf(StatisticGroup._meta.props.size),
82+
size: PropTypes.oneOf(_.without(SUI.SIZES, 'big', 'massive', 'medium')),
7783

7884
/** A statistic group can have its items divided evenly. */
79-
widths: PropTypes.oneOf(StatisticGroup._meta.props.widths),
85+
widths: PropTypes.oneOf(SUI.WIDTHS),
8086
}
8187

8288
export default StatisticGroup

src/views/Statistic/StatisticLabel.js

+10-3
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, { PropTypes } from 'react'
44

55
import {
@@ -9,13 +9,20 @@ import {
99
META,
1010
} from '../../lib'
1111

12+
/**
13+
* A statistic can contain a label to help provide context for the presented value.
14+
*/
1215
function StatisticLabel(props) {
1316
const { children, className, label } = props
14-
const classes = cx(className, 'label')
17+
const classes = cx('label', className)
1518
const rest = getUnhandledProps(StatisticLabel, props)
1619
const ElementType = getElementType(StatisticLabel, props)
1720

18-
return <ElementType {...rest} className={classes}>{_.isNil(children) ? label : children}</ElementType>
21+
return (
22+
<ElementType {...rest} className={classes}>
23+
{_.isNil(children) ? label : children}
24+
</ElementType>
25+
)
1926
}
2027

2128
StatisticLabel._meta = {

src/views/Statistic/StatisticValue.js

+21-4
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, { PropTypes } from 'react'
44

55
import {
@@ -10,13 +10,30 @@ import {
1010
useKeyOnly,
1111
} from '../../lib'
1212

13+
/**
14+
* A statistic can contain a numeric, icon, image, or text value.
15+
*/
1316
function StatisticValue(props) {
14-
const { children, className, text, value } = props
15-
const classes = cx(useKeyOnly(text, 'text'), className, 'value')
17+
const {
18+
children,
19+
className,
20+
text,
21+
value,
22+
} = props
23+
24+
const classes = cx(
25+
useKeyOnly(text, 'text'),
26+
'value',
27+
className,
28+
)
1629
const rest = getUnhandledProps(StatisticValue, props)
1730
const ElementType = getElementType(StatisticValue, props)
1831

19-
return <ElementType {...rest} className={classes}>{_.isNil(children) ? value : children}</ElementType>
32+
return (
33+
<ElementType {...rest} className={classes}>
34+
{_.isNil(children) ? value : children}
35+
</ElementType>
36+
)
2037
}
2138

2239
StatisticValue._meta = {

src/views/Statistic/index.d.ts

+23-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
import { Label } from '../../elements/Label';
2-
import { SemanticCOLORS, SemanticFLOATS, SemanticSIZES, SemanticWIDTHS } from '../..';
31
import * as React from 'react';
2+
import {
3+
SemanticCOLORS,
4+
SemanticFLOATS,
5+
SemanticWIDTHS
6+
} from '../..';
47

8+
type StatisticSizeProp = 'mini' | 'tiny' | 'small' | 'large' | 'huge';
59

610
interface StatisticProps {
11+
[key: string]: any;
12+
713
/** An element type to render as (string or function). */
814
as?: any;
915

@@ -29,7 +35,7 @@ interface StatisticProps {
2935
label?: any;
3036

3137
/** A statistic can vary in size. */
32-
size?: SemanticSIZES;
38+
size?: StatisticSizeProp;
3339

3440
/** Format the StatisticValue with smaller font size to fit nicely beside number values. */
3541
text?: boolean;
@@ -38,15 +44,17 @@ interface StatisticProps {
3844
value?: any;
3945
}
4046

41-
interface StatisticClass extends React.ComponentClass<StatisticProps> {
47+
interface StatisticComponent extends React.StatelessComponent<StatisticProps> {
4248
Group: typeof StatisticGroup;
4349
Label: typeof StatisticLabel;
4450
Value: typeof StatisticValue;
4551
}
4652

47-
export const Statistic: StatisticClass;
53+
export const Statistic: StatisticComponent;
4854

4955
interface StatisticGroupProps {
56+
[key: string]: any;
57+
5058
/** An element type to render as (string or function). */
5159
as?: any;
5260

@@ -69,15 +77,17 @@ interface StatisticGroupProps {
6977
items?: any;
7078

7179
/** A statistic group can vary in size. */
72-
size?: SemanticSIZES;
80+
size?: StatisticSizeProp;
7381

7482
/** A statistic group can have its items divided evenly. */
7583
widths?: SemanticWIDTHS;
7684
}
7785

78-
export const StatisticGroup: React.ComponentClass<StatisticGroupProps>;
86+
export const StatisticGroup: React.StatelessComponent<StatisticGroupProps>;
7987

8088
interface StatisticLabelProps {
89+
[key: string]: any;
90+
8191
/** An element type to render as (string or function). */
8292
as?: any;
8393

@@ -88,12 +98,14 @@ interface StatisticLabelProps {
8898
className?: string;
8999

90100
/** Shorthand for primary content. */
91-
label?: any;
101+
label?: React.ReactNode;
92102
}
93103

94-
export const StatisticLabel: React.ComponentClass<StatisticLabelProps>;
104+
export const StatisticLabel: React.StatelessComponent<StatisticLabelProps>;
95105

96106
interface StatisticValueProps {
107+
[key: string]: any;
108+
97109
/** An element type to render as (string or function). */
98110
as?: any;
99111

@@ -107,7 +119,7 @@ interface StatisticValueProps {
107119
text?: boolean;
108120

109121
/** Primary content of the StatisticValue. Mutually exclusive with the children prop. */
110-
value?: any;
122+
value?: React.ReactNode;
111123
}
112124

113-
export const StatisticValue: React.ComponentClass<StatisticValueProps>;
125+
export const StatisticValue: React.StatelessComponent<StatisticValueProps>;

test/specs/views/Stastistic/StatisticLabel-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import faker from 'faker'
22
import React from 'react'
33

4-
import * as common from 'test/specs/commonTests'
54
import StatisticLabel from 'src/views/Statistic/StatisticLabel'
5+
import * as common from 'test/specs/commonTests'
66

77
describe('StatisticLabel', () => {
88
common.isConformant(StatisticLabel)

test/specs/views/Stastistic/StatisticValue-test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import faker from 'faker'
22
import React from 'react'
33

4-
import * as common from 'test/specs/commonTests'
54
import StatisticValue from 'src/views/Statistic/StatisticValue'
5+
import * as common from 'test/specs/commonTests'
66

77
describe('StatisticValue', () => {
88
common.isConformant(StatisticValue)
99
common.rendersChildren(StatisticValue)
10+
1011
common.propKeyOnlyToClassName(StatisticValue, 'text')
1112

1213
it('renders text with label prop', () => {

0 commit comments

Comments
 (0)