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.
132 lines
3.7 KiB
132 lines
3.7 KiB
5 years ago
|
---
|
||
3 years ago
|
order: 7.5
|
||
2 years ago
|
title: Use custom date library
|
||
5 years ago
|
---
|
||
|
|
||
2 years ago
|
Bu default, Ant Design use [Day.js](https://day.js.org) to handle time and date. Day.js is an immutable date-time library alternative to Moment.js with the same API.
|
||
|
|
||
|
You might want to use another date library (**Ant design currently supports [moment](http://momentjs.com/) and [date-fns](https://date-fns.org)**). We provide two ways to customize:
|
||
5 years ago
|
|
||
4 years ago
|
## Custom component
|
||
5 years ago
|
|
||
3 years ago
|
The first way is to use `generatePicker` (or `generateCalendar`) to help create Picker components.
|
||
5 years ago
|
|
||
5 years ago
|
First, we initialize an antd demo with `create-react-app`. You can refer to [Use in TypeScript](/docs/react/use-in-typescript), or you can start directly here [init antd](https://github.com/xiaohuoni/antd4-generate-picker/commit/47fec964e36d48bd15760f8f5abcb9655c259aa6)
|
||
5 years ago
|
|
||
4 years ago
|
### DatePicker.tsx
|
||
5 years ago
|
|
||
|
Create `src/components/DatePicker.tsx`.
|
||
|
|
||
|
For example:
|
||
|
|
||
|
```tsx
|
||
2 years ago
|
import type { Moment } from 'moment';
|
||
|
import momentGenerateConfig from 'rc-picker/lib/generate/moment';
|
||
5 years ago
|
import generatePicker from 'antd/es/date-picker/generatePicker';
|
||
|
import 'antd/es/date-picker/style/index';
|
||
|
|
||
2 years ago
|
const DatePicker = generatePicker<Moment>(momentGenerateConfig);
|
||
5 years ago
|
|
||
|
export default DatePicker;
|
||
|
```
|
||
|
|
||
4 years ago
|
### TimePicker.tsx
|
||
5 years ago
|
|
||
|
Create `src/components/TimePicker.tsx`.
|
||
|
|
||
|
For example:
|
||
|
|
||
|
```tsx
|
||
2 years ago
|
import type { Moment } from 'moment';
|
||
5 years ago
|
import * as React from 'react';
|
||
2 years ago
|
import type { PickerTimeProps } from 'antd/es/date-picker/generatePicker';
|
||
5 years ago
|
import DatePicker from './DatePicker';
|
||
|
|
||
2 years ago
|
export interface TimePickerProps extends Omit<PickerTimeProps<Moment>, 'picker'> {}
|
||
5 years ago
|
|
||
2 years ago
|
const TimePicker = React.forwardRef<any, TimePickerProps>((props, ref) => (
|
||
|
<DatePicker {...props} picker="time" mode={undefined} ref={ref} />
|
||
|
));
|
||
5 years ago
|
|
||
|
TimePicker.displayName = 'TimePicker';
|
||
|
|
||
|
export default TimePicker;
|
||
|
```
|
||
|
|
||
4 years ago
|
### Calendar.tsx
|
||
5 years ago
|
|
||
|
Create `src/components/Calendar.tsx`.
|
||
|
|
||
|
For example:
|
||
|
|
||
|
```tsx
|
||
2 years ago
|
import type { Moment } from 'moment';
|
||
|
import momentGenerateConfig from 'rc-picker/lib/generate/moment';
|
||
5 years ago
|
import generateCalendar from 'antd/es/calendar/generateCalendar';
|
||
|
import 'antd/es/calendar/style';
|
||
|
|
||
2 years ago
|
const Calendar = generateCalendar<Moment>(momentGenerateConfig);
|
||
5 years ago
|
|
||
|
export default Calendar;
|
||
|
```
|
||
|
|
||
4 years ago
|
### Export Custom component
|
||
5 years ago
|
|
||
|
Create `src/components/index.tsx`.
|
||
|
|
||
|
For example:
|
||
|
|
||
|
```tsx
|
||
|
export { default as DatePicker } from './DatePicker';
|
||
|
export { default as Calendar } from './Calendar';
|
||
|
export { default as TimePicker } from './TimePicker';
|
||
|
```
|
||
|
|
||
4 years ago
|
### Use Custom component
|
||
5 years ago
|
|
||
2 years ago
|
Modify `src/App.tsx`,import `moment` and custom component.
|
||
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
|
We also provide another implementation, which we provide with `@ant-design/moment-webpack-plugin`, replacing `Day.js` with `moment` directly without changing a line of existing code. More info can be found at [@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
|
## Use date-fns
|
||
|
|
||
4 years ago
|
[date-fns](https://date-fns.org/) currently supports custom component methods similar to `dayjs`. The difference is that the parameter types used are different. Support is provided in antd 4.5.0 and above.
|
||
4 years ago
|
|
||
|
For Example:
|
||
|
|
||
|
### DatePicker.tsx
|
||
|
|
||
|
Create `src/components/DatePicker.tsx`.
|
||
|
|
||
|
Code as follows:
|
||
|
|
||
|
```tsx
|
||
|
import dateFnsGenerateConfig from 'rc-picker/lib/generate/dateFns';
|
||
|
import generatePicker from 'antd/es/date-picker/generatePicker';
|
||
|
import 'antd/es/date-picker/style/index';
|
||
|
|
||
|
const DatePicker = generatePicker<Date>(dateFnsGenerateConfig);
|
||
|
|
||
|
export default DatePicker;
|
||
|
```
|