Skip to content

Commit e3ce637

Browse files
committed
chore(Comment): use React.forwardRef() (#4242)
* chore(Comment): use React.forwardRef() * fix displayName * fix tests
1 parent 162a2a5 commit e3ce637

18 files changed

+45
-27
lines changed

src/views/Comment/Comment.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,21 @@ import CommentText from './CommentText'
2121
/**
2222
* A comment displays user feedback to site content.
2323
*/
24-
function Comment(props) {
24+
const Comment = React.forwardRef(function (props, ref) {
2525
const { className, children, collapsed, content } = props
2626

2727
const classes = cx(useKeyOnly(collapsed, 'collapsed'), 'comment', className)
2828
const rest = getUnhandledProps(Comment, props)
2929
const ElementType = getElementType(Comment, props)
3030

3131
return (
32-
<ElementType {...rest} className={classes}>
32+
<ElementType {...rest} className={classes} ref={ref}>
3333
{childrenUtils.isNil(children) ? content : children}
3434
</ElementType>
3535
)
36-
}
36+
})
3737

38+
Comment.displayName = 'Comment'
3839
Comment.propTypes = {
3940
/** An element type to render as (string or function). */
4041
as: PropTypes.elementType,

src/views/Comment/CommentAction.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,25 @@ import {
1313
/**
1414
* A comment can contain an action.
1515
*/
16-
function CommentAction(props) {
16+
const CommentAction = React.forwardRef(function (props, ref) {
1717
const { active, className, children, content } = props
1818

1919
const classes = cx(useKeyOnly(active, 'active'), className)
2020
const rest = getUnhandledProps(CommentAction, props)
2121
const ElementType = getElementType(CommentAction, props)
2222

2323
return (
24-
<ElementType {...rest} className={classes}>
24+
<ElementType {...rest} className={classes} ref={ref}>
2525
{childrenUtils.isNil(children) ? content : children}
2626
</ElementType>
2727
)
28-
}
28+
})
2929

3030
CommentAction.defaultProps = {
3131
as: 'a',
3232
}
3333

34+
CommentAction.displayName = 'CommentAction'
3435
CommentAction.propTypes = {
3536
/** An element type to render as (string or function). */
3637
as: PropTypes.elementType,

src/views/Comment/CommentActions.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@ import { childrenUtils, customPropTypes, getElementType, getUnhandledProps } fro
77
/**
88
* A comment can contain an list of actions a user may perform related to this comment.
99
*/
10-
function CommentActions(props) {
10+
const CommentActions = React.forwardRef(function (props, ref) {
1111
const { className, children, content } = props
1212
const classes = cx('actions', className)
1313
const rest = getUnhandledProps(CommentActions, props)
1414
const ElementType = getElementType(CommentActions, props)
1515

1616
return (
17-
<ElementType {...rest} className={classes}>
17+
<ElementType {...rest} className={classes} ref={ref}>
1818
{childrenUtils.isNil(children) ? content : children}
1919
</ElementType>
2020
)
21-
}
21+
})
2222

23+
CommentActions.displayName = 'CommentActions'
2324
CommentActions.propTypes = {
2425
/** An element type to render as (string or function). */
2526
as: PropTypes.elementType,

src/views/Comment/CommentAuthor.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@ import { childrenUtils, customPropTypes, getElementType, getUnhandledProps } fro
77
/**
88
* A comment can contain an author.
99
*/
10-
function CommentAuthor(props) {
10+
const CommentAuthor = React.forwardRef(function (props, ref) {
1111
const { className, children, content } = props
1212
const classes = cx('author', className)
1313
const rest = getUnhandledProps(CommentAuthor, props)
1414
const ElementType = getElementType(CommentAuthor, props)
1515

1616
return (
17-
<ElementType {...rest} className={classes}>
17+
<ElementType {...rest} className={classes} ref={ref}>
1818
{childrenUtils.isNil(children) ? content : children}
1919
</ElementType>
2020
)
21-
}
21+
})
2222

23+
CommentAuthor.displayName = 'CommentAuthor'
2324
CommentAuthor.propTypes = {
2425
/** An element type to render as (string or function). */
2526
as: PropTypes.elementType,

src/views/Comment/CommentAvatar.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
/**
1414
* A comment can contain an image or avatar.
1515
*/
16-
function CommentAvatar(props) {
16+
const CommentAvatar = React.forwardRef(function (props, ref) {
1717
const { className, src } = props
1818

1919
const classes = cx('avatar', className)
@@ -22,12 +22,13 @@ function CommentAvatar(props) {
2222
const ElementType = getElementType(CommentAvatar, props)
2323

2424
return (
25-
<ElementType {...rootProps} className={classes}>
25+
<ElementType {...rootProps} className={classes} ref={ref}>
2626
{createHTMLImage(src, { autoGenerateKey: false, defaultProps: imageProps })}
2727
</ElementType>
2828
)
29-
}
29+
})
3030

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

src/views/Comment/CommentContent.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@ import { childrenUtils, customPropTypes, getElementType, getUnhandledProps } fro
77
/**
88
* A comment can contain content.
99
*/
10-
function CommentContent(props) {
10+
const CommentContent = React.forwardRef(function (props, ref) {
1111
const { className, children, content } = props
1212
const classes = cx(className, 'content')
1313
const rest = getUnhandledProps(CommentContent, props)
1414
const ElementType = getElementType(CommentContent, props)
1515

1616
return (
17-
<ElementType {...rest} className={classes}>
17+
<ElementType {...rest} className={classes} ref={ref}>
1818
{childrenUtils.isNil(children) ? content : children}
1919
</ElementType>
2020
)
21-
}
21+
})
2222

23+
CommentContent.displayName = 'CommentContent'
2324
CommentContent.propTypes = {
2425
/** An element type to render as (string or function). */
2526
as: PropTypes.elementType,

src/views/Comment/CommentGroup.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
/**
1616
* Comments can be grouped.
1717
*/
18-
function CommentGroup(props) {
18+
const CommentGroup = React.forwardRef(function (props, ref) {
1919
const { className, children, collapsed, content, minimal, size, threaded } = props
2020

2121
const classes = cx(
@@ -31,12 +31,13 @@ function CommentGroup(props) {
3131
const ElementType = getElementType(CommentGroup, props)
3232

3333
return (
34-
<ElementType {...rest} className={classes}>
34+
<ElementType {...rest} className={classes} ref={ref}>
3535
{childrenUtils.isNil(children) ? content : children}
3636
</ElementType>
3737
)
38-
}
38+
})
3939

40+
CommentGroup.displayName = 'CommentGroup'
4041
CommentGroup.propTypes = {
4142
/** An element type to render as (string or function). */
4243
as: PropTypes.elementType,

src/views/Comment/CommentMetadata.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@ import { childrenUtils, customPropTypes, getElementType, getUnhandledProps } fro
77
/**
88
* A comment can contain metadata about the comment, an arbitrary amount of metadata may be defined.
99
*/
10-
function CommentMetadata(props) {
10+
const CommentMetadata = React.forwardRef(function (props, ref) {
1111
const { className, children, content } = props
1212
const classes = cx('metadata', className)
1313
const rest = getUnhandledProps(CommentMetadata, props)
1414
const ElementType = getElementType(CommentMetadata, props)
1515

1616
return (
17-
<ElementType {...rest} className={classes}>
17+
<ElementType {...rest} className={classes} ref={ref}>
1818
{childrenUtils.isNil(children) ? content : children}
1919
</ElementType>
2020
)
21-
}
21+
})
2222

23+
CommentMetadata.displayName = 'CommentMetadata'
2324
CommentMetadata.propTypes = {
2425
/** An element type to render as (string or function). */
2526
as: PropTypes.elementType,

src/views/Comment/CommentText.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@ import { childrenUtils, customPropTypes, getElementType, getUnhandledProps } fro
77
/**
88
* A comment can contain text.
99
*/
10-
function CommentText(props) {
10+
const CommentText = React.forwardRef(function (props, ref) {
1111
const { className, children, content } = props
1212
const classes = cx(className, 'text')
1313
const rest = getUnhandledProps(CommentText, props)
1414
const ElementType = getElementType(CommentText, props)
1515

1616
return (
17-
<ElementType {...rest} className={classes}>
17+
<ElementType {...rest} className={classes} ref={ref}>
1818
{childrenUtils.isNil(children) ? content : children}
1919
</ElementType>
2020
)
21-
}
21+
})
2222

23+
CommentText.displayName = 'CommentText'
2324
CommentText.propTypes = {
2425
/** An element type to render as (string or function). */
2526
as: PropTypes.elementType,

test/specs/views/Comment/Comment-test.js

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

1212
describe('Comment', () => {
1313
common.isConformant(Comment)
14+
common.forwardsRef(Comment)
1415
common.hasSubcomponents(Comment, [
1516
CommentAction,
1617
CommentActions,

test/specs/views/Comment/CommentAction-test.js

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

66
describe('CommentAction', () => {
77
common.isConformant(CommentAction)
8+
common.forwardsRef(CommentAction, { tagName: 'a' })
89
common.rendersChildren(CommentAction)
910

1011
it('renders an a element by default', () => {

test/specs/views/Comment/CommentActions-test.js

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

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

test/specs/views/Comment/CommentAuthor-test.js

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

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

test/specs/views/Comment/CommentAvatar-test.js

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

99
describe('CommentAvatar', () => {
1010
common.isConformant(CommentAvatar)
11+
common.forwardsRef(CommentAvatar)
1112

1213
describe('src', () => {
1314
it('passes to the "img" element', () => {

test/specs/views/Comment/CommentContent-test.js

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

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

test/specs/views/Comment/CommentGroup-test.js

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

77
describe('CommentGroup', () => {
88
common.isConformant(CommentGroup)
9+
common.forwardsRef(CommentGroup)
910
common.rendersChildren(CommentGroup)
1011

1112
common.propKeyOnlyToClassName(CommentGroup, 'collapsed')

test/specs/views/Comment/CommentMetadata-test.js

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

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

test/specs/views/Comment/CommentText-test.js

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

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

0 commit comments

Comments
 (0)