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
|
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;
|
|
|
|
|
|
|
|
class ControlledDatePicker extends React.Component {
|
|
|
|
state = { mode: 'time' };
|
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
handleOpenChange = open => {
|
2017-08-21 21:15:44 +08:00
|
|
|
if (open) {
|
|
|
|
this.setState({ mode: 'time' });
|
|
|
|
}
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2017-08-21 21:15:44 +08:00
|
|
|
|
|
|
|
handlePanelChange = (value, mode) => {
|
|
|
|
this.setState({ mode });
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2017-08-21 21:15:44 +08:00
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<DatePicker
|
|
|
|
mode={this.state.mode}
|
|
|
|
showTime
|
|
|
|
onOpenChange={this.handleOpenChange}
|
|
|
|
onPanelChange={this.handlePanelChange}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ControlledRangePicker extends React.Component {
|
|
|
|
state = {
|
|
|
|
mode: ['month', 'month'],
|
|
|
|
value: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
handlePanelChange = (value, mode) => {
|
|
|
|
this.setState({
|
|
|
|
value,
|
2019-05-07 14:57:32 +08:00
|
|
|
mode: [mode[0] === 'date' ? 'month' : mode[0], mode[1] === 'date' ? 'month' : mode[1]],
|
2017-08-21 21:15:44 +08:00
|
|
|
});
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2017-08-21 21:15:44 +08:00
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
handleChange = value => {
|
2019-02-14 17:50:37 +08:00
|
|
|
this.setState({ value });
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2019-02-14 17:50:37 +08:00
|
|
|
|
2017-08-21 21:15:44 +08:00
|
|
|
render() {
|
|
|
|
const { value, mode } = this.state;
|
|
|
|
return (
|
|
|
|
<RangePicker
|
|
|
|
placeholder={['Start month', 'End month']}
|
|
|
|
format="YYYY-MM"
|
|
|
|
value={value}
|
|
|
|
mode={mode}
|
2019-02-14 17:50:37 +08:00
|
|
|
onChange={this.handleChange}
|
2017-08-21 21:15:44 +08:00
|
|
|
onPanelChange={this.handlePanelChange}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReactDOM.render(
|
2020-07-30 19:17:34 +08:00
|
|
|
<Space direction="vertical" size={12}>
|
2017-08-21 21:15:44 +08:00
|
|
|
<ControlledDatePicker />
|
|
|
|
<ControlledRangePicker />
|
2020-07-30 19:17:34 +08:00
|
|
|
</Space>,
|
2019-05-07 14:57:32 +08:00
|
|
|
mountNode,
|
2018-11-28 15:00:03 +08:00
|
|
|
);
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|