UNPKG

3.05 kBJavaScriptView Raw
1import React, {Component} from 'react';
2import {DragDropContext, Droppable, Draggable} from 'react-beautiful-dnd';
3import Drag from 'react-draggable'
4import PropTypes from 'prop-types';
5import isEqual from 'lodash.isequal';
6import SortList from './SortList';
7import Between from './Between';
8import GridLayout from './GridLayout';
9
10const propTypes = {
11 onStart: PropTypes.func,
12 onDrag: PropTypes.func,
13 onStop: PropTypes.func,
14 onDragUpdate: PropTypes.func,
15 dropClass: PropTypes.string,
16 dropOverClass: PropTypes.string,
17 dragClass: PropTypes.string,
18 dragingClass: PropTypes.string,
19 draggedClass:PropTypes.string,
20 className:PropTypes.string,
21 style:PropTypes.object,
22 list: PropTypes.array,
23 otherList: PropTypes.array,
24 type:PropTypes.oneOf(['vertical','horizontal','betweenVertical','betweenHorizontal']),
25 showKey:PropTypes.string
26
27};
28const defaultProps = {
29 onStart: () => {
30
31 },
32 onDrag:()=>{
33
34 },
35 onStop: () => {
36
37 },
38 onDragUpdate: () => {
39
40 },
41 list: false,
42 otherList:[],
43 dropClass:'',
44 dropOverClass:'',
45 dragClass:'',
46 dragingClass:'',
47 draggedClass:'',
48 type:'vertical'
49};
50
51
52class Dnd extends Component {
53 constructor(props) {
54 super(props);
55 this.state={
56 items:this.props.list||[]
57 }
58 }
59 componentWillReceiveProps(nextProps){
60 if(!isEqual(this.state.items,nextProps.list)){
61 this.setState({
62 items:nextProps.list
63 })
64 }
65 }
66
67 render() {
68 let self = this;
69 let DndType = () => {
70 switch(this.props.type){
71 case 'vertical':
72 return <SortList {...this.props}/>;
73 break;
74 case 'horizontal':
75 return <SortList {...this.props}/>;
76 break;
77 case 'betweenVertical':
78 return <Between {...this.props}/>;
79 break;
80 case 'betweenHorizontal':
81 return <Between {...this.props}/>;
82 break;
83 default :
84 return <SortList {...this.props}/>;
85 break;
86 }
87 }
88 return (
89 <div>
90 {
91 self.state.items.length ?
92 DndType(): (
93 <Drag defaultClassName={this.props.dragClass}
94 defaultClassNameDragging={this.props.dragingClass}
95 defaultClassNameDragged={this.props.draggedClass}
96 {...this.props}>
97 {self.props.children}
98 </Drag>
99 )
100 }
101 </div>
102
103
104 );
105 }
106}
107Dnd.propTypes = propTypes;
108Dnd.defaultProps = defaultProps;
109Dnd.Drag = Drag;
110Dnd.DragDropContext = DragDropContext;
111Dnd.Droppable = Droppable;
112Dnd.Draggable = Draggable;
113Dnd.GridLayout = GridLayout;
114export default Dnd;
\No newline at end of file