Skip to content

Commit

Permalink
feat(design): DatePicker add format and global format demo
Browse files Browse the repository at this point in the history
  • Loading branch information
dengfuping committed Nov 20, 2024
1 parent 51b94f5 commit b6cda65
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 6 deletions.
2 changes: 0 additions & 2 deletions .dumirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,11 @@ export default defineConfig({
alias: {
'@oceanbase/design': path.join(__dirname, 'packages/design/src'),
'@oceanbase/design/es': path.join(__dirname, 'packages/design/src'),
'@oceanbase/design/locale': path.join(__dirname, 'packages/design/src/locale'),
// for @import in less
'~@oceanbase/design/es': path.join(__dirname, 'packages/design/src'),
'@oceanbase/icons': path.join(__dirname, 'packages/icons/src'),
'@oceanbase/ui': path.join(__dirname, 'packages/ui/src'),
'@oceanbase/ui/es': path.join(__dirname, 'packages/ui/src'),
'@oceanbase/ui/locale': path.join(__dirname, 'packages/ui/src/locale'),
'@oceanbase/charts': path.join(__dirname, 'packages/charts/src'),
'@oceanbase/charts/es': path.join(__dirname, 'packages/charts/src'),
'@oceanbase/util': path.join(__dirname, 'packages/util/src'),
Expand Down
2 changes: 1 addition & 1 deletion packages/design/src/date-picker/demo/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const onChange: DatePickerProps['onChange'] = (date, dateString) => {
};

const App: React.FC = () => (
<Space direction="vertical">
<Space direction="vertical" size={12}>
<DatePicker onChange={onChange} />
<DatePicker onChange={onChange} showTime />
<DatePicker onChange={onChange} picker="week" />
Expand Down
39 changes: 39 additions & 0 deletions packages/design/src/date-picker/demo/format.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React from 'react';
import { DatePicker, Space } from '@oceanbase/design';
import type { DatePickerProps } from '@oceanbase/design';
import dayjs from 'dayjs';
import customParseFormat from 'dayjs/plugin/customParseFormat';

dayjs.extend(customParseFormat);

const { RangePicker } = DatePicker;

const dateFormat = 'YYYY/MM/DD';
const weekFormat = 'MM/DD';
const monthFormat = 'YYYY/MM';

const dateFormatList = ['DD/MM/YYYY', 'DD/MM/YY', 'DD-MM-YYYY', 'DD-MM-YY'];

const customFormat: DatePickerProps['format'] = value =>
`custom format: ${value.format(dateFormat)}`;

const customWeekStartEndFormat: DatePickerProps['format'] = value =>
`${dayjs(value).startOf('week').format(weekFormat)} ~ ${dayjs(value)
.endOf('week')
.format(weekFormat)}`;

const App: React.FC = () => (
<Space direction="vertical" size={12}>
<DatePicker defaultValue={dayjs('2015/01/01', dateFormat)} format={dateFormat} />
<DatePicker defaultValue={dayjs('01/01/2015', dateFormatList[0])} format={dateFormatList} />
<DatePicker defaultValue={dayjs('2015/01', monthFormat)} format={monthFormat} picker="month" />
<DatePicker defaultValue={dayjs()} format={customWeekStartEndFormat} picker="week" />
<RangePicker
defaultValue={[dayjs('2015/01/01', dateFormat), dayjs('2015/01/01', dateFormat)]}
format={dateFormat}
/>
<DatePicker defaultValue={dayjs('2015/01/01', dateFormat)} format={customFormat} />
</Space>
);

export default App;
78 changes: 78 additions & 0 deletions packages/design/src/date-picker/demo/global-format.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import React, { useState } from 'react';
import { ConfigProvider, DatePicker, Radio, Space, Switch } from '@oceanbase/design';
import type { DatePickerProps } from '@oceanbase/design';
import enUS from '@oceanbase/design/locale/en-US';
import zhCN from '@oceanbase/design/locale/zh-CN';
import dayjs from 'dayjs';

const App: React.FC = () => {
const [locale, setLocale] = useState('en-US');
const [timezone, setTimezone] = useState(false);

const onChange: DatePickerProps['onChange'] = (_, dateStr) => {
console.log('onChange:', dateStr);
};

const timezoneFormat = (format: string) => {
return timezone ? `${format} (UTC+8)` : format;
};

const enUSLocale: typeof enUS = {
...enUS,
DatePicker: {
...enUS.DatePicker!,
lang: {
...enUS.DatePicker?.lang,
fieldDateFormat: timezoneFormat('MM/DD/YYYY'),
fieldDateTimeFormat: timezoneFormat('MM/DD/YYYY HH:mm:ss'),
},
},
};

const zhCNLocale: typeof zhCN = {
...zhCN,
DatePicker: {
...zhCN.DatePicker!,
lang: {
...zhCN.DatePicker?.lang,
fieldDateFormat: timezoneFormat('YYYY-MM-DD'),
fieldDateTimeFormat: timezoneFormat('YYYY-MM-DD HH:mm:ss'),
},
},
};

return (
<Space direction="vertical" size={12}>
<Space>
locale:
<Radio.Group
value={locale}
onChange={e => {
setLocale(e.target.value);
}}
>
<Radio value="en-US">en-US</Radio>
<Radio value="zh-CN">zh-CN</Radio>
</Radio.Group>
</Space>
<Space>
timezone:
<Switch
size="small"
value={timezone}
onChange={value => {
setTimezone(value);
}}
/>
</Space>
<ConfigProvider locale={locale === 'en-US' ? enUSLocale : zhCNLocale}>
<Space direction="vertical">
<DatePicker defaultValue={dayjs('2024-01-01')} onChange={onChange} />
<DatePicker defaultValue={dayjs('2024-01-01')} onChange={onChange} showTime />
</Space>
</ConfigProvider>
</Space>
);
};

export default App;
2 changes: 2 additions & 0 deletions packages/design/src/date-picker/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ demo:
<!-- prettier-ignore -->
<code src="./demo/basic.tsx" title="基本"></code>
<code src="./demo/range-picker.tsx" title="范围选择器"></code>
<code src="./demo/format.tsx" title="日期格式" description="通过 `format` 属性进行设置,支持数组。"></code>
<code src="./demo/global-format.tsx" title="全局设置日期格式" description="通过 ConfigProvider `locale` 属性进行设置。"></code>

## API

Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/locale/en-US.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import enUS from '@oceanbase/design/es/locale/en-US';
import enUS from '@oceanbase/design/locale/en-US';
import BasicLayout from '../BasicLayout/locale/en-US';
import BatchOperationBar from '../BatchOperationBar/locale/en-US';
import Boundary from '../Boundary/locale/en-US';
Expand Down
2 changes: 0 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@
"@@/*": [".dumi/tmp/*"],
"@oceanbase/design": ["packages/design/src/index.ts"],
"@oceanbase/design/es/*": ["packages/design/src/*"],
"@oceanbase/design/locale/*": ["packages/design/src/locale/*"],
"@oceanbase/icons": ["packages/icons/src/index.ts"],
"@oceanbase/icons/es/*": ["packages/icons/src/*"],
"@oceanbase/ui": ["packages/ui/src/index.ts"],
"@oceanbase/ui/es/*": ["packages/ui/src/*"],
"@oceanbase/ui/locale/*": ["packages/ui/src/locale/*"],
"@oceanbase/charts": ["packages/charts/src/index.ts"],
"@oceanbase/charts/es/*": ["packages/charts/src/*"],
"@oceanbase/util": ["packages/util/src/index.ts"]
Expand Down

0 comments on commit b6cda65

Please sign in to comment.