React BigCalendar拖放示例不起作用

时间:2018-06-07 06:14:46

标签: reactjs react-dnd react-big-calendar

我刚刚浏览了BigCalendar拖放示例HERE。我尝试自己创建拖放的本地示例,只是为了看看拖放如何与BigCalendar一起使用。我创建了以下内容:

Dnd.js

import React from 'react'
import events from './events'
import HTML5Backend from 'react-dnd-html5-backend'
import { DragDropContext } from 'react-dnd'
import BigCalendar from 'react-big-calendar'
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.less';

const DragAndDropCalendar = withDragAndDrop(BigCalendar)

class Dnd extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      events: events,
    }

    this.moveEvent = this.moveEvent.bind(this)
  }

  moveEvent({ event, start, end }) {
    const { events } = this.state

    const idx = events.indexOf(event)
    const updatedEvent = { ...event, start, end }

    const nextEvents = [...events]
    nextEvents.splice(idx, 1, updatedEvent)

    this.setState({
      events: nextEvents,
    })

    alert(`${event.title} was dropped onto ${event.start}`)
  }

  resizeEvent = (resizeType, { event, start, end }) => {
    const { events } = this.state

    const nextEvents = events.map(existingEvent => {
      return existingEvent.id == event.id
        ? { ...existingEvent, start, end }
        : existingEvent
    })

    this.setState({
      events: nextEvents,
    })

    alert(`${event.title} was resized to ${start}-${end}`)
  }

  render() {
    return (
      <DragAndDropCalendar
        selectable
        events={this.state.events}
        onEventDrop={this.moveEvent}
        resizable
        onEventResize={this.resizeEvent}
        defaultView="week"
        defaultDate={new Date(2015, 3, 12)}
      />
    )
  }
}

export default DragDropContext(HTML5Backend)(Dnd)

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import Knight from './knight/knight';
import Square from './square/square';
import Board from './board/board';
import Dnd from './bigcalender/dnd';
import { moveKnight , observe } from './game';
import BigCalendar from 'react-big-calendar'
import SimpleDrag from './simpleDrag/simpleDrag';
import moment from 'moment';

BigCalendar.momentLocalizer(moment);

const rootEl = document.getElementById('root');

ReactDOM.render(
    <Dnd />,
    rootEl
)

日历显示所有事件都很好,问题是拖放功能不能正常工作。代码几乎是来自BigCalendar drag and drop source code来源的复制粘贴。那么为什么拖放示例在我的项目中不起作用?

我在本地创建的示例也可以看作我的回购here

3 个答案:

答案 0 :(得分:4)

默认情况下create-react-app似乎不支持LESS,因此.lessdragAndDrop的{​​{1}}导入无效。

您可以在项目中设置LESS,也可以直接导入Dnd.js文件,因为它已经存在。

要使用.css文件,请在.css文件中更改此导入...

dnd.js

对此...

import 'react-big-calendar/lib/addons/dragAndDrop/styles.less';

答案 1 :(得分:1)

检查您的样式加载器。可能并非所有样式都应用,因为拖放需要大量的CSS定位。我的项目中遇到了类似的问题。

答案 2 :(得分:-1)

 import React, { Component } from 'react';
 import moment from 'moment';

 import BigCalendar from 'react-big-calendar';

 import withDragAndDrop from "react-big-calendar/lib/addons/dragAndDrop";

 import "react-big-calendar/lib/addons/dragAndDrop/styles.css";
 import "react-big-calendar/lib/css/react-big-calendar.css";

 const DragAndDropCalendar = withDragAndDrop(BigCalendar);
 const localizer = BigCalendar.momentLocalizer(moment)

 class Calendar extends Component {
   constructor(props) {
    super(props);
    this.state = {
        events: [
            {
                start: new Date(),
                end: new Date(),
                title: "Some title",
                id: 1,
                allDay: false,
            },
            {
                start: new Date('Wed Jan 30 2019 10:06:44 GMT+0200'),
                end: new Date('Thu Jan 31 2019 10:06:44 GMT+0200'),
                title: "Some another title",
                id: 2,
                allDay: false,
            }
        ]
    };
    this.onEventDrop = this.onEventDrop.bind(this);
    this.onEventResize = this.onEventResize.bind(this);
}

onEventResize({event, start, end}){
    const { events } = this.state
    const nextEvents = events.map(existingEvent => {
      return existingEvent.id === event.id
        ? { ...existingEvent, start, end }
        : existingEvent
    })

    this.setState({
      events: nextEvents,
    })
}

onEventDrop({ event, start, end }) {
    const { events } = this.state;
    const idx = events.indexOf(event);
    const updatedEvent = { ...event, start, end };
    const nextEvents = [...events];
    nextEvents.splice(idx, 1, updatedEvent);
    this.setState({
      events: nextEvents,
    })
}


render() {
    const { events } = this.state;
    return (
        <div>
            <DragAndDropCalendar
                style={{ height: '100vh' }}
                localizer={localizer}
                culture="en-GB"
                events={events}
                defaultDate={new Date()}
                defaultView="month"
                startAccessor="start"
                endAccessor="end"
                titleAccessor="title"
                onEventResize={this.onEventResize}
                onEventDrop={this.onEventDrop}
                resizable
                selectable
            />
        </div>
    );
  }
 }

export default Calendar;