gpt4 book ai didi

reactjs - React BigCalendar 拖放示例不起作用

转载 作者:行者123 更新时间:2023-12-03 13:42:08 24 4
gpt4 key购买 nike

我刚刚浏览了 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 .

最佳答案

create-react-app 中似乎不支持 LESS默认情况下,所以 .less导入dragAndDrop在你的Dnd.js不管用。

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

要使用.css文件,在您的 dnd.js 中文件,更改此导入...

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

对此...

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

关于reactjs - React BigCalendar 拖放示例不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50734092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com