This repository has been archived by the owner on Dec 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#905 Dashboard edit mode (wip) - refactor + placeholder for target in…
…dicators + dnd widget wrapper
- Loading branch information
1 parent
1496ca4
commit b4f2625
Showing
11 changed files
with
379 additions
and
313 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
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
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,87 @@ | ||
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import ItemTypes from '../../constants/ItemTypes'; | ||
import { DragSource, DropTarget } from 'react-dnd'; | ||
|
||
const cardSource = { | ||
beginDrag(props) { | ||
return { | ||
id: props.id, | ||
index: props.index | ||
}; | ||
} | ||
}; | ||
|
||
const cardTarget = { | ||
hover(props, monitor) { | ||
const dragIndex = monitor.getItem().index; | ||
const hoverIndex = props.index; | ||
|
||
// Don't replace items with themselves | ||
if (dragIndex === hoverIndex) { | ||
return; | ||
} | ||
|
||
// Time to actually perform the action | ||
props.moveCard(props.entity, dragIndex, hoverIndex); | ||
|
||
// Note: we're mutating the monitor item here! | ||
// Generally it's better to avoid mutations, | ||
// but it's good here for the sake of performance | ||
// to avoid expensive index searches. | ||
monitor.getItem().index = hoverIndex; | ||
} | ||
}; | ||
|
||
function collect(connect, monitor) { | ||
return { | ||
connectDragSource: connect.dragSource(), | ||
isDragging: monitor.isDragging() | ||
}; | ||
} | ||
|
||
function connect(connect) { | ||
return { | ||
connectDropTarget: connect.dropTarget() | ||
}; | ||
} | ||
|
||
export class DndWidget extends Component { | ||
constructor(props) { | ||
super(props); | ||
} | ||
|
||
render() { | ||
const { | ||
children, connectDragSource, connectDropTarget, isDragging, | ||
className, transparent, removeCard, entity, id, placeholder | ||
} = this.props; | ||
|
||
if(transparent) return <div {...{className}}>{children}</div>; | ||
|
||
return connectDragSource(connectDropTarget( | ||
<div | ||
className={ | ||
className + ' dnd-widget ' + | ||
(isDragging ? 'dragging ' : '') + | ||
(placeholder ? 'dnd-placeholder ' : '') | ||
} | ||
> | ||
{!placeholder && <i | ||
className="meta-icon-trash draggable-icon-remove" | ||
onClick={() => removeCard(entity, id)} | ||
/>} | ||
{children} | ||
</div> | ||
)); | ||
} | ||
} | ||
|
||
DndWidget = | ||
DragSource(props => props.entity, cardSource, collect)( | ||
DropTarget(props => props.entity, cardTarget, connect)( | ||
DndWidget | ||
) | ||
); | ||
|
||
export default DndWidget; |
Oops, something went wrong.