Skip to content

Commit f58a865

Browse files
authored
chore(Advertisement|Breadcrumb): use React.forwardRef() (#4250)
1 parent 635874a commit f58a865

File tree

7 files changed

+40
-32
lines changed

7 files changed

+40
-32
lines changed

src/collections/Breadcrumb/Breadcrumb.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import BreadcrumbSection from './BreadcrumbSection'
1010
/**
1111
* A breadcrumb is used to show hierarchy between content.
1212
*/
13-
function Breadcrumb(props) {
13+
const Breadcrumb = React.forwardRef(function (props, ref) {
1414
const { children, className, divider, icon, sections, size } = props
1515

1616
const classes = cx('ui', size, 'breadcrumb', className)
@@ -19,7 +19,7 @@ function Breadcrumb(props) {
1919

2020
if (!childrenUtils.isNil(children)) {
2121
return (
22-
<ElementType {...rest} className={classes}>
22+
<ElementType {...rest} className={classes} ref={ref}>
2323
{children}
2424
</ElementType>
2525
)
@@ -40,12 +40,13 @@ function Breadcrumb(props) {
4040
})
4141

4242
return (
43-
<ElementType {...rest} className={classes}>
43+
<ElementType {...rest} className={classes} ref={ref}>
4444
{childElements}
4545
</ElementType>
4646
)
47-
}
47+
})
4848

49+
Breadcrumb.displayName = 'Breadcrumb'
4950
Breadcrumb.propTypes = {
5051
/** An element type to render as (string or function). */
5152
as: PropTypes.elementType,

src/collections/Breadcrumb/BreadcrumbDivider.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Icon from '../../elements/Icon'
1515
/**
1616
* A divider sub-component for Breadcrumb component.
1717
*/
18-
function BreadcrumbDivider(props) {
18+
const BreadcrumbDivider = React.forwardRef(function (props, ref) {
1919
const { children, className, content, icon } = props
2020

2121
const classes = cx('divider', className)
@@ -26,24 +26,26 @@ function BreadcrumbDivider(props) {
2626
return Icon.create(icon, {
2727
defaultProps: { ...rest, className: classes },
2828
autoGenerateKey: false,
29+
ref,
2930
})
3031
}
3132

3233
if (!_.isNil(content)) {
3334
return (
34-
<ElementType {...rest} className={classes}>
35+
<ElementType {...rest} className={classes} ref={ref}>
3536
{content}
3637
</ElementType>
3738
)
3839
}
3940

4041
return (
41-
<ElementType {...rest} className={classes}>
42+
<ElementType {...rest} className={classes} ref={ref}>
4243
{childrenUtils.isNil(children) ? '/' : children}
4344
</ElementType>
4445
)
45-
}
46+
})
4647

48+
BreadcrumbDivider.displayName = 'BreadcrumbDivider'
4749
BreadcrumbDivider.propTypes = {
4850
/** An element type to render as (string or function). */
4951
as: PropTypes.elementType,

src/collections/Breadcrumb/BreadcrumbSection.js

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import cx from 'clsx'
22
import _ from 'lodash'
33
import PropTypes from 'prop-types'
4-
import React, { Component } from 'react'
4+
import React from 'react'
55

66
import {
77
childrenUtils,
@@ -10,35 +10,31 @@ import {
1010
getUnhandledProps,
1111
getElementType,
1212
useKeyOnly,
13+
useEventCallback,
1314
} from '../../lib'
1415

1516
/**
1617
* A section sub-component for Breadcrumb component.
1718
*/
18-
export default class BreadcrumbSection extends Component {
19-
computeElementType = () => {
20-
const { link, onClick } = this.props
19+
const BreadcrumbSection = React.forwardRef(function (props, ref) {
20+
const { active, children, className, content, href, link, onClick } = props
2121

22+
const classes = cx(useKeyOnly(active, 'active'), 'section', className)
23+
const rest = getUnhandledProps(BreadcrumbSection, props)
24+
const ElementType = getElementType(BreadcrumbSection, props, () => {
2225
if (link || onClick) return 'a'
23-
}
26+
})
2427

25-
handleClick = (e) => _.invoke(this.props, 'onClick', e, this.props)
28+
const handleClick = useEventCallback((e) => _.invoke(props, 'onClick', e, props))
2629

27-
render() {
28-
const { active, children, className, content, href } = this.props
29-
30-
const classes = cx(useKeyOnly(active, 'active'), 'section', className)
31-
const rest = getUnhandledProps(BreadcrumbSection, this.props)
32-
const ElementType = getElementType(BreadcrumbSection, this.props, this.computeElementType)
33-
34-
return (
35-
<ElementType {...rest} className={classes} href={href} onClick={this.handleClick}>
36-
{childrenUtils.isNil(children) ? content : children}
37-
</ElementType>
38-
)
39-
}
40-
}
30+
return (
31+
<ElementType {...rest} className={classes} href={href} onClick={handleClick} ref={ref}>
32+
{childrenUtils.isNil(children) ? content : children}
33+
</ElementType>
34+
)
35+
})
4136

37+
BreadcrumbSection.displayName = 'BreadcrumbSection'
4238
BreadcrumbSection.propTypes = {
4339
/** An element type to render as (string or function). */
4440
as: PropTypes.elementType,
@@ -75,3 +71,5 @@ BreadcrumbSection.create = createShorthandFactory(BreadcrumbSection, (content) =
7571
content,
7672
link: true,
7773
}))
74+
75+
export default BreadcrumbSection

src/views/Advertisement/Advertisement.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
/**
1414
* An ad displays third-party promotional content.
1515
*/
16-
function Advertisement(props) {
16+
const Advertisement = React.forwardRef(function (props, ref) {
1717
const { centered, children, className, content, test, unit } = props
1818

1919
const classes = cx(
@@ -28,12 +28,13 @@ function Advertisement(props) {
2828
const ElementType = getElementType(Advertisement, props)
2929

3030
return (
31-
<ElementType {...rest} className={classes} data-text={test}>
31+
<ElementType {...rest} className={classes} data-text={test} ref={ref}>
3232
{childrenUtils.isNil(children) ? content : children}
3333
</ElementType>
3434
)
35-
}
35+
})
3636

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

test/specs/collections/Breadcrumb/Breadcrumb-test.js

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

88
describe('Breadcrumb', () => {
99
common.isConformant(Breadcrumb)
10+
common.forwardsRef(Breadcrumb)
11+
common.forwardsRef(Breadcrumb, { requiredProps: { children: <span /> } })
1012
common.hasSubcomponents(Breadcrumb, [BreadcrumbDivider, BreadcrumbSection])
1113
common.hasUIClassName(Breadcrumb)
1214
common.rendersChildren(Breadcrumb, {

test/specs/collections/Breadcrumb/BreadcrumbDivider-test.js

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

34
import BreadcrumbDivider from 'src/collections/Breadcrumb/BreadcrumbDivider'
45
import * as common from 'test/specs/commonTests'
56

67
describe('BreadcrumbDivider', () => {
78
common.isConformant(BreadcrumbDivider)
9+
common.forwardsRef(BreadcrumbDivider)
10+
common.forwardsRef(BreadcrumbDivider, { requiredProps: { content: faker.lorem.word() } })
811
common.rendersChildren(BreadcrumbDivider)
912

1013
common.implementsIconProp(BreadcrumbDivider, {

test/specs/collections/Breadcrumb/BreadcrumbSection-test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { sandbox } from 'test/utils'
66

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

1112
common.propKeyOnlyToClassName(BreadcrumbSection, 'active')
@@ -39,7 +40,7 @@ describe('BreadcrumbSection', () => {
3940
const event = { target: null }
4041
const props = { active: true, content: 'home' }
4142

42-
shallow(<BreadcrumbSection onClick={onClick} {...props} />).simulate('click', event)
43+
mount(<BreadcrumbSection onClick={onClick} {...props} />).simulate('click', event)
4344

4445
onClick.should.have.been.calledOnce()
4546
onClick.should.have.been.calledWithMatch(event, props)

0 commit comments

Comments
 (0)