Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(components): add new component #40

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
}
},
"dependencies": {
"clsx": "^1.1.1"
"clsx": "^1.1.1",
"plural-ru": "^2.0.2"
},
"devDependencies": {
"@babel/cli": "^7.13.14",
Expand Down
51 changes: 51 additions & 0 deletions src/__docz__/formatted-date.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
name: FormattedDate
route: /formatted-date
---
import { Playground, Props } from 'docz'
import { FormattedDate } from '../';


# FormattedDate

## Properties

<Props of={FormattedDate} />

## Date from string

<Playground>
{() => {
const dateFromServer = '2022-10-10T17:33:32.877Z';
return <FormattedDate date={new Date(dateFromServer)}/>;
}}
</Playground>

## Today

<Playground>
{() => {
const today = new Date();
return <FormattedDate date={new Date(today.getFullYear(), today.getMonth(), today.getDate(), today.getHours(), today.getMinutes() - 1, 0)}/>;
}}
</Playground>

## Yesterday

<Playground>
{() => {
const today = new Date();
const yesterday = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 1, today.getHours(), today.getMinutes() - 1, 0);
return <FormattedDate date={yesterday}/>;
}}
</Playground>

## 5 days ago

<Playground>
{() => {
const today = new Date();
const fiveDaysAgo = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 5, today.getHours(), today.getMinutes() - 1, 0);
return <FormattedDate date={fiveDaysAgo}/>;
}}
</Playground>
2 changes: 2 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Button } from './ui/button';
import { Logo } from './ui/logo';
import { ConstructorElement } from './ui/constructor-element';
import { Tab } from './ui/tab';
import { FormattedDate } from './ui/formatted-date/formatted-date';
import { Input } from './ui/input';
import { Counter } from './ui/counter';
import { EmailInput } from './ui/email-input';
Expand Down Expand Up @@ -35,6 +36,7 @@ export {
Logo,
ConstructorElement,
Tab,
FormattedDate,
Input,
Counter,
EmailInput,
Expand Down
17 changes: 17 additions & 0 deletions src/ui/formatted-date/formatted-date.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';
import { getFormattedDate, isYesterday, isToday, getFormattedTime } from './formatted-date.utils';

export const FormattedDate: React.FC<{
date: Date;
className?: string;
}> = ({ date, className }) => {
if (isToday(date)) {
return <span className={className}>Сегодня, {getFormattedTime(date)}</span>;
}

if (isYesterday(date)) {
return <span className={className}>Вчера, {getFormattedTime(date)}</span>;
}

return <span className={className}>{getFormattedDate(date)}</span>;
};
15 changes: 15 additions & 0 deletions src/ui/formatted-date/formatted-date.utils.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import plural from 'plural-ru';

const getDiffDays = (date: Date) =>
Math.floor((new Date().getTime() - date.getTime()) / (1000 * 60 * 60 * 24));

export const getFormattedTime = (date: Date): string =>
`${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}`;

export const getFormattedDate = (date: Date): string => {
const diffDays = getDiffDays(date);
return `${plural(diffDays, '%d день', '%d дня', '%d дней')} назад, ${getFormattedTime(date)}`;
};

export const isToday = (date: Date): boolean => getDiffDays(date) === 0;
export const isYesterday = (date: Date): boolean => getDiffDays(date) === 1;