mock平台

EasyDragSort.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import PropTypes from 'prop-types';
  4. /**
  5. * @author suxiaoxin
  6. * @demo
  7. * <EasyDragSort data={()=>this.state.list} onChange={this.handleChange} >
  8. * {list}
  9. * </EasyDragSot>
  10. */
  11. let curDragIndex = null;
  12. function isDom(obj) {
  13. return (
  14. obj &&
  15. typeof obj === 'object' &&
  16. obj.nodeType === 1 &&
  17. typeof obj.nodeName === 'string' &&
  18. typeof obj.getAttribute === 'function'
  19. );
  20. }
  21. export default class EasyDragSort extends React.Component {
  22. static propTypes = {
  23. children: PropTypes.array,
  24. onChange: PropTypes.func,
  25. onDragEnd: PropTypes.func,
  26. data: PropTypes.func,
  27. onlyChild: PropTypes.string
  28. };
  29. render() {
  30. const that = this;
  31. const props = this.props;
  32. const { onlyChild } = props;
  33. let container = props.children;
  34. const onChange = (from, to) => {
  35. if (from === to) {
  36. return;
  37. }
  38. let curValue;
  39. curValue = props.data();
  40. let newValue = arrMove(curValue, from, to);
  41. if (typeof props.onChange === 'function') {
  42. return props.onChange(newValue, from, to);
  43. }
  44. };
  45. return (
  46. <div>
  47. {container.map((item, index) => {
  48. if (React.isValidElement(item)) {
  49. return React.cloneElement(item, {
  50. draggable: onlyChild ? false : true,
  51. ref: 'x' + index,
  52. 'data-ref': 'x' + index,
  53. onDragStart: function() {
  54. curDragIndex = index;
  55. },
  56. /**
  57. * 控制 dom 是否可拖动
  58. * @param {*} e
  59. */
  60. onMouseDown(e) {
  61. if (!onlyChild) {
  62. return;
  63. }
  64. let el = e.target,
  65. target = e.target;
  66. if (!isDom(el)) {
  67. return;
  68. }
  69. do {
  70. if (el && isDom(el) && el.getAttribute(onlyChild)) {
  71. target = el;
  72. }
  73. if (el && el.tagName == 'DIV' && el.getAttribute('data-ref')) {
  74. break;
  75. }
  76. } while ((el = el.parentNode));
  77. if (!el) {
  78. return;
  79. }
  80. let ref = that.refs[el.getAttribute('data-ref')];
  81. let dom = ReactDOM.findDOMNode(ref);
  82. if (dom) {
  83. dom.draggable = target.getAttribute(onlyChild) ? true : false;
  84. }
  85. },
  86. onDragEnter: function() {
  87. onChange(curDragIndex, index);
  88. curDragIndex = index;
  89. },
  90. onDragEnd: function() {
  91. curDragIndex = null;
  92. if (typeof props.onDragEnd === 'function') {
  93. props.onDragEnd();
  94. }
  95. }
  96. });
  97. }
  98. return item;
  99. })}
  100. </div>
  101. );
  102. }
  103. }
  104. function arrMove(arr, fromIndex, toIndex) {
  105. arr = [].concat(arr);
  106. let item = arr.splice(fromIndex, 1)[0];
  107. arr.splice(toIndex, 0, item);
  108. return arr;
  109. }