-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide onDropFromOutside prop for Dnd Cal #1290
Merged
jquense
merged 4 commits into
jquense:master
from
kamry-bowman:feature-drag-from-outside
Apr 22, 2019
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dumb question, how do you know if this is "from the outside", is it b/c for "internal" drops we don't actually use the drop/drag events?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's exactly right. I didn't really think about it that way but that's the reason. I think if we did move to drop events for the internals we would have to refactor this approach.