2017-08-21 21:15:44 +08:00
|
|
|
---
|
2019-12-11 23:32:19 +08:00
|
|
|
order: 99
|
2017-08-21 21:15:44 +08:00
|
|
|
title:
|
|
|
|
zh-CN: 受控面板
|
|
|
|
en-US: Controlled Panels
|
2019-12-11 23:32:19 +08:00
|
|
|
debug: true
|
2017-08-21 21:15:44 +08:00
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
通过组合 `mode` 与 `onPanelChange` 控制要展示的面板。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
Determing which panel to show with `mode` and `onPanelChange`.
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
```tsx
|
2022-05-23 14:37:16 +08:00
|
|
|
import type { DatePickerProps } from 'antd';
|
2020-07-30 19:17:34 +08:00
|
|
|
import { DatePicker, Space } from 'antd';
|
2022-05-19 09:46:26 +08:00
|
|
|
import type { RangePickerProps } from 'antd/es/date-picker';
|
|
|
|
import type { Moment } from 'moment';
|
2022-05-23 14:37:16 +08:00
|
|
|
import React, { useState } from 'react';
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2017-08-21 21:15:44 +08:00
|
|
|
const { RangePicker } = DatePicker;
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
type RangeValue = [Moment | null, Moment | null] | null;
|
2017-08-21 21:15:44 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const ControlledDatePicker = () => {
|
|
|
|
const [mode, setMode] = useState<DatePickerProps['mode']>('time');
|
2017-08-21 21:15:44 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const handleOpenChange = (open: boolean) => {
|
2017-08-21 21:15:44 +08:00
|
|
|
if (open) {
|
2022-05-11 19:43:54 +08:00
|
|
|
setMode('time');
|
2017-08-21 21:15:44 +08:00
|
|
|
}
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2017-08-21 21:15:44 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const handlePanelChange: DatePickerProps['onPanelChange'] = (_, newMode) => {
|
|
|
|
setMode(newMode);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2017-08-21 21:15:44 +08:00
|
|
|
|
2022-05-11 19:43:54 +08:00
|
|
|
return (
|
|
|
|
<DatePicker
|
|
|
|
mode={mode}
|
|
|
|
showTime
|
|
|
|
onOpenChange={handleOpenChange}
|
|
|
|
onPanelChange={handlePanelChange}
|
|
|
|
/>
|
|
|
|
);
|
2022-05-19 09:46:26 +08:00
|
|
|
};
|
2017-08-21 21:15:44 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const ControlledRangePicker = () => {
|
|
|
|
const [mode, setMode] = useState<RangePickerProps['mode']>(['month', 'month']);
|
|
|
|
const [value, setValue] = useState<RangeValue>([null, null]);
|
2017-08-21 21:15:44 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const handlePanelChange: RangePickerProps['onPanelChange'] = (newValue, newModes) => {
|
|
|
|
setValue(newValue);
|
2022-05-11 19:43:54 +08:00
|
|
|
setMode([
|
2022-05-19 09:46:26 +08:00
|
|
|
newModes[0] === 'date' ? 'month' : newModes[0],
|
|
|
|
newModes[1] === 'date' ? 'month' : newModes[1],
|
2022-05-11 19:43:54 +08:00
|
|
|
]);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2017-08-21 21:15:44 +08:00
|
|
|
|
2022-05-11 19:43:54 +08:00
|
|
|
return (
|
|
|
|
<RangePicker
|
|
|
|
placeholder={['Start month', 'End month']}
|
|
|
|
format="YYYY-MM"
|
|
|
|
value={value}
|
|
|
|
mode={mode}
|
2022-05-19 09:46:26 +08:00
|
|
|
onChange={setValue}
|
2022-05-11 19:43:54 +08:00
|
|
|
onPanelChange={handlePanelChange}
|
|
|
|
/>
|
|
|
|
);
|
2022-05-19 09:46:26 +08:00
|
|
|
};
|
2017-08-21 21:15:44 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const App: React.FC = () => (
|
2020-07-30 19:17:34 +08:00
|
|
|
<Space direction="vertical" size={12}>
|
2017-08-21 21:15:44 +08:00
|
|
|
<ControlledDatePicker />
|
|
|
|
<ControlledRangePicker />
|
2022-04-03 23:27:45 +08:00
|
|
|
</Space>
|
2018-11-28 15:00:03 +08:00
|
|
|
);
|
2022-05-19 09:46:26 +08:00
|
|
|
|
|
|
|
export default App;
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|