2020-08-06 15:10:00 +08:00
|
|
|
---
|
|
|
|
order: 1.1
|
|
|
|
title:
|
|
|
|
zh-CN: 切换不同的选择器
|
|
|
|
en-US: Switchable picker
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
提供选择器,自由切换不同类型的日期选择器,常用于日期筛选场合。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
Switch in different types of pickers by Select.
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
```tsx
|
2022-05-21 22:14:15 +08:00
|
|
|
import type { DatePickerProps, TimePickerProps } from 'antd';
|
|
|
|
import { DatePicker, Select, Space, TimePicker } from 'antd';
|
2020-08-06 15:10:00 +08:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
|
|
|
|
const { Option } = Select;
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
type PickerType = 'time' | 'date';
|
|
|
|
|
|
|
|
const PickerWithType = ({
|
|
|
|
type,
|
|
|
|
onChange,
|
|
|
|
}: {
|
|
|
|
type: PickerType;
|
|
|
|
onChange: TimePickerProps['onChange'] | DatePickerProps['onChange'];
|
|
|
|
}) => {
|
2020-08-06 15:10:00 +08:00
|
|
|
if (type === 'time') return <TimePicker onChange={onChange} />;
|
|
|
|
if (type === 'date') return <DatePicker onChange={onChange} />;
|
|
|
|
return <DatePicker picker={type} onChange={onChange} />;
|
2022-05-19 09:46:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const App: React.FC = () => {
|
|
|
|
const [type, setType] = useState<PickerType>('time');
|
2020-08-06 15:10:00 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Space>
|
|
|
|
<Select value={type} onChange={setType}>
|
|
|
|
<Option value="time">Time</Option>
|
|
|
|
<Option value="date">Date</Option>
|
|
|
|
<Option value="week">Week</Option>
|
|
|
|
<Option value="month">Month</Option>
|
|
|
|
<Option value="quarter">Quarter</Option>
|
|
|
|
<Option value="year">Year</Option>
|
|
|
|
</Select>
|
|
|
|
<PickerWithType type={type} onChange={value => console.log(value)} />
|
|
|
|
</Space>
|
|
|
|
);
|
2022-05-19 09:46:26 +08:00
|
|
|
};
|
2020-08-06 15:10:00 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
export default App;
|
2020-08-06 15:10:00 +08:00
|
|
|
```
|