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`.
|
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
```jsx
|
2022-05-11 19:43:54 +08:00
|
|
|
import React, { useState } from 'react';
|
2020-07-30 19:17:34 +08:00
|
|
|
import { DatePicker, Space } from 'antd';
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2017-08-21 21:15:44 +08:00
|
|
|
const { RangePicker } = DatePicker;
|
|
|
|
|
2022-05-11 19:43:54 +08:00
|
|
|
function ControlledDatePicker() {
|
|
|
|
const [mode, setMode] = useState('time');
|
2017-08-21 21:15:44 +08:00
|
|
|
|
2022-05-11 19:43:54 +08:00
|
|
|
const handleOpenChange = open => {
|
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-11 19:43:54 +08:00
|
|
|
const handlePanelChange = (value, dateMode) => {
|
|
|
|
setMode(dateMode);
|
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}
|
|
|
|
/>
|
|
|
|
);
|
2017-08-21 21:15:44 +08:00
|
|
|
}
|
|
|
|
|
2022-05-11 19:43:54 +08:00
|
|
|
function ControlledRangePicker() {
|
|
|
|
const [mode, setMode] = useState(['month', 'month']);
|
|
|
|
const [value, setValue] = useState([]);
|
2017-08-21 21:15:44 +08:00
|
|
|
|
2022-05-11 19:43:54 +08:00
|
|
|
const handlePanelChange = (dateValue, dateMode) => {
|
|
|
|
setValue(dateValue);
|
|
|
|
setMode([
|
|
|
|
dateMode[0] === 'date' ? 'month' : dateMode[0],
|
|
|
|
dateMode[1] === 'date' ? 'month' : dateMode[1],
|
|
|
|
]);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2017-08-21 21:15:44 +08:00
|
|
|
|
2022-05-11 19:43:54 +08:00
|
|
|
const handleChange = dateValue => {
|
|
|
|
setValue(dateValue);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2019-02-14 17:50:37 +08:00
|
|
|
|
2022-05-11 19:43:54 +08:00
|
|
|
return (
|
|
|
|
<RangePicker
|
|
|
|
placeholder={['Start month', 'End month']}
|
|
|
|
format="YYYY-MM"
|
|
|
|
value={value}
|
|
|
|
mode={mode}
|
|
|
|
onChange={handleChange}
|
|
|
|
onPanelChange={handlePanelChange}
|
|
|
|
/>
|
|
|
|
);
|
2017-08-21 21:15:44 +08:00
|
|
|
}
|
|
|
|
|
2022-04-03 23:27:45 +08:00
|
|
|
export default () => (
|
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
|
|
|
);
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|