Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(Statistic): use React.forwardRef() #4245

Merged
merged 1 commit into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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