Skip to content

Commit

Permalink
chore(Segment): use React.forwardRef() (#4238)
Browse files Browse the repository at this point in the history
  • Loading branch information
layershifter committed Dec 13, 2022
1 parent 6cd1359 commit 50d8109
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 9 deletions.
7 changes: 4 additions & 3 deletions src/elements/Segment/Segment.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import SegmentInline from './SegmentInline'
/**
* A segment is used to create a grouping of related content.
*/
function Segment(props) {
const Segment = React.forwardRef(function SegmentInner(props, ref) {
const {
attached,
basic,
Expand Down Expand Up @@ -76,15 +76,16 @@ function Segment(props) {
const ElementType = getElementType(Segment, props)

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

Segment.Group = SegmentGroup
Segment.Inline = SegmentInline

Segment.displayName = 'Segment'
Segment.propTypes = {
/** An element type to render as (string or function). */
as: PropTypes.elementType,
Expand Down
7 changes: 4 additions & 3 deletions src/elements/Segment/SegmentGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
/**
* A group of segments can be formatted to appear together.
*/
function SegmentGroup(props) {
const SegmentGroup = React.forwardRef(function SegmentGroupInner(props, ref) {
const { children, className, compact, content, horizontal, piled, raised, size, stacked } = props

const classes = cx(
Expand All @@ -33,12 +33,13 @@ function SegmentGroup(props) {
const ElementType = getElementType(SegmentGroup, props)

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

SegmentGroup.displayName = 'SegmentGroup'
SegmentGroup.propTypes = {
/** An element type to render as (string or function). */
as: PropTypes.elementType,
Expand Down
7 changes: 4 additions & 3 deletions src/elements/Segment/SegmentInline.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ import { childrenUtils, customPropTypes, getElementType, getUnhandledProps } fro
/**
* A placeholder segment can be inline.
*/
function SegmentInline(props) {
const SegmentInline = React.forwardRef(function SegmentInlineInner(props, ref) {
const { children, className, content } = props
const classes = cx('inline', className)
const rest = getUnhandledProps(SegmentInline, props)
const ElementType = getElementType(SegmentInline, props)

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

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

describe('Segment', () => {
common.isConformant(Segment)
common.forwardsRef(Segment)
common.hasSubcomponents(Segment, [SegmentGroup, SegmentInline])
common.hasUIClassName(Segment)
common.rendersChildren(Segment)
Expand Down
1 change: 1 addition & 0 deletions test/specs/elements/Segment/SegmentGroup-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as common from 'test/specs/commonTests'

describe('SegmentGroup', () => {
common.isConformant(SegmentGroup)
common.forwardsRef(SegmentGroup)
common.hasUIClassName(SegmentGroup)
common.rendersChildren(SegmentGroup)

Expand Down
1 change: 1 addition & 0 deletions test/specs/elements/Segment/SegmentInline-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('SegmentInline', () => {
common.isConformant(SegmentInline)
common.forwardsRef(SegmentInline)
common.rendersChildren(SegmentInline)
})

0 comments on commit 50d8109

Please sign in to comment.