【问题标题】:Need to create custom navigation header for calendar需要为日历创建自定义导航标题
【发布时间】:2019-11-18 05:45:09
【问题描述】:

我有一个 Antd 日历,我正在使用它作为一个更大的 React 项目的一部分。该组件将从 API 响应中加载数据,并将一组日期和时间 AM, PM, ANY(每个都有自己的唯一 ID)映射到日历上的日期,以便选择日期/时间,例如 07/12/2019PM

开箱即用,用于选择月份和年份的默认标题样式类似于 this

我正在尝试更改这些以包含一个自定义标题,该标题仅列出月份名称和年份(例如:July 2019)以及用于在月份之间导航的左右 V 形。

我已经加入了 V 形,但是在实现导航功能以及如何呈现当前月份和年份的名称时,我完全不知所措。

我在这里添加了一个 CodeSandbox 来显示我当前的进度

My Calendar Component

import React from "react";
import { Calendar, Select, Radio, Col, Row } from "antd";
import { FaChevronRight, FaChevronLeft } from "react-icons/fa";
import styled from "styled-components";

const { Group, Button } = Radio;

function onPanelChange(value, mode) {
  console.log(value, mode);
}

const StylingCalendar = props => (
  <div style={{ width: 300, border: "1px solid #d9d9d9", borderRadius: 4 }}>
    <Calendar
      fullscreen={false}
      headerRender={({ value, type, onChange, onTypeChange }) => {
        const start = 0;
        const end = 12;
        const monthOptions = [];

        const current = value.clone();
        const localeData = value.localeData();
        const months = [];
        for (let i = 0; i < 12; i++) {
          current.month(i);
          months.push(localeData.monthsShort(current));
        }

        for (let index = start; index < end; index++) {
          monthOptions.push(
            <Select.Option className="month-item" key={`${index}`}>
              {months[index]}
            </Select.Option>
          );
        }
        const month = value.month();

        const year = value.year();
        const options = [];
        for (let i = year - 10; i < year + 10; i += 1) {
          options.push(
            <Select.Option key={i} value={i} className="year-item">
              {i}
            </Select.Option>
          );
        }
        return (
          <div style={{ padding: 10 }}>
            <div style={{ marginBottom: "10px" }}>{monthOptions.name}</div>
            <Row type="flex" justify="space-between">
              {/* <Col>
                <Group
                  size="small"
                  onChange={e => onTypeChange(e.target.value)}
                  value={type}
                >
                  <Button value="month">Month</Button>
                  <Button value="year">Year</Button>
                </Group>
              </Col> */}
              {/* <Col>
                <Select
                  size="small"
                  dropdownMatchSelectWidth={false}
                  className="my-year-select"
                  onChange={newYear => {
                    const now = value.clone().year(newYear);
                    onChange(now);
                  }}
                  value={String(year)}
                >
                  {options}
                </Select>
              </Col> */}
              <Col span={8}>
                <LeftArrow onClick={null} />
              </Col>
              <Col span={8}>
                {/* <FaChevronLeft /> */}
                <Select
                  size="small"
                  dropdownMatchSelectWidth={false}
                  value={String(month)}
                  onChange={selectedMonth => {
                    const newValue = value.clone();
                    newValue.month(parseInt(selectedMonth, 10));
                    onChange(newValue);
                  }}
                >
                  {monthOptions}
                </Select>
              </Col>
              <Col span={8}>
                <RightArrow onClick={null} />
              </Col>
            </Row>
          </div>
        );
      }}
      onPanelChange={onPanelChange}
    />
  </div>
);

const RightArrow = styled(FaChevronRight)`
  cursor: pointer;
`;

const LeftArrow = styled(FaChevronLeft)`
  cursor: pointer;
`;

export default StylingCalendar;

【问题讨论】:

  • 您的组件是无状态的,您打算如何更改当前月份的状态?尝试添加当前时刻状态
  • 而且,你太努力了,这个组件的目的是什么?你想选择一个日期吗?还是只是展示一个日历?
  • 第 1 部分是显示日历,第 2 部分是显示所选日期以及下拉菜单以选择日历右侧的 AM, PM, ANY 时间
  • 那么您是要安排约会吗?请尝试在您的问题中添加您的组件用途,因为我认为您做得过火了,我可以向您展示一个简单的解决方法
  • 嗯,基本上,我正在尝试安排日期和时间,但整个组件将从 API 调用加载 JSON 响应并附加一组 3 次:AM, PM, ANY 每个都有他们的该响应中的日期集拥有自己的唯一 ID。组件的目的比我的问题要深刻得多,我只是想先敲掉日历的初始样式和导航

标签: javascript reactjs momentjs antd


【解决方案1】:

我也遇到了同样的问题。如果您使用日历组件的 onChange 属性,这非常容易。 更改月份的按钮代码如下:

`<Button
   onClick={() => {
      const newValue = moment(value).subtract(1, 'M')
      onChange(newValue)
   }
>
{'last month'}
</Button>`

【讨论】:

    【解决方案2】:

    尝试使用DatePickershowTime 功能,检查example,按下select time

    <div>
      <DatePicker
        showTime
        placeholder="Select Time"
        onChange={onChange}
        onOk={onOk}
      />
    </div>
    

    此外,如果您使用 open 属性,您将获得您想要实现的组件:

    <DatePicker open showTime placeholder="Select Time" />
    

    【讨论】:

    • 这太接近了,但现在我得想办法将左侧打开的日历和右侧的日期选择器输入显示为单独的组件
    猜你喜欢
    • 2019-11-14
    • 1970-01-01
    • 1970-01-01
    • 2019-11-01
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    相关资源
    最近更新 更多