Skip to content

Commit a65236c

Browse files
committed
chore(Statistic): use React.forwardRef() (#4245)
1 parent f5d79fa commit a65236c

File tree

8 files changed

+30
-16
lines changed

8 files changed

+30
-16
lines changed

src/views/Statistic/Statistic.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import StatisticValue from './StatisticValue'
2020
/**
2121
* A statistic emphasizes the current value of an attribute.
2222
*/
23-
function Statistic(props) {
23+
const Statistic = React.forwardRef(function (props, ref) {
2424
const {
2525
children,
2626
className,
@@ -50,30 +50,31 @@ function Statistic(props) {
5050

5151
if (!childrenUtils.isNil(children)) {
5252
return (
53-
<ElementType {...rest} className={classes}>
53+
<ElementType {...rest} className={classes} ref={ref}>
5454
{children}
5555
</ElementType>
5656
)
5757
}
5858
if (!childrenUtils.isNil(content)) {
5959
return (
60-
<ElementType {...rest} className={classes}>
60+
<ElementType {...rest} className={classes} ref={ref}>
6161
{content}
6262
</ElementType>
6363
)
6464
}
6565

6666
return (
67-
<ElementType {...rest} className={classes}>
67+
<ElementType {...rest} className={classes} ref={ref}>
6868
{StatisticValue.create(value, {
6969
defaultProps: { text },
7070
autoGenerateKey: false,
7171
})}
7272
{StatisticLabel.create(label, { autoGenerateKey: false })}
7373
</ElementType>
7474
)
75-
}
75+
})
7676

77+
Statistic.displayName = 'Statistic'
7778
Statistic.propTypes = {
7879
/** An element type to render as (string or function). */
7980
as: PropTypes.elementType,

src/views/Statistic/StatisticGroup.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Statistic from './Statistic'
1717
/**
1818
* A group of statistics.
1919
*/
20-
function StatisticGroup(props) {
20+
const StatisticGroup = React.forwardRef(function (props, ref) {
2121
const { children, className, color, content, horizontal, inverted, items, size, widths } = props
2222

2323
const classes = cx(
@@ -35,26 +35,27 @@ function StatisticGroup(props) {
3535

3636
if (!childrenUtils.isNil(children)) {
3737
return (
38-
<ElementType {...rest} className={classes}>
38+
<ElementType {...rest} className={classes} ref={ref}>
3939
{children}
4040
</ElementType>
4141
)
4242
}
4343
if (!childrenUtils.isNil(content)) {
4444
return (
45-
<ElementType {...rest} className={classes}>
45+
<ElementType {...rest} className={classes} ref={ref}>
4646
{content}
4747
</ElementType>
4848
)
4949
}
5050

5151
return (
52-
<ElementType {...rest} className={classes}>
52+
<ElementType {...rest} className={classes} ref={ref}>
5353
{_.map(items, (item) => Statistic.create(item))}
5454
</ElementType>
5555
)
56-
}
56+
})
5757

58+
StatisticGroup.displayName = 'StatisticGroup'
5859
StatisticGroup.propTypes = {
5960
/** An element type to render as (string or function). */
6061
as: PropTypes.elementType,

src/views/Statistic/StatisticLabel.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,20 @@ import {
1313
/**
1414
* A statistic can contain a label to help provide context for the presented value.
1515
*/
16-
function StatisticLabel(props) {
16+
const StatisticLabel = React.forwardRef(function (props, ref) {
1717
const { children, className, content } = props
1818
const classes = cx('label', className)
1919
const rest = getUnhandledProps(StatisticLabel, props)
2020
const ElementType = getElementType(StatisticLabel, props)
2121

2222
return (
23-
<ElementType {...rest} className={classes}>
23+
<ElementType {...rest} className={classes} ref={ref}>
2424
{childrenUtils.isNil(children) ? content : children}
2525
</ElementType>
2626
)
27-
}
27+
})
2828

29+
StatisticLabel.displayName = 'StatisticLabel'
2930
StatisticLabel.propTypes = {
3031
/** An element type to render as (string or function). */
3132
as: PropTypes.elementType,

src/views/Statistic/StatisticValue.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,21 @@ import {
1414
/**
1515
* A statistic can contain a numeric, icon, image, or text value.
1616
*/
17-
function StatisticValue(props) {
17+
const StatisticValue = React.forwardRef(function (props, ref) {
1818
const { children, className, content, text } = props
1919

2020
const classes = cx(useKeyOnly(text, 'text'), 'value', className)
2121
const rest = getUnhandledProps(StatisticValue, props)
2222
const ElementType = getElementType(StatisticValue, props)
2323

2424
return (
25-
<ElementType {...rest} className={classes}>
25+
<ElementType {...rest} className={classes} ref={ref}>
2626
{childrenUtils.isNil(children) ? content : children}
2727
</ElementType>
2828
)
29-
}
29+
})
3030

31+
StatisticValue.displayName = 'StatisticValue'
3132
StatisticValue.propTypes = {
3233
/** An element type to render as (string or function). */
3334
as: PropTypes.elementType,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import faker from 'faker'
12
import _ from 'lodash'
23
import React from 'react'
34

@@ -10,6 +11,9 @@ import * as common from 'test/specs/commonTests'
1011

1112
describe('Statistic', () => {
1213
common.isConformant(Statistic)
14+
common.forwardsRef(Statistic)
15+
common.forwardsRef(Statistic, { requiredProps: { children: <span /> } })
16+
common.forwardsRef(Statistic, { requiredProps: { content: faker.lorem.word() } })
1317
common.implementsCreateMethod(Statistic)
1418
common.hasSubcomponents(Statistic, [StatisticGroup, StatisticLabel, StatisticValue])
1519
common.hasUIClassName(Statistic)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import faker from 'faker'
12
import _ from 'lodash'
23
import React from 'react'
34

@@ -7,6 +8,9 @@ import * as common from 'test/specs/commonTests'
78

89
describe('StatisticGroup', () => {
910
common.isConformant(StatisticGroup)
11+
common.forwardsRef(StatisticGroup)
12+
common.forwardsRef(StatisticGroup, { requiredProps: { children: <span /> } })
13+
common.forwardsRef(StatisticGroup, { requiredProps: { content: faker.lorem.word() } })
1014
common.hasUIClassName(StatisticGroup)
1115
common.rendersChildren(StatisticGroup)
1216

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as common from 'test/specs/commonTests'
33

44
describe('StatisticLabel', () => {
55
common.isConformant(StatisticLabel)
6+
common.forwardsRef(StatisticLabel)
67
common.implementsCreateMethod(StatisticLabel)
78
common.rendersChildren(StatisticLabel)
89
})

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as common from 'test/specs/commonTests'
33

44
describe('StatisticValue', () => {
55
common.isConformant(StatisticValue)
6+
common.forwardsRef(StatisticValue)
67
common.implementsCreateMethod(StatisticValue)
78
common.rendersChildren(StatisticValue)
89

0 commit comments

Comments
 (0)