Skip to content

Commit

Permalink
chore(Statistic): use React.forwardRef() (#4245)
Browse files Browse the repository at this point in the history
  • Loading branch information
layershifter committed Jun 21, 2022
1 parent be3c0b7 commit f4d40f0
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 16 deletions.
11 changes: 6 additions & 5 deletions src/views/Statistic/Statistic.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import StatisticValue from './StatisticValue'
/**
* A statistic emphasizes the current value of an attribute.
*/
function Statistic(props) {
const Statistic = React.forwardRef(function (props, ref) {
const {
children,
className,
Expand Down Expand Up @@ -50,30 +50,31 @@ function Statistic(props) {

if (!childrenUtils.isNil(children)) {
return (
<ElementType {...rest} className={classes}>
<ElementType {...rest} className={classes} ref={ref}>
{children}
</ElementType>
)
}
if (!childrenUtils.isNil(content)) {
return (
<ElementType {...rest} className={classes}>
<ElementType {...rest} className={classes} ref={ref}>
{content}
</ElementType>
)
}

return (
<ElementType {...rest} className={classes}>
<ElementType {...rest} className={classes} ref={ref}>
{StatisticValue.create(value, {
defaultProps: { text },
autoGenerateKey: false,
})}
{StatisticLabel.create(label, { autoGenerateKey: false })}
</ElementType>
)
}
})

Statistic.displayName = 'Statistic'
Statistic.propTypes = {
/** An element type to render as (string or function). */
as: PropTypes.elementType,
Expand Down
11 changes: 6 additions & 5 deletions src/views/Statistic/StatisticGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import Statistic from './Statistic'
/**
* A group of statistics.
*/
function StatisticGroup(props) {
const StatisticGroup = React.forwardRef(function (props, ref) {
const { children, className, color, content, horizontal, inverted, items, size, widths } = props

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

if (!childrenUtils.isNil(children)) {
return (
<ElementType {...rest} className={classes}>
<ElementType {...rest} className={classes} ref={ref}>
{children}
</ElementType>
)
}
if (!childrenUtils.isNil(content)) {
return (
<ElementType {...rest} className={classes}>
<ElementType {...rest} className={classes} ref={ref}>
{content}
</ElementType>
)
}

return (
<ElementType {...rest} className={classes}>
<ElementType {...rest} className={classes} ref={ref}>
{_.map(items, (item) => Statistic.create(item))}
</ElementType>
)
}
})

StatisticGroup.displayName = 'StatisticGroup'
StatisticGroup.propTypes = {
/** An element type to render as (string or function). */
as: PropTypes.elementType,
Expand Down
7 changes: 4 additions & 3 deletions src/views/Statistic/StatisticLabel.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@ import {
/**
* A statistic can contain a label to help provide context for the presented value.
*/
function StatisticLabel(props) {
const StatisticLabel = React.forwardRef(function (props, ref) {
const { children, className, content } = props
const classes = cx('label', className)
const rest = getUnhandledProps(StatisticLabel, props)
const ElementType = getElementType(StatisticLabel, props)

return (
<ElementType {...rest} className={classes}>
<ElementType {...rest} className={classes} ref={ref}>
{childrenUtils.isNil(children) ? content : children}
</ElementType>
)
}
})

StatisticLabel.displayName = 'StatisticLabel'
StatisticLabel.propTypes = {
/** An element type to render as (string or function). */
as: PropTypes.elementType,
Expand Down
7 changes: 4 additions & 3 deletions src/views/Statistic/StatisticValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,21 @@ import {
/**
* A statistic can contain a numeric, icon, image, or text value.
*/
function StatisticValue(props) {
const StatisticValue = React.forwardRef(function (props, ref) {
const { children, className, content, text } = props

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

return (
<ElementType {...rest} className={classes}>
<ElementType {...rest} className={classes} ref={ref}>
{childrenUtils.isNil(children) ? content : children}
</ElementType>
)
}
})

StatisticValue.displayName = 'StatisticValue'
StatisticValue.propTypes = {
/** An element type to render as (string or function). */
as: PropTypes.elementType,
Expand Down
4 changes: 4 additions & 0 deletions test/specs/views/Stastistic/Statistic-test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import faker from 'faker'
import _ from 'lodash'
import React from 'react'

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

describe('Statistic', () => {
common.isConformant(Statistic)
common.forwardsRef(Statistic)
common.forwardsRef(Statistic, { requiredProps: { children: <span /> } })
common.forwardsRef(Statistic, { requiredProps: { content: faker.lorem.word() } })
common.implementsCreateMethod(Statistic)
common.hasSubcomponents(Statistic, [StatisticGroup, StatisticLabel, StatisticValue])
common.hasUIClassName(Statistic)
Expand Down
4 changes: 4 additions & 0 deletions test/specs/views/Stastistic/StatisticGroup-test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import faker from 'faker'
import _ from 'lodash'
import React from 'react'

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

describe('StatisticGroup', () => {
common.isConformant(StatisticGroup)
common.forwardsRef(StatisticGroup)
common.forwardsRef(StatisticGroup, { requiredProps: { children: <span /> } })
common.forwardsRef(StatisticGroup, { requiredProps: { content: faker.lorem.word() } })
common.hasUIClassName(StatisticGroup)
common.rendersChildren(StatisticGroup)

Expand Down
1 change: 1 addition & 0 deletions test/specs/views/Stastistic/StatisticLabel-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as common from 'test/specs/commonTests'

describe('StatisticLabel', () => {
common.isConformant(StatisticLabel)
common.forwardsRef(StatisticLabel)
common.implementsCreateMethod(StatisticLabel)
common.rendersChildren(StatisticLabel)
})
1 change: 1 addition & 0 deletions test/specs/views/Stastistic/StatisticValue-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as common from 'test/specs/commonTests'

describe('StatisticValue', () => {
common.isConformant(StatisticValue)
common.forwardsRef(StatisticValue)
common.implementsCreateMethod(StatisticValue)
common.rendersChildren(StatisticValue)

Expand Down

0 comments on commit f4d40f0

Please sign in to comment.