From b35d6563db4cd0dea511af96ccb2c713467cc0d9 Mon Sep 17 00:00:00 2001 From: Cameron Gorrie Date: Thu, 7 Mar 2019 12:47:41 -0500 Subject: [PATCH] Navigation badge accepts react element --- UNRELEASED.md | 2 + .../Navigation/components/Item/Item.tsx | 39 ++++++++++++------- .../components/Item/tests/Item.test.tsx | 28 +++++++++++++ 3 files changed, 56 insertions(+), 13 deletions(-) diff --git a/UNRELEASED.md b/UNRELEASED.md index 61a60d9e8ad..1b314801683 100644 --- a/UNRELEASED.md +++ b/UNRELEASED.md @@ -12,6 +12,8 @@ Use [the changelog guidelines](https://git.io/polaris-changelog-guidelines) to f ### Enhancements +- Updated `Navigation` badge prop to accept a react node ([#1142](https://github.com/Shopify/polaris-react/pull/1142)) + ### Bug fixes - Fixed unnecessary height on `TextField` due to unhandled carriage returns ([#901](https://github.com/Shopify/polaris-react/pull/901)) diff --git a/src/components/Navigation/components/Item/Item.tsx b/src/components/Navigation/components/Item/Item.tsx index b62d8dc1e52..c7c755e6007 100644 --- a/src/components/Navigation/components/Item/Item.tsx +++ b/src/components/Navigation/components/Item/Item.tsx @@ -40,7 +40,7 @@ interface SecondaryAction { export interface Props extends ItemURLDetails { icon?: IconProps['source']; iconBody?: string; - badge?: string | null; + badge?: React.ReactNode; label: string; disabled?: boolean; accessibilityLabel?: string; @@ -90,16 +90,16 @@ export class BaseItem extends React.Component { url, icon, label, - badge, subNavigationItems = [], secondaryAction, disabled, onClick, accessibilityLabel, iconBody, + selected: selectedOverride, + badge, new: isNew, polaris: {intl}, - selected: selectedOverride, } = this.props; const {location, onNavigationDismiss} = this.context; @@ -116,15 +116,6 @@ export class BaseItem extends React.Component { ) : null; - const badgeMarkup = - badge || isNew ? ( -
- - {badge || intl.translate('Polaris.Badge.STATUS_LABELS.new')} - -
- ) : null; - const iconMarkup = iconBody ? (
@@ -137,6 +128,28 @@ export class BaseItem extends React.Component { ) ); + let badgeMarkup: React.ReactNode = null; + if (isNew) { + badgeMarkup = ( + + {intl.translate('Polaris.Badge.STATUS_LABELS.new')} + + ); + } else if (typeof badge === 'string') { + badgeMarkup = ( + + {badge} + + ); + } else { + badgeMarkup = badge; + } + + const wrappedBadgeMarkup = + badgeMarkup == null ? null : ( +
{badgeMarkup}
+ ); + const itemContentMarkup = ( {iconMarkup} @@ -144,7 +157,7 @@ export class BaseItem extends React.Component { {label} {indicatorMarkup} - {badgeMarkup} + {wrappedBadgeMarkup} ); diff --git a/src/components/Navigation/components/Item/tests/Item.test.tsx b/src/components/Navigation/components/Item/tests/Item.test.tsx index 9ee2a827291..2d797410330 100644 --- a/src/components/Navigation/components/Item/tests/Item.test.tsx +++ b/src/components/Navigation/components/Item/tests/Item.test.tsx @@ -100,6 +100,34 @@ describe('', () => { const link = item.find(UnstyledLink); expect(link.exists()).toBe(true); }); + + it('renders a small badge with new status if the prop is provided with a string', () => { + const item = mountWithAppProvider(); + + expect(item.find(Badge).props()).toMatchObject({ + status: 'new', + size: 'small', + children: '1', + }); + }); + + it('renders a badge if the prop is provided with an element', () => { + const item = mountWithAppProvider( + Custom badge} />, + ); + + expect(item.find(Badge).text()).toContain('Custom badge'); + }); + + it('renders a single new badge even if a badge prop is also provided', () => { + const item = mountWithAppProvider( + Custom badge} new />, + ); + const badge = item.find(Badge); + + expect(badge).toHaveLength(1); + expect(badge.text()).toContain('New'); + }); }); describe('with SubNavigationItems', () => {