1 | import React, {Component} from 'react';
|
2 | import {DragDropContext, Droppable, Draggable} from 'react-beautiful-dnd';
|
3 | import Drag from 'react-draggable'
|
4 | import PropTypes from 'prop-types';
|
5 | import isEqual from 'lodash.isequal';
|
6 | import SortList from './SortList';
|
7 | import Between from './Between';
|
8 |
|
9 |
|
10 | const 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 | };
|
28 | const 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 |
|
52 | class 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 | }
|
107 | Dnd.propTypes = propTypes;
|
108 | Dnd.defaultProps = defaultProps;
|
109 | Dnd.Drag = Drag;
|
110 | Dnd.DragDropContext = DragDropContext;
|
111 | Dnd.Droppable = Droppable;
|
112 | Dnd.Draggable = Draggable;
|
113 | export default Dnd; |
\ | No newline at end of file |