Skip to content

Commit

Permalink
chore(Item): use React.forwardRef()
Browse files Browse the repository at this point in the history
  • Loading branch information
layershifter committed Jul 26, 2021
1 parent 94491dc commit 3ebeb3d
Show file tree
Hide file tree
Showing 17 changed files with 67 additions and 31 deletions.
9 changes: 5 additions & 4 deletions src/views/Item/Item.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import ItemMeta from './ItemMeta'
/**
* An item view presents large collections of site content for display.
*/
function Item(props) {
const Item = React.forwardRef(function (props, ref) {
const { children, className, content, description, extra, header, image, meta } = props

const classes = cx('item', className)
Expand All @@ -23,14 +23,14 @@ function Item(props) {

if (!childrenUtils.isNil(children)) {
return (
<ElementType {...rest} className={classes}>
<ElementType {...rest} className={classes} ref={ref}>
{children}
</ElementType>
)
}

return (
<ElementType {...rest} className={classes}>
<ElementType {...rest} className={classes} ref={ref}>
{ItemImage.create(image, { autoGenerateKey: false })}

<ItemContent
Expand All @@ -42,7 +42,7 @@ function Item(props) {
/>
</ElementType>
)
}
})

Item.Content = ItemContent
Item.Description = ItemDescription
Expand All @@ -52,6 +52,7 @@ Item.Header = ItemHeader
Item.Image = ItemImage
Item.Meta = ItemMeta

Item.displayName = 'Item'
Item.propTypes = {
/** An element type to render as (string or function). */
as: PropTypes.elementType,
Expand Down
9 changes: 5 additions & 4 deletions src/views/Item/ItemContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import ItemMeta from './ItemMeta'
/**
* An item can contain content.
*/
function ItemContent(props) {
const ItemContent = React.forwardRef(function (props, ref) {
const { children, className, content, description, extra, header, meta, verticalAlign } = props

const classes = cx(useVerticalAlignProp(verticalAlign), 'content', className)
Expand All @@ -27,23 +27,24 @@ function ItemContent(props) {

if (!childrenUtils.isNil(children)) {
return (
<ElementType {...rest} className={classes}>
<ElementType {...rest} className={classes} ref={ref}>
{children}
</ElementType>
)
}

return (
<ElementType {...rest} className={classes}>
<ElementType {...rest} className={classes} ref={ref}>
{ItemHeader.create(header, { autoGenerateKey: false })}
{ItemMeta.create(meta, { autoGenerateKey: false })}
{ItemDescription.create(description, { autoGenerateKey: false })}
{ItemExtra.create(extra, { autoGenerateKey: false })}
{content}
</ElementType>
)
}
})

ItemContent.displayName = 'ItemContent'
ItemContent.propTypes = {
/** An element type to render as (string or function). */
as: PropTypes.elementType,
Expand Down
7 changes: 4 additions & 3 deletions src/views/Item/ItemDescription.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@ import {
/**
* An item can contain a description with a single or multiple paragraphs.
*/
function ItemDescription(props) {
const ItemDescription = React.forwardRef(function (props, ref) {
const { children, className, content } = props
const classes = cx('description', className)
const rest = getUnhandledProps(ItemDescription, props)
const ElementType = getElementType(ItemDescription, props)

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

ItemDescription.displayName = 'ItemDescription'
ItemDescription.propTypes = {
/** An element type to render as (string or function). */
as: PropTypes.elementType,
Expand Down
7 changes: 4 additions & 3 deletions src/views/Item/ItemExtra.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@ import {
/**
* An item can contain extra content meant to be formatted separately from the main content.
*/
function ItemExtra(props) {
const ItemExtra = React.forwardRef(function (props, ref) {
const { children, className, content } = props
const classes = cx('extra', className)
const rest = getUnhandledProps(ItemExtra, props)
const ElementType = getElementType(ItemExtra, props)

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

ItemExtra.displayName = 'ItemExtra'
ItemExtra.propTypes = {
/** An element type to render as (string or function). */
as: PropTypes.elementType,
Expand Down
11 changes: 6 additions & 5 deletions src/views/Item/ItemGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Item from './Item'
/**
* A group of items.
*/
function ItemGroup(props) {
const ItemGroup = React.forwardRef(function (props, ref) {
const { children, className, content, divided, items, link, relaxed, unstackable } = props

const classes = cx(
Expand All @@ -33,14 +33,14 @@ function ItemGroup(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>
)
Expand All @@ -56,12 +56,13 @@ function ItemGroup(props) {
})

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

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

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

ItemHeader.displayName = 'ItemHeader'
ItemHeader.propTypes = {
/** An element type to render as (string or function). */
as: PropTypes.elementType,
Expand Down
14 changes: 12 additions & 2 deletions src/views/Item/ItemImage.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import * as React from 'react'

import { ImageProps, StrictImageProps } from '../../elements/Image'
import { SemanticSIZES } from '../../generic'

export interface ItemImageProps extends ImageProps {
[key: string]: any

export interface ItemImageProps extends ImageProps {}
/** An image may appear at different sizes. */
size?: SemanticSIZES
}

export interface StrictItemImageProps extends StrictImageProps {}
export interface StrictItemImageProps extends StrictImageProps {
/** An image may appear at different sizes. */
size?: SemanticSIZES
}

declare const ItemImage: React.StatelessComponent<ItemImageProps>

Expand Down
7 changes: 4 additions & 3 deletions src/views/Item/ItemImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import Image from '../../elements/Image'
/**
* An item can contain an image.
*/
function ItemImage(props) {
const ItemImage = React.forwardRef(function (props, ref) {
const { size } = props
const rest = getUnhandledProps(ItemImage, props)

return <Image {...rest} size={size} ui={!!size} wrapped />
}
return <Image {...rest} size={size} ui={!!size} wrapped ref={ref} />
})

ItemImage.displayName = 'ItemImage'
ItemImage.propTypes = {
/** An image may appear at different sizes. */
size: Image.propTypes.size,
Expand Down
7 changes: 4 additions & 3 deletions src/views/Item/ItemMeta.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@ import {
/**
* An item can contain content metadata.
*/
function ItemMeta(props) {
const ItemMeta = React.forwardRef(function (props, ref) {
const { children, className, content } = props
const classes = cx('meta', className)
const rest = getUnhandledProps(ItemMeta, props)
const ElementType = getElementType(ItemMeta, props)

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

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

describe('Item', () => {
common.isConformant(Item)
common.forwardsRef(Item)
common.forwardsRef(Item, { requiredProps: { children: <span /> } })
common.forwardsRef(Item, { requiredProps: { content: faker.lorem.word() } })
common.hasSubcomponents(Item, [
ItemContent,
ItemDescription,
Expand Down
6 changes: 6 additions & 0 deletions test/specs/views/Item/ItemContent-test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import faker from 'faker'
import React from 'react'

import ItemContent from 'src/views/Item/ItemContent'
import ItemDescription from 'src/views/Item/ItemDescription'
import ItemExtra from 'src/views/Item/ItemExtra'
Expand All @@ -7,6 +10,9 @@ import * as common from 'test/specs/commonTests'

describe('ItemContent', () => {
common.isConformant(ItemContent)
common.forwardsRef(ItemContent)
common.forwardsRef(ItemContent, { requiredProps: { children: <span /> } })
common.forwardsRef(ItemContent, { requiredProps: { content: faker.lorem.word() } })
common.rendersChildren(ItemContent)

common.implementsShorthandProp(ItemContent, {
Expand Down
1 change: 1 addition & 0 deletions test/specs/views/Item/ItemDescription-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('ItemDescription', () => {
common.isConformant(ItemDescription)
common.forwardsRef(ItemDescription)
common.rendersChildren(ItemDescription)

common.implementsCreateMethod(ItemDescription)
Expand Down
1 change: 1 addition & 0 deletions test/specs/views/Item/ItemExtra-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('ItemExtra', () => {
common.isConformant(ItemExtra)
common.forwardsRef(ItemExtra)
common.rendersChildren(ItemExtra)

common.implementsCreateMethod(ItemExtra)
Expand Down
5 changes: 4 additions & 1 deletion test/specs/views/Item/ItemGroup-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import ItemGroup from 'src/views/Item/ItemGroup'
import * as common from 'test/specs/commonTests'

describe('ItemGroup', () => {
common.hasUIClassName(ItemGroup)
common.isConformant(ItemGroup)
common.forwardsRef(ItemGroup)
common.forwardsRef(ItemGroup, { requiredProps: { children: <span /> } })
common.forwardsRef(ItemGroup, { requiredProps: { content: faker.lorem.word() } })
common.hasUIClassName(ItemGroup)
common.rendersChildren(ItemGroup)

common.propKeyOnlyToClassName(ItemGroup, 'divided')
Expand Down
1 change: 1 addition & 0 deletions test/specs/views/Item/ItemHeader-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('ItemHeader', () => {
common.isConformant(ItemHeader)
common.forwardsRef(ItemHeader)
common.rendersChildren(ItemHeader)

common.implementsCreateMethod(ItemHeader)
Expand Down
2 changes: 2 additions & 0 deletions test/specs/views/Item/ItemImage-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import ItemImage from 'src/views/Item/ItemImage'
import * as common from 'test/specs/commonTests'

describe('ItemImage', () => {
common.isConformant(ItemImage, { rendersChildren: false })
common.forwardsRef(ItemImage, { tagName: 'img' })
common.implementsCreateMethod(ItemImage)

it('renders Image component', () => {
Expand Down
1 change: 1 addition & 0 deletions test/specs/views/Item/ItemMeta-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('ItemMeta', () => {
common.isConformant(ItemMeta)
common.forwardsRef(ItemMeta)
common.rendersChildren(ItemMeta)

common.implementsCreateMethod(ItemMeta)
Expand Down

0 comments on commit 3ebeb3d

Please sign in to comment.