【问题标题】:create an all day event by drag and drop通过拖放创建全天事件
【发布时间】:2019-11-17 06:32:42
【问题描述】:

我使用react-big-calendar 来安排可用性和不可用性。我可以通过单击相应的日期单元格来创建一天的时间表。但是,我的要求是通过拖放方法创建计划。就像如果我无法从 Nov17 - Nov25 获得,那么我应该能够从 Nov17 and drop to Nov25 拖动以将那些日子创建为我无法获得的事件。

我无法完成它。

这就是我所做的

https://codesandbox.io/s/upbeat-engelbart-sfsnh

这里是代码

import React from "react";
import { Calendar, momentLocalizer } from "react-big-calendar";
import moment from "moment";
import withDragAndDrop from "react-big-calendar/lib/addons/dragAndDrop";
import "react-big-calendar/lib/css/react-big-calendar.css";
import "react-big-calendar/lib/addons/dragAndDrop/styles.css";

const DnDCalendar = withDragAndDrop(Calendar);

const localizer = momentLocalizer(moment);

const events = [
  {
    id: 0,
    title: "All Day Event very long title",
    allDay: true,
    start: new Date(2015, 3, 0),
    end: new Date(2015, 3, 1)
  },
  {
    id: 1,
    title: "Long Event",
    start: new Date(2015, 3, 7),
    end: new Date(2015, 3, 10)
  },

  {
    id: 2,
    title: "DTS STARTS",
    start: new Date(2016, 2, 13, 0, 0, 0),
    end: new Date(2016, 2, 20, 0, 0, 0)
  },

  {
    id: 3,
    title: "DTS ENDS",
    start: new Date(2016, 10, 6, 0, 0, 0),
    end: new Date(2016, 10, 13, 0, 0, 0),
    desc: "Description is shown here"
  },

  {
    id: 4,
    title: "Leave",
    start: new Date(new Date().setHours(new Date().getHours() - 3)),
    end: new Date(new Date().setHours(new Date().getHours() + 3)),
    desc: "Description is shown here"
  }
];

const onEventDrop = ({ event, start, end, allDay }) => {
  console.log("event clicked");
  console.log(start, event, end, allDay);
};

const Scheduler = () => {
  return (
    <>
      <div className="wrapper" style={{ minHeight: "100vh" }}>
        <DnDCalendar
          selectable
          events={events}
          startAccessor="start"
          endAccessor="end"
          defaultDate={moment().toDate()}
          localizer={localizer}
          toolbar
          resizable
          onEventDrop={onEventDrop}
          components={{
            event: EventComponent,
            agenda: {
              event: EventAgenda
            }
          }}
          onSelectSlot={() => console.log("selected")}
          onSelectEvent={event => alert(event.desc)}
        />
      </div>
    </>
  );
};

export default Scheduler;

const EventComponent = ({ start, end, title }) => {
  return (
    <>
      <p>{title}</p>
      <p>{start}</p>
      <p>{end}</p>
    </>
  );
};

const EventAgenda = ({ event }) => {
  return (
    <span>
      <em style={{ color: "magenta" }}>{event.title}</em>
      <p>{event.desc}</p>
    </span>
  );
};

【问题讨论】:

    标签: javascript reactjs react-big-calendar


    【解决方案1】:

    由于你的 events 数组是动态的,它不应该存储在静态变量中,而是作为 props 传递给组件或存储在组件的状态中,这样每次更新数组都会生成重新渲染.

    我已经在您的代码沙箱中分叉了一个工作示例:https://codesandbox.io/s/gifted-lehmann-xinxc

    这里是主要变化(使用 React.useState 钩子在组件状态中存储动态事件数组):

    const Scheduler = () => {
      const [ events, setEvents ] = React.useState(initialEvents);
    
      const addEvent = ({ event, start, end, allDay }) => {
        const newEvent = {
          id: events.length,
          title: "New event",
          start: new Date(new Date(start).setHours(new Date().getHours() - 3)),
          end: new Date(new Date(end).setHours(new Date().getHours() + 3)),
          desc: "This is a new event"
        }
    
        setEvents(state => [ ...state, newEvent ]);
      };
    
      return (
        <>
          <div className="wrapper" style={{ minHeight: "100vh" }}>
            <DnDCalendar
              selectable
              events={events}
              startAccessor="start"
              endAccessor="end"
              defaultDate={moment().toDate()}
              localizer={localizer}
              toolbar
              resizable
              onEventDrop={onEventDrop}
              components={{
                event: EventComponent,
                agenda: {
                  event: EventAgenda
                }
              }}
              onSelectSlot={addEvent}
              onSelectEvent={event => alert(event.desc)}
            />
          </div>
        </>
      );
    };
    
    export default Scheduler;
    

    【讨论】:

      猜你喜欢
      • 2013-03-12
      • 2012-07-07
      • 2019-04-06
      • 2022-06-12
      • 2021-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多