You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
3.6 KiB
128 lines
3.6 KiB
5 years ago
|
---
|
||
3 years ago
|
order: 7.5
|
||
2 years ago
|
title: 使用自定义日期库
|
||
5 years ago
|
---
|
||
|
|
||
2 years ago
|
Ant Design 默认使用 [Day.js](https://day.js.org) 来处理时间日期问题。Day.js 相比于 moment 使用了不可变数据结构,性能更快,体积仅 2KB,API 设计完全一致。你可以很方便的改用其他自定义日期库如([moment](http://momentjs.com/)、[date-fns](https://date-fns.org))。在这里我们提供了两种方式来实现替换:
|
||
5 years ago
|
|
||
4 years ago
|
## 自定义组件
|
||
5 years ago
|
|
||
|
第一种方法是使用 `generatePicker`(或 `generateCalendar`)辅助创建 Picker 组件。
|
||
|
|
||
|
我们先初始化一个 `create-react-app` 的 antd demo,你可以参考 [在 TypeScript 中使用](/docs/react/use-in-typescript) 进行构建,也可以直接从这里开始[init antd](https://github.com/xiaohuoni/antd4-generate-picker/commit/47fec964e36d48bd15760f8f5abcb9655c259aa6)
|
||
|
|
||
4 years ago
|
### DatePicker.tsx
|
||
5 years ago
|
|
||
|
新建 `src/components/DatePicker.tsx`。
|
||
|
|
||
|
编写如下代码:
|
||
|
|
||
|
```tsx
|
||
2 years ago
|
import { Moment } from 'moment';
|
||
|
import momentGenerateConfig from 'rc-picker/es/generate/moment';
|
||
5 years ago
|
import generatePicker from 'antd/es/date-picker/generatePicker';
|
||
|
|
||
2 years ago
|
const DatePicker = generatePicker<Moment>(momentGenerateConfig);
|
||
5 years ago
|
|
||
|
export default DatePicker;
|
||
|
```
|
||
|
|
||
4 years ago
|
### TimePicker.tsx
|
||
5 years ago
|
|
||
|
新建 `src/components/TimePicker.tsx`。
|
||
|
|
||
|
编写如下代码:
|
||
|
|
||
|
```tsx
|
||
2 years ago
|
import { Moment } from 'moment';
|
||
5 years ago
|
import * as React from 'react';
|
||
|
import DatePicker from './DatePicker';
|
||
|
import { PickerTimeProps } from 'antd/es/date-picker/generatePicker';
|
||
|
|
||
2 years ago
|
export interface TimePickerProps extends Omit<PickerTimeProps<Moment>, 'picker'> {}
|
||
5 years ago
|
|
||
|
const TimePicker = React.forwardRef<any, TimePickerProps>((props, ref) => {
|
||
|
return <DatePicker {...props} picker="time" mode={undefined} ref={ref} />;
|
||
|
});
|
||
|
|
||
|
TimePicker.displayName = 'TimePicker';
|
||
|
|
||
|
export default TimePicker;
|
||
|
```
|
||
|
|
||
4 years ago
|
### Calendar.tsx
|
||
5 years ago
|
|
||
|
新建 `src/components/Calendar.tsx`。
|
||
|
|
||
|
编写如下代码:
|
||
|
|
||
|
```tsx
|
||
2 years ago
|
import { Moment } from 'moment';
|
||
|
import momentGenerateConfig from 'rc-picker/es/generate/moment';
|
||
5 years ago
|
import generateCalendar from 'antd/es/calendar/generateCalendar';
|
||
|
|
||
2 years ago
|
const Calendar = generateCalendar<Moment>(momentGenerateConfig);
|
||
5 years ago
|
|
||
|
export default Calendar;
|
||
|
```
|
||
|
|
||
|
#### 导出自定义组件
|
||
|
|
||
|
新建 `src/components/index.tsx`。
|
||
|
|
||
|
编写如下代码:
|
||
|
|
||
|
```tsx
|
||
|
export { default as DatePicker } from './DatePicker';
|
||
|
export { default as Calendar } from './Calendar';
|
||
|
export { default as TimePicker } from './TimePicker';
|
||
|
```
|
||
|
|
||
4 years ago
|
### 使用自定义组件
|
||
5 years ago
|
|
||
2 years ago
|
修改 `src/App.tsx`,引入 `moment` 和自定义的组件。
|
||
5 years ago
|
|
||
|
```diff
|
||
|
- import { DatePicker, Calendar } from 'antd';
|
||
2 years ago
|
- import format from 'dayjs';
|
||
5 years ago
|
|
||
|
+ import { DatePicker, TimePicker, Calendar } from './components';
|
||
2 years ago
|
+ import format from 'moment';
|
||
5 years ago
|
```
|
||
|
|
||
2 years ago
|
## antd-moment-webpack-plugin
|
||
5 years ago
|
|
||
2 years ago
|
我们还提供另一种实现方式。使用 `@ant-design/moment-webpack-plugin` 插件,无需对现有代码做任何修改直接替换成 `Moment.js`。请参考 [@ant-design/moment-webpack-plugin](https://github.com/ant-design/antd-moment-webpack-plugin)。
|
||
4 years ago
|
|
||
4 years ago
|
```js
|
||
|
// webpack-config.js
|
||
2 years ago
|
import AntdMomentWebpackPlugin from '@ant-design/moment-webpack-plugin';
|
||
4 years ago
|
|
||
|
module.exports = {
|
||
|
// ...
|
||
2 years ago
|
plugins: [new AntdMomentWebpackPlugin()],
|
||
4 years ago
|
};
|
||
|
```
|
||
|
|
||
4 years ago
|
## 使用 date-fns
|
||
|
|
||
4 years ago
|
[date-fns](https://date-fns.org/) 目前支持和 dayjs 类似的自定义组件方法,区别在于使用的参数类型不同,在 antd 4.5.0 以上版本提供支持。
|
||
4 years ago
|
|
||
|
做一个简单的例子:
|
||
|
|
||
|
### DatePicker.tsx
|
||
|
|
||
|
新建 `src/components/DatePicker.tsx`。
|
||
|
|
||
|
编写如下代码:
|
||
|
|
||
|
```tsx
|
||
4 years ago
|
import dateFnsGenerateConfig from 'rc-picker/es/generate/dateFns';
|
||
4 years ago
|
import generatePicker from 'antd/es/date-picker/generatePicker';
|
||
|
import 'antd/es/date-picker/style/index';
|
||
|
|
||
|
const DatePicker = generatePicker<Date>(dateFnsGenerateConfig);
|
||
|
|
||
|
export default DatePicker;
|
||
|
```
|