1- import React , { Component } from 'react' ;
2- import PropTypes from 'prop-types' ;
3- import {
4- CardHeader , CardRightContent , CardTitle , Detail , Footer ,
5- MovableCardWrapper ,
6- } from '../styles/Base' ;
7- import { DragType } from '../helpers/DragType' ;
8- import { DragSource , DropTarget } from 'react-dnd' ;
9- import { findDOMNode } from 'react-dom' ;
10- import Tag from './Tag' ;
11- import flow from 'lodash/flow' ;
12- import DeleteButton from './widgets/DeleteButton' ;
1+ import React , { Component } from 'react'
2+ import PropTypes from 'prop-types'
3+ import { CardHeader , CardRightContent , CardTitle , Detail , Footer , MovableCardWrapper } from '../styles/Base'
4+ import Tag from './Tag'
5+ import DeleteButton from './widgets/DeleteButton'
6+ import { Draggable } from 'react-beautiful-dnd'
137
148class Card extends Component {
159 removeCard = e => {
@@ -22,8 +16,7 @@ class Card extends Component {
2216 renderBody = ( ) => {
2317 if ( this . props . customCardLayout ) {
2418 const { customCard, ...otherProps } = this . props
25- const customCardWithProps = React . cloneElement ( customCard , { ...otherProps } )
26- return customCardWithProps
19+ return React . cloneElement ( customCard , { ...otherProps } )
2720 } else {
2821 const { title, description, label, tags} = this . props
2922 return (
@@ -39,94 +32,43 @@ class Card extends Component {
3932 }
4033 }
4134
42- render ( ) {
43- const { id, connectDragSource, connectDropTarget, isDragging, cardStyle, editable, customCardLayout, ...otherProps } = this . props
44- const opacity = isDragging ? 0 : 1
45- const background = isDragging ? '#CCC' : '#E3E3E3'
35+ getItemStyle = ( isDragging , draggableStyle ) => ( {
36+ backgroundColor : isDragging ? '#fbfbbc' : '#fff' ,
37+ ...draggableStyle
38+ } )
39+
40+ render ( ) {
41+ const { id, index, cardStyle, editable, customCardLayout, ...otherProps } = this . props
4642 const style = customCardLayout ? { ...cardStyle , padding : 0 } : cardStyle
47- return connectDragSource (
48- connectDropTarget (
49- < div style = { { background : background } } >
50- < MovableCardWrapper key = { id } data-id = { id } { ...otherProps } style = { { ...style , opacity : opacity } } >
51- { this . renderBody ( ) }
52- { editable && < DeleteButton onClick = { this . removeCard } /> }
53- </ MovableCardWrapper >
54- </ div >
55- )
43+ return (
44+ < Draggable key = { id } draggableId = { id } index = { index } >
45+ { ( dragProvided , dragSnapshot ) => {
46+ const dragStyle = this . getItemStyle ( dragSnapshot . isDragging , dragProvided . draggableProps . style )
47+ return (
48+ < div >
49+ < MovableCardWrapper
50+ key = { id }
51+ data-id = { id }
52+ { ...otherProps }
53+ innerRef = { dragProvided . innerRef }
54+ { ...dragProvided . draggableProps }
55+ { ...dragProvided . dragHandleProps }
56+ style = { {
57+ ...style ,
58+ ...dragStyle
59+ } } >
60+ { this . renderBody ( ) }
61+ { editable && < DeleteButton onClick = { this . removeCard } /> }
62+ </ MovableCardWrapper >
63+ { dragProvided . placeholder }
64+ </ div >
65+ )
66+ } }
67+ </ Draggable >
5668 )
5769 }
5870}
5971
60- const cardSource = {
61- canDrag ( props ) {
62- return props . draggable
63- } ,
64-
65- beginDrag ( props ) {
66- props . handleDragStart && props . handleDragStart ( props . id , props . laneId )
67- return {
68- id : props . id ,
69- laneId : props . laneId ,
70- index : props . index ,
71- card : props
72- }
73- } ,
74-
75- endDrag ( props , monitor ) {
76- const item = monitor . getItem ( )
77- const dropResult = monitor . getDropResult ( )
78- if ( dropResult ) {
79- if ( dropResult . laneId !== item . laneId ) {
80- props . moveCardAcrossLanes ( item . laneId , dropResult . laneId , item . id )
81- }
82- props . handleDragEnd && props . handleDragEnd ( item . id , item . laneId , dropResult . laneId )
83- }
84- }
85- }
86-
87- const cardTarget = {
88- hover ( props , monitor , component ) {
89- const dragIndex = monitor . getItem ( ) . index
90- const hoverIndex = props . index
91- const sourceListId = monitor . getItem ( ) . laneId
92-
93- if ( dragIndex === hoverIndex ) {
94- return
95- }
96-
97- // Determine rectangle on screen
98- const hoverBoundingRect = findDOMNode ( component ) . getBoundingClientRect ( )
99-
100- // Get vertical middle
101- const hoverMiddleY = ( hoverBoundingRect . bottom - hoverBoundingRect . top ) / 2
102-
103- // Determine mouse position
104- const clientOffset = monitor . getClientOffset ( )
105-
106- // Get pixels to the top
107- const hoverClientY = clientOffset . y - hoverBoundingRect . top
108-
109- // Only perform the move when the mouse has crossed half of the items height
110- // When dragging downwards, only move when the cursor is below 50%
111- // When dragging upwards, only move when the cursor is above 50%
112-
113- // Dragging downwards
114- if ( dragIndex < hoverIndex && hoverClientY < hoverMiddleY ) {
115- return
116- }
117-
118- // Dragging upwards
119- if ( dragIndex > hoverIndex && hoverClientY > hoverMiddleY ) {
120- return
121- }
122-
123- if ( props . laneId === sourceListId ) {
124- props . moveCard ( dragIndex , hoverIndex )
125- monitor . getItem ( ) . index = hoverIndex
126- }
127- }
128- }
129-
13072Card . defaultProps = {
13173 cardStyle : { } ,
13274 customCardLayout : false ,
@@ -142,21 +84,11 @@ Card.propTypes = {
14284 onClick : PropTypes . func ,
14385 onDelete : PropTypes . func ,
14486 metadata : PropTypes . object ,
145- connectDragSource : PropTypes . func . isRequired ,
146- isDragging : PropTypes . bool . isRequired ,
14787 handleDragStart : PropTypes . func ,
14888 handleDragEnd : PropTypes . func ,
14989 customCardLayout : PropTypes . bool ,
15090 customCard : PropTypes . node ,
15191 editable : PropTypes . bool
15292}
15393
154- export default flow (
155- DropTarget ( DragType . CARD , cardTarget , connect => ( {
156- connectDropTarget : connect . dropTarget ( )
157- } ) ) ,
158- DragSource ( DragType . CARD , cardSource , ( connect , monitor ) => ( {
159- connectDragSource : connect . dragSource ( ) ,
160- isDragging : monitor . isDragging ( )
161- } ) )
162- ) ( Card )
94+ export default Card
0 commit comments