ant-design/components/date-picker/demo/mode.md
dingkang be2b0d8a6e
docs: replace class component with hooks (#35500)
* 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
2022-05-11 19:43:54 +08:00

1.5 KiB

order title debug
99
zh-CN en-US
受控面板 Controlled Panels
true

zh-CN

通过组合 modeonPanelChange 控制要展示的面板。

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>
);