diff --git a/UNRELEASED.md b/UNRELEASED.md index b1858525ba4..7e2c82f7513 100644 --- a/UNRELEASED.md +++ b/UNRELEASED.md @@ -13,6 +13,7 @@ Use [the changelog guidelines](https://git.io/polaris-changelog-guidelines) to f - Set `Collapsible` to use `overflow: visible;` once fully open ([#951](https://github.com/Shopify/polaris-react/pull/951)) - Removes `TopBar` logo background ([#957](https://github.com/Shopify/polaris-react/pull/957)) - Adopt width of `TopBar` search results to search input and add a minimum width ([#969](https://github.com/Shopify/polaris-react/pull/969)) +- Update `Card.Section` to accept `React.ReactNode` as `title` ([#781](https://github.com/Shopify/polaris-react/pull/781)) ### Bug fixes diff --git a/src/components/Card/README.md b/src/components/Card/README.md index 27d98eaefcf..a792c0420ce 100644 --- a/src/components/Card/README.md +++ b/src/components/Card/README.md @@ -436,6 +436,30 @@ Use to be able to use custom React elements as header content. ``` +### Card section with custom React Node title + + + +Use to render custom content such as icons, links, or buttons in a card section's header. + +```jsx + + + + New Products + + } + > + + Socks + Super Shoes + + + +``` + --- ## Related components diff --git a/src/components/Card/components/Section/Section.tsx b/src/components/Card/components/Section/Section.tsx index 8a561cb48db..819999cb506 100644 --- a/src/components/Card/components/Section/Section.tsx +++ b/src/components/Card/components/Section/Section.tsx @@ -6,28 +6,29 @@ import Subheading from '../../../Subheading'; import styles from '../../Card.scss'; export interface Props { - title?: string; + title?: React.ReactNode; children?: React.ReactNode; subdued?: boolean; fullWidth?: boolean; } export default function Section({children, title, subdued, fullWidth}: Props) { - const headerContent = title ? ( -
- {title} -
- ) : null; - const className = classNames( styles.Section, subdued && styles['Section-subdued'], fullWidth && styles['Section-fullWidth'], ); + let headerMarkup = null; + if (title) { + const headerContent = + typeof title === 'string' ? {title} : title; + headerMarkup =
{headerContent}
; + } + return (
- {headerContent} + {headerMarkup} {children}
); diff --git a/src/components/Card/components/Section/tests/Section.test.tsx b/src/components/Card/components/Section/tests/Section.test.tsx new file mode 100644 index 00000000000..a487ff591db --- /dev/null +++ b/src/components/Card/components/Section/tests/Section.test.tsx @@ -0,0 +1,33 @@ +import * as React from 'react'; +import {mountWithAppProvider} from 'test-utilities'; +import {Badge, Subheading} from 'components'; +import Section from '../Section'; + +describe('', () => { + it('can have any valid react element as the card section title', () => { + const titleString = 'Online store'; + const badgeString = 'I am a badge'; + const titleMarkup = ( +

+ {titleString} + {badgeString} +

+ ); + + const card = mountWithAppProvider(
); + const headerMarkup = card.find('h2'); + + expect(headerMarkup.text().includes(titleString)).toBe(true); + expect(headerMarkup.find('Badge').text()).toBe(badgeString); + }); + + it('wraps plain string titles in a ', () => { + const titleString = 'Online store'; + + const card = mountWithAppProvider(
); + const headerMarkup = card.find(Subheading); + + expect(headerMarkup.exists()).toBeTruthy(); + expect(headerMarkup.text()).toEqual(titleString); + }); +});