-
-
Notifications
You must be signed in to change notification settings - Fork 380
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: display date as time has passed (#3840)
- Loading branch information
Showing
13 changed files
with
137 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
packages/components/src/DisplayDate/DisplayDate.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { subMonths } from 'date-fns' | ||
|
||
import { DisplayDate } from './DisplayDate' | ||
|
||
import type { Meta, StoryFn } from '@storybook/react' | ||
|
||
export default { | ||
/* 👇 The title prop is optional. | ||
* See https://storybook.js.org/docs/react/configure/overview#configure-story-loading | ||
* to learn how to generate automatic titles | ||
*/ | ||
title: 'Components/DisplayDate', | ||
component: DisplayDate, | ||
} as Meta<typeof DisplayDate> | ||
|
||
export const Default: StoryFn<typeof DisplayDate> = () => { | ||
return <DisplayDate date={new Date()}></DisplayDate> | ||
} | ||
|
||
export const TwoMonthsAGo: StoryFn<typeof DisplayDate> = () => { | ||
const twoMonthsAGo = subMonths(new Date(), 2) | ||
return <DisplayDate date={twoMonthsAGo}></DisplayDate> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import '@testing-library/jest-dom/vitest' | ||
|
||
import { describe, expect, it } from 'vitest' | ||
|
||
import { render } from '../test/utils' | ||
import { Default, TwoMonthsAGo } from './DisplayDate.stories' | ||
|
||
import type { IProps } from './DisplayDate' | ||
|
||
describe('DisplayDate', () => { | ||
it('renders correctly current date', () => { | ||
const { getByText } = render(<Default {...(TwoMonthsAGo.args as IProps)} />) | ||
|
||
expect(getByText('less than a minute ago')).toBeInTheDocument() | ||
}) | ||
|
||
it('renders correctly when two months ago', () => { | ||
const { getByText } = render( | ||
<TwoMonthsAGo {...(TwoMonthsAGo.args as IProps)} />, | ||
) | ||
|
||
expect(getByText('2 months ago')).toBeInTheDocument() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { format, formatDistanceToNow } from 'date-fns' | ||
import { Text } from 'theme-ui' | ||
|
||
type DateType = string | number | Date | ||
|
||
export interface IProps { | ||
date?: DateType | ||
} | ||
|
||
const formatDateTime = (date: DateType | undefined) => { | ||
if (!date) { | ||
return '' | ||
} | ||
|
||
return format(new Date(date), 'dd-MM-yyyy HH:mm') | ||
} | ||
|
||
const relativeDateFormat = (d: DateType | undefined): string => { | ||
if (!d) { | ||
return '' | ||
} | ||
return formatDistanceToNow(new Date(d), { addSuffix: true }) | ||
} | ||
|
||
export const DisplayDate = ({ date }: IProps) => { | ||
const formattedDate = formatDateTime(date) | ||
const relativeDate = relativeDateFormat(date) | ||
|
||
return <Text title={formattedDate}>{relativeDate}</Text> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.