Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Item): actually make content a shorthand prop for ItemContent #4118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/views/Item/Item.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react'

import { SemanticShorthandContent, SemanticShorthandItem } from '../../generic'
import ItemContent from './ItemContent'
import { SemanticShorthandItem } from '../../generic'
import ItemContent, { ItemContentProps } from './ItemContent'
import ItemDescription, { ItemDescriptionProps } from './ItemDescription'
import ItemExtra, { ItemExtraProps } from './ItemExtra'
import ItemGroup from './ItemGroup'
Expand All @@ -24,7 +24,7 @@ export interface StrictItemProps {
className?: string

/** Shorthand for ItemContent component. */
content?: SemanticShorthandContent
content?: SemanticShorthandItem<ItemContentProps>

/** Shorthand for ItemDescription component. */
description?: SemanticShorthandItem<ItemDescriptionProps>
Expand Down
22 changes: 14 additions & 8 deletions src/views/Item/Item.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import cx from 'clsx'
import _ from 'lodash'
import PropTypes from 'prop-types'
import React from 'react'

Expand Down Expand Up @@ -29,17 +30,22 @@ function Item(props) {
)
}

let contentShorthandValue = content
if (
contentShorthandValue === undefined &&
[description, extra, header, meta].some((prop) => !_.isNil(prop))
) {
contentShorthandValue = {}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only setting this when content is strictly undefined here and letting ItemContent.create handle everything else, because it allows the user to still use null (or a boolean) to prevent rendering when one of the other shorthands is set. Maybe true should also be included though?

}

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

<ItemContent
content={content}
description={description}
extra={extra}
header={header}
meta={meta}
/>
{ItemContent.create(contentShorthandValue, {
autoGenerateKey: false,
overrideProps: { description, extra, header, meta },
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively you could use defaultProps here, but I think it makes sense for them to override because they're more 'specific'.

})}
</ElementType>
)
}
Expand All @@ -63,7 +69,7 @@ Item.propTypes = {
className: PropTypes.string,

/** Shorthand for ItemContent component. */
content: customPropTypes.contentShorthand,
content: customPropTypes.itemShorthand,

/** Shorthand for ItemDescription component. */
description: customPropTypes.itemShorthand,
Expand Down
3 changes: 3 additions & 0 deletions src/views/Item/ItemContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React from 'react'

import {
childrenUtils,
createShorthandFactory,
customPropTypes,
getElementType,
getUnhandledProps,
Expand Down Expand Up @@ -73,4 +74,6 @@ ItemContent.propTypes = {
verticalAlign: PropTypes.oneOf(SUI.VERTICAL_ALIGNMENTS),
}

ItemContent.create = createShorthandFactory(ItemContent, (content) => ({ content }))

export default ItemContent
7 changes: 7 additions & 0 deletions test/specs/views/Item/Item-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ describe('Item', () => {
mapValueToProps: (val) => ({ src: val }),
})

common.implementsShorthandProp(Item, {
autoGenerateKey: false,
propKey: 'content',
ShorthandComponent: ItemContent,
mapValueToProps: (val) => ({ content: val }),
})

describe('content prop', () => {
it('renders ItemContent component', () => {
shallow(<Item content={faker.hacker.phrase()} />).should.have.descendants('ItemContent')
Expand Down