mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-12 15:19:58 +08:00
be2b0d8a6e
* docs(badge): replace class component with hooks * docs(button): replace class component with hooks * docs(calendar): replace class component with hooks * docs(card): replace class component with hooks * docs(button): replace class component with hooks * chore(deps): remove webpack devDependencies * docs(cascader): replace class component with hooks * docs(checkbox): replace class component with hooks * docs(collapse): replace class component with hooks * docs(comment): replace class component with hooks * docs(descriptions): replace class component with hooks * docs(config-provider): replace class component with hooks * docs(date-picker): replace class component with hooks
1.5 KiB
1.5 KiB
order | title | debug | ||||
---|---|---|---|---|---|---|
99 |
|
true |
zh-CN
通过组合 mode
与 onPanelChange
控制要展示的面板。
en-US
Determing which panel to show with mode
and onPanelChange
.
import React, { useState } from 'react';
import { DatePicker, Space } from 'antd';
const { RangePicker } = DatePicker;
function ControlledDatePicker() {
const [mode, setMode] = useState('time');
const handleOpenChange = open => {
if (open) {
setMode('time');
}
};
const handlePanelChange = (value, dateMode) => {
setMode(dateMode);
};
return (
<DatePicker
mode={mode}
showTime
onOpenChange={handleOpenChange}
onPanelChange={handlePanelChange}
/>
);
}
function ControlledRangePicker() {
const [mode, setMode] = useState(['month', 'month']);
const [value, setValue] = useState([]);
const handlePanelChange = (dateValue, dateMode) => {
setValue(dateValue);
setMode([
dateMode[0] === 'date' ? 'month' : dateMode[0],
dateMode[1] === 'date' ? 'month' : dateMode[1],
]);
};
const handleChange = dateValue => {
setValue(dateValue);
};
return (
<RangePicker
placeholder={['Start month', 'End month']}
format="YYYY-MM"
value={value}
mode={mode}
onChange={handleChange}
onPanelChange={handlePanelChange}
/>
);
}
export default () => (
<Space direction="vertical" size={12}>
<ControlledDatePicker />
<ControlledRangePicker />
</Space>
);