Skip to content

Commit

Permalink
feat: [Time] export Time
Browse files Browse the repository at this point in the history
  • Loading branch information
akai committed May 24, 2021
1 parent 5650b87 commit 1e6ad64
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 3 deletions.
22 changes: 22 additions & 0 deletions demo/src/components/LangSwitcher.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react';
import locales from '../../../src/components/LocaleProvider/locales';

interface LangSwitcherProps {
value: string;
onChange: (lang: string) => void;
}

export const LangSwitcher = ({ value, onChange }: LangSwitcherProps) => (
<select
value={value}
onChange={(e) => {
onChange(e.target.value);
}}
>
{Object.keys(locales).map((t) => (
<option value={t} key={t}>
{t}
</option>
))}
</select>
);
1 change: 1 addition & 0 deletions demo/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { DemoPage } from './DemoPage';
export { DemoSection } from './DemoSection';
export { LangSwitcher } from './LangSwitcher';
42 changes: 42 additions & 0 deletions demo/src/demo/Time.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React, { useState } from 'react';
import { DemoPage, DemoSection, LangSwitcher } from '../components';
import { Time, LocaleProvider } from '../../../src';
import '../../../src/styles/index.less';

const now = Date.now();
const MS_A_DAY = 24 * 60 * 60 * 1000;
const MS_A_WEEK = MS_A_DAY * 7;

export default () => {
const [lang, setLang] = useState('zh-CN');

return (
<DemoPage>
<DemoSection title="基础用法">
<LangSwitcher value={lang} onChange={setLang} />
<LocaleProvider locale={lang}>
<p>
<span>现在:</span>
<Time date={now} />
</p>
<p>
<span>刚才:</span>
<Time date={now - 120000} />
</p>
<p>
<span>昨天:</span>
<Time date={now - MS_A_DAY} />
</p>
<p>
<span>前天:</span>
<Time date={now - MS_A_DAY * 2} />
</p>
<p>
<span>上上周:</span>
<Time date={now - MS_A_WEEK * 2} />
</p>
</LocaleProvider>
</DemoSection>
</DemoPage>
);
};
1 change: 1 addition & 0 deletions demo/src/demo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export { default as Stepper } from './Stepper';
export { default as Tabs } from './Tabs';
export { default as Tag } from './Tag';
export { default as Text } from './Text';
export { default as Time } from './Time';
export { default as Toast } from './Toast';
export { default as Video } from './Video';
export { default as VisuallyHidden } from './VisuallyHidden';
1 change: 1 addition & 0 deletions demo/src/navConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const navConfig = [
{ name: '标签页', code: 'tabs' },
{ name: '标签', code: 'tag' },
{ name: '文本', code: 'text' },
{ name: '时间', code: 'time' },
{ name: '视频', code: 'video' },
{ name: '媒体对象(废弃)', code: 'media-object' },
],
Expand Down
2 changes: 1 addition & 1 deletion src/components/Time/Time.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ export const Time: React.FC<TimeProps> = ({ date }) => {
{formatDate(date, trans())}
</time>
);
}
};
1 change: 1 addition & 0 deletions src/components/Time/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { Time } from './Time';
export type { TimeProps } from './Time';
6 changes: 4 additions & 2 deletions src/components/Time/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ const getFormat = (date: Date) => {

if (diff < 0) {
return 'LT'; // 今天
} else if (diff < MS_A_DAY) {
}
if (diff < MS_A_DAY) {
return 'YT'; // 昨天
} else if (diff < MS_A_WEEK) {
}
if (diff < MS_A_WEEK) {
return 'WT'; // 这周
}
return 'lll';
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ export { Tag } from './components/Tag';
export type { TagProps } from './components/Tag';
export { Text } from './components/Text';
export type { TextProps } from './components/Text';
export { Time } from './components/Time';
export type { TimeProps } from './components/Time';
export { toast } from './components/Toast';
export { Toolbar } from './components/Toolbar';
export type { ToolbarProps, ToolbarItemProps } from './components/Toolbar';
Expand Down

0 comments on commit 1e6ad64

Please sign in to comment.