Skip to content

Commit

Permalink
chore(Header): use React.forwardRef() (#4237)
Browse files Browse the repository at this point in the history
  • Loading branch information
layershifter committed Feb 18, 2022
1 parent 0ef11e5 commit 166edb5
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 11 deletions.
11 changes: 6 additions & 5 deletions src/elements/Header/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import HeaderContent from './HeaderContent'
/**
* A header provides a short summary of content
*/
function Header(props) {
const Header = React.forwardRef(function HeaderInner(props, ref) {
const {
attached,
block,
Expand Down Expand Up @@ -65,7 +65,7 @@ function Header(props) {

if (!childrenUtils.isNil(children)) {
return (
<ElementType {...rest} className={classes}>
<ElementType {...rest} className={classes} ref={ref}>
{children}
</ElementType>
)
Expand All @@ -77,7 +77,7 @@ function Header(props) {

if (iconElement || imageElement) {
return (
<ElementType {...rest} className={classes}>
<ElementType {...rest} className={classes} ref={ref}>
{iconElement || imageElement}
{(content || subheaderElement) && (
<HeaderContent>
Expand All @@ -90,13 +90,14 @@ function Header(props) {
}

return (
<ElementType {...rest} className={classes}>
<ElementType {...rest} className={classes} ref={ref}>
{content}
{subheaderElement}
</ElementType>
)
}
})

Header.displayName = 'Header'
Header.propTypes = {
/** An element type to render as (string or function). */
as: PropTypes.elementType,
Expand Down
7 changes: 4 additions & 3 deletions src/elements/Header/HeaderContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ import { childrenUtils, customPropTypes, getElementType, getUnhandledProps } fro
/**
* Header content wraps the main content when there is an adjacent Icon or Image.
*/
function HeaderContent(props) {
const HeaderContent = React.forwardRef(function HeaderContentInner(props, ref) {
const { children, className, content } = props
const classes = cx('content', className)
const rest = getUnhandledProps(HeaderContent, props)
const ElementType = getElementType(HeaderContent, props)

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

HeaderContent.displayName = 'HeaderContent'
HeaderContent.propTypes = {
/** An element type to render as (string or function). */
as: PropTypes.elementType,
Expand Down
7 changes: 4 additions & 3 deletions src/elements/Header/HeaderSubheader.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@ import {
/**
* Headers may contain subheaders.
*/
function HeaderSubheader(props) {
const HeaderSubheader = React.forwardRef(function HeaderSubheaderInner(props, ref) {
const { children, className, content } = props
const classes = cx('sub header', className)
const rest = getUnhandledProps(HeaderSubheader, props)
const ElementType = getElementType(HeaderSubheader, props)

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

HeaderSubheader.displayName = 'HeaderSubheader'
HeaderSubheader.propTypes = {
/** An element type to render as (string or function). */
as: PropTypes.elementType,
Expand Down
2 changes: 2 additions & 0 deletions test/specs/elements/Header/Header-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import * as common from 'test/specs/commonTests'

describe('Header', () => {
common.hasUIClassName(Header)
common.forwardsRef(Header, { children: <span /> })
common.forwardsRef(Header, { icon: 'book' })
common.hasSubcomponents(Header, [HeaderContent, HeaderSubheader])
common.rendersChildren(Header)

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

describe('HeaderContent', () => {
common.isConformant(HeaderContent)
common.forwardsRef(HeaderContent)
common.rendersChildren(HeaderContent)
})
1 change: 1 addition & 0 deletions test/specs/elements/Header/HeaderSubheader-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import * as common from 'test/specs/commonTests'

describe('HeaderSubheader', () => {
common.isConformant(HeaderSubheader)
common.forwardsRef(HeaderSubheader)
common.rendersChildren(HeaderSubheader)
})

0 comments on commit 166edb5

Please sign in to comment.