mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-16 18:09:22 +08:00
5a08aab0e8
* docs: DatePicker change type demo * chore: snapshot * chore * docs: DatePicker use Space * chore: snapshot
87 lines
1.6 KiB
Markdown
87 lines
1.6 KiB
Markdown
---
|
|
order: 99
|
|
title:
|
|
zh-CN: 受控面板
|
|
en-US: Controlled Panels
|
|
debug: true
|
|
---
|
|
|
|
## zh-CN
|
|
|
|
通过组合 `mode` 与 `onPanelChange` 控制要展示的面板。
|
|
|
|
## en-US
|
|
|
|
Determing which panel to show with `mode` and `onPanelChange`.
|
|
|
|
```jsx
|
|
import { DatePicker, Space } from 'antd';
|
|
|
|
const { RangePicker } = DatePicker;
|
|
|
|
class ControlledDatePicker extends React.Component {
|
|
state = { mode: 'time' };
|
|
|
|
handleOpenChange = open => {
|
|
if (open) {
|
|
this.setState({ mode: 'time' });
|
|
}
|
|
};
|
|
|
|
handlePanelChange = (value, mode) => {
|
|
this.setState({ mode });
|
|
};
|
|
|
|
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,
|
|
mode: [mode[0] === 'date' ? 'month' : mode[0], mode[1] === 'date' ? 'month' : mode[1]],
|
|
});
|
|
};
|
|
|
|
handleChange = value => {
|
|
this.setState({ value });
|
|
};
|
|
|
|
render() {
|
|
const { value, mode } = this.state;
|
|
return (
|
|
<RangePicker
|
|
placeholder={['Start month', 'End month']}
|
|
format="YYYY-MM"
|
|
value={value}
|
|
mode={mode}
|
|
onChange={this.handleChange}
|
|
onPanelChange={this.handlePanelChange}
|
|
/>
|
|
);
|
|
}
|
|
}
|
|
|
|
ReactDOM.render(
|
|
<Space direction="vertical" size={12}>
|
|
<ControlledDatePicker />
|
|
<ControlledRangePicker />
|
|
</Space>,
|
|
mountNode,
|
|
);
|
|
```
|