-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dnd): add onDropFromOutside prop for Dnd Cal (#1290)
This PR is meant to resolve issue #1090. ## Basic callback for outside drops The change exposes the `onDropFromOutside` prop on the withDragAndDrop HOC, which takes a callback that fires when an outside draggable item is dropped onto the calendar. The callback receives as a parameter an object with start and end properties that are times based on the drop position and slot size. ![a4be055597d294f257d59a6fa2982f27](https://user-images.githubusercontent.com/37093582/56405067-a6036e00-6227-11e9-9274-b1846b5b0be8.gif) It is worth noting that it is entirely up to the user to handle actual event creation based on the callback. All that this API does is allow `draggable` DOM elements to trigger a callback that receives start and end times for the slot an item was dropped on, and a boolean as to whether it's an all-day event. If the user wants to know which event was dropped, they will have to handle that themselves outside of React-Big-Calendar. An example added to the example App demonstrates how this can be done. ## Optional selective dropping By default, if `onDropFromOutside` prop is passed, all draggable events are droppable on calendar. If the user wishes to discriminate as to whether draggable events are droppable on the calendar, they can pass an additional `onDragOver` callback function. The `onDragOver` callback takes a DragEvent as its sole parameter. If it calls the DragEvent's `preventDefault` method, then the draggable item in question is droppable. If it does not call `preventDefault` during the function call, it will not be droppable. ![e374b60b55809f471b2f275d7f166278](https://user-images.githubusercontent.com/37093582/56405161-3b9efd80-6228-11e9-9b0b-2c925f371eb1.gif) An example was also added to the examples App, this one labelled `Addon: Drag and Drop (from outside calendar). The GIFs show this example in action. I also added the following comments into the withDragAndDrop HOC by way of documentation. ``` * Additionally, this HOC adds the callback props `onDropFromOutside` and `onDragOver`. * By default, the calendar will not respond to outside draggable items being dropped * onto it. However, if `onDropFromOutside` callback is passed, then when draggable * DOM elements are dropped on the calendar, the callback will fire, receiving an * object with start and end times, and an allDay boolean. * * If `onDropFromOutside` is passed, but `onDragOver` is not, any draggable event will be * droppable onto the calendar by default. On the other hand, if an `onDragOver` callback * *is* passed, then it can discriminate as to whether a draggable item is droppable on the * calendar. To designate a draggable item as droppable, call `event.preventDefault` * inside `onDragOver`. If `event.preventDefault` is not called in the `onDragOver` * callback, then the draggable item will not be droppable on the calendar. ``` Hopefully this gives users the flexibility they need, without getting react-big-calendar overly involved with managing outside drag and drop scenarios. Any feedback/discussion/harangues are welcome!
- Loading branch information
1 parent
0fa2c30
commit b9fdce4
Showing
6 changed files
with
289 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
import React from 'react' | ||
import events from '../events' | ||
import BigCalendar from 'react-big-calendar' | ||
import withDragAndDrop from 'react-big-calendar/lib/addons/dragAndDrop' | ||
import Layout from 'react-tackle-box/Layout' | ||
import Card from '../Card' | ||
|
||
import 'react-big-calendar/lib/addons/dragAndDrop/styles.less' | ||
|
||
const DragAndDropCalendar = withDragAndDrop(BigCalendar) | ||
|
||
const formatName = (name, count) => `${name} ID ${count}` | ||
|
||
class Dnd extends React.Component { | ||
constructor(props) { | ||
super(props) | ||
this.state = { | ||
events: events, | ||
draggedEvent: null, | ||
counters: { | ||
item1: 0, | ||
item2: 0, | ||
}, | ||
} | ||
} | ||
|
||
handleDragStart = name => { | ||
this.setState({ draggedEvent: name }) | ||
} | ||
|
||
customOnDragOver = event => { | ||
// check for undroppable is specific to this example | ||
// and not part of API. This just demonstrates that | ||
// onDragOver can optionally be passed to conditionally | ||
// allow draggable items to be dropped on cal, based on | ||
// whether event.preventDefault is called | ||
if (this.state.draggedEvent !== 'undroppable') { | ||
console.log('preventDefault') | ||
event.preventDefault() | ||
} | ||
} | ||
|
||
onDropFromOutside = ({ start, end, allDay }) => { | ||
const { draggedEvent, counters } = this.state | ||
const event = { | ||
title: formatName(draggedEvent, counters[draggedEvent]), | ||
start, | ||
end, | ||
isAllDay: allDay, | ||
} | ||
const updatedCounters = { | ||
...counters, | ||
[draggedEvent]: counters[draggedEvent] + 1, | ||
} | ||
this.setState({ draggedEvent: null, counters: updatedCounters }) | ||
this.newEvent(event) | ||
} | ||
|
||
moveEvent({ event, start, end, isAllDay: droppedOnAllDaySlot }) { | ||
const { events } = this.state | ||
|
||
const idx = events.indexOf(event) | ||
let allDay = event.allDay | ||
|
||
if (!event.allDay && droppedOnAllDaySlot) { | ||
allDay = true | ||
} else if (event.allDay && !droppedOnAllDaySlot) { | ||
allDay = false | ||
} | ||
|
||
const updatedEvent = { ...event, start, end, allDay } | ||
|
||
const nextEvents = [...events] | ||
nextEvents.splice(idx, 1, updatedEvent) | ||
|
||
this.setState({ | ||
events: nextEvents, | ||
}) | ||
|
||
// alert(`${event.title} was dropped onto ${updatedEvent.start}`) | ||
} | ||
|
||
resizeEvent = ({ 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}`) | ||
} | ||
|
||
newEvent(event) { | ||
let idList = this.state.events.map(a => a.id) | ||
let newId = Math.max(...idList) + 1 | ||
let hour = { | ||
id: newId, | ||
title: event.title, | ||
allDay: event.isAllDay, | ||
start: event.start, | ||
end: event.end, | ||
} | ||
this.setState({ | ||
events: this.state.events.concat([hour]), | ||
}) | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<Card | ||
className="examples--header" | ||
style={{ | ||
display: 'flex', | ||
justifyContent: 'center', | ||
flexWrap: 'wrap', | ||
}} | ||
> | ||
<h4 style={{ color: 'gray', width: '100%' }}>Outside Drag Sources</h4> | ||
{Object.entries(this.state.counters).map(([name, count]) => ( | ||
<div | ||
style={{ | ||
border: '2px solid gray', | ||
borderRadius: '4px', | ||
width: '100px', | ||
margin: '10px', | ||
}} | ||
draggable="true" | ||
key={name} | ||
onDragStart={() => this.handleDragStart(name)} | ||
> | ||
{formatName(name, count)} | ||
</div> | ||
))} | ||
<div | ||
style={{ | ||
border: '2px solid gray', | ||
borderRadius: '4px', | ||
width: '100px', | ||
margin: '10px', | ||
}} | ||
draggable="true" | ||
key={name} | ||
onDragStart={() => this.handleDragStart('undroppable')} | ||
> | ||
Draggable but not for calendar. | ||
</div> | ||
</Card> | ||
<DragAndDropCalendar | ||
selectable | ||
localizer={this.props.localizer} | ||
events={this.state.events} | ||
onEventDrop={this.moveEvent} | ||
onDropFromOutside={this.onDropFromOutside} | ||
onDragOver={this.customOnDragOver} | ||
resizable | ||
onEventResize={this.resizeEvent} | ||
onSelectSlot={this.newEvent} | ||
onD | ||
defaultView={BigCalendar.Views.MONTH} | ||
defaultDate={new Date(2015, 3, 12)} | ||
/> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
export default Dnd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.