Skip to content

Commit

Permalink
Adds noTitle option to RelativeTime component (#4635)
Browse files Browse the repository at this point in the history
* Adds noTitle option to RelativeTime component

* Removes iconbutton story

* Reverts package-lock.json changes

* Adds changeset

* Update packages/react/src/RelativeTime/RelativeTime.tsx

Co-authored-by: Clay Miller <[email protected]>

* Update clever-pots-leave.md

---------

Co-authored-by: Clay Miller <[email protected]>
Co-authored-by: Siddharth Kshetrapal <[email protected]>
  • Loading branch information
3 people authored Jun 5, 2024
1 parent 240fa50 commit bd861cc
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-pots-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': minor
---

RelativeTime: Adds `noTitle` prop if you want to not render the `title` attribute with full date time.
6 changes: 6 additions & 0 deletions packages/react/src/RelativeTime/RelativeTime.docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@
"name": "ref",
"type": "React.RefObject<RelativeTimeElement>"
},
{
"name": "noTitle",
"type": "boolean",
"defaultValue": "",
"description": "Removes the `title` attribute provided on the element by default"
},
{
"name": "as",
"type": "React.ElementType",
Expand Down
19 changes: 19 additions & 0 deletions packages/react/src/RelativeTime/RelativeTime.examples.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react'
import RelativeTime from './RelativeTime'
import Link from '../Link'
import {Tooltip} from '../TooltipV2'

export default {
title: 'Components/RelativeTime/Examples',
component: RelativeTime,
}

export const NoTitleAttribute = () => <RelativeTime date={new Date('2020-01-01T00:00:00Z')} noTitle={true} />

export const LinkWithTooltip = () => (
<Tooltip text={new Date('2020-01-01T00:00:00Z').toString()}>
<Link href="https://www.github.com">
<RelativeTime date={new Date('2020-01-01T00:00:00Z')} noTitle={true} />
</Link>
</Tooltip>
)
2 changes: 1 addition & 1 deletion packages/react/src/RelativeTime/RelativeTime.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const meta: Meta = {
},
}

export const Default: Story = () => <RelativeTime date={new Date('2020-01-01T00:00:00Z')} />
export const Default: Story = () => <RelativeTime date={new Date('2020-01-01T00:00:00Z')} noTitle={true} />

export const Playground: Story = args => {
const {date, ...rest} = args
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/RelativeTime/RelativeTime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {createComponent} from '../utils/create-component'
const RelativeTimeComponent = createComponent(RelativeTimeElement, 'relative-time')

const localeOptions: Intl.DateTimeFormatOptions = {month: 'short', day: 'numeric', year: 'numeric'}
function RelativeTime({date, datetime, children, ...props}: RelativeTimeProps) {
function RelativeTime({date, datetime, children, noTitle, ...props}: RelativeTimeProps) {
if (datetime) date = new Date(datetime)
return (
<RelativeTimeComponent {...props} date={date}>
<RelativeTimeComponent {...props} date={date} no-title={noTitle ? '' : undefined}>
{children || date?.toLocaleDateString('en', localeOptions) || ''}
</RelativeTimeComponent>
)
Expand Down
12 changes: 12 additions & 0 deletions packages/react/src/__tests__/RelativeTime.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,16 @@ describe('RelativeTime', () => {
'server rendered date',
])
})

it('does not render no-title attribute by default', () => {
const date = new Date('2024-03-07T12:22:48.123Z')
const {container} = HTMLRender(<RelativeTime date={date} />)
expect(container.firstChild).not.toHaveAttribute('no-title')
})

it('adds no-title attribute if noTitle={true}', () => {
const date = new Date('2024-03-07T12:22:48.123Z')
const {container} = HTMLRender(<RelativeTime date={date} noTitle={true} />)
expect(container.firstChild).toHaveAttribute('no-title')
})
})

0 comments on commit bd861cc

Please sign in to comment.