Skip to content

Commit

Permalink
feat(card): Make date optional on <ResourceCard /> (#1001)
Browse files Browse the repository at this point in the history
* chore: Update package-lock.json

* feat: Make dates optional

* feat: Add test

* chore: Add changeset

* chore: Adjustments for immutability

- Test: Spread default props into new object
- meta assignment: use new arrays as values rather than mutating
  • Loading branch information
EnMod committed Sep 26, 2023
1 parent 5974f08 commit 901bf28
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/old-comics-argue.md
@@ -0,0 +1,5 @@
---
'@hashicorp/react-card': minor
---

Makes the `date` prop optional, preventing it and the `|` separator from rendering if not set.
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/card/docs.mdx
Expand Up @@ -316,7 +316,6 @@ A component used to promote and link to marketing pages, always using a descript
appearance="dark"
heading="Display 5 - 4 lines with character limit of 130. Leo mauris fermentum pharetra blandit tellus"
category="Category"
date="August 15, 2022"
thumbnail={{
src: 'https://www.datocms-assets.com/19447/1648680074-screenshot-2022-03-31-at-00-40-47.png',
alt: 'HashiConf Europe 2022 Recap',
Expand Down
16 changes: 15 additions & 1 deletion packages/card/resource-card/index.test.tsx
Expand Up @@ -18,7 +18,7 @@ const defaultProps = {
}

describe('<ResourceCard />', () => {
it('should render the provided metadata correctly', () => {
it('should render the provided date and category with separator', () => {
const expectedMeta = [defaultProps.date, defaultProps.category]

render(<ResourceCard {...defaultProps} />)
Expand All @@ -31,4 +31,18 @@ describe('<ResourceCard />', () => {

expect(metaElement).toContainElement(screen.getByText('|'))
})

it('should not render the date if no date is provided', () => {
const defaultPropsWithoutDate = { ...defaultProps, date: '' }

render(<ResourceCard {...defaultPropsWithoutDate} />)

const metaElement = screen.getByTestId('wpl-card-meta')

expect(metaElement).toContainElement(
screen.getByText(defaultProps.category)
)

expect(metaElement).not.toContainElement(screen.queryByText('|'))
})
})
6 changes: 4 additions & 2 deletions packages/card/resource-card/index.tsx
Expand Up @@ -8,7 +8,7 @@ import type { CardProps, ThumbnailProps } from '../types'

export interface ResourceCardProps {
heading: string
date: string
date?: string
category: string
link: string
productBadges?: CardProps['productBadges']
Expand All @@ -25,6 +25,8 @@ export function ResourceCard({
thumbnail,
appearance,
}: ResourceCardProps): JSX.Element {
const meta: string[] = date ? [date, category] : [category]

return (
<CardPrimitives.Card
heading={heading}
Expand All @@ -34,7 +36,7 @@ export function ResourceCard({
>
<CardPrimitives.Thumbnail {...thumbnail} />
<CardPrimitives.Content>
<CardPrimitives.Meta items={[date, category]} />
<CardPrimitives.Meta items={meta} />
<CardPrimitives.Heading>{heading}</CardPrimitives.Heading>
{productBadges && productBadges?.length > 0 ? (
<CardPrimitives.ProductBadges
Expand Down

1 comment on commit 901bf28

@vercel
Copy link

@vercel vercel bot commented on 901bf28 Sep 26, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.