-
-
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
- Loading branch information
Showing
10 changed files
with
112 additions
and
73 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
24 changes: 24 additions & 0 deletions
24
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,24 @@ | ||
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> | ||
<br /> | ||
<DisplayDate date={subMonths(new Date(), 2)}></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,31 @@ | ||
import { format, formatDistanceToNow } from 'date-fns' | ||
import { Text } from 'theme-ui' | ||
|
||
type DateType = string | number | Date | ||
|
||
export interface DisplayDateProps { | ||
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 }: DisplayDateProps) => { | ||
console.log(date) | ||
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
This file was deleted.
Oops, something went wrong.