GridItem.js 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import * as React from "react";
  2. import { Dragger } from './dragger/index';
  3. import { checkInContainer } from './util/correction';
  4. export default class GridItem extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.onDrag = this.onDrag.bind(this);
  8. this.onDragStart = this.onDragStart.bind(this);
  9. this.onDragEnd = this.onDragEnd.bind(this);
  10. this.calGridXY = this.calGridXY.bind(this);
  11. this.calColWidth = this.calColWidth.bind(this);
  12. }
  13. /** 计算容器的每一个格子多大 */
  14. calColWidth() {
  15. const { containerWidth, col, containerPadding, margin } = this.props;
  16. if (margin) {
  17. return (containerWidth - containerPadding[0] * 2 - margin[0] * (col + 1)) / col;
  18. }
  19. return (containerWidth - containerPadding[0] * 2 - 0 * (col + 1)) / col;
  20. }
  21. /**转化,计算网格的GridX,GridY值 */
  22. calGridXY(x, y) {
  23. const { margin, containerWidth, col, w, rowHeight } = this.props;
  24. /**坐标转换成格子的时候,无须计算margin */
  25. let GridX = Math.round(x / containerWidth * col);
  26. let GridY = Math.round(y / (rowHeight + (margin ? margin[1] : 0)));
  27. // /**防止元素出container */
  28. return checkInContainer(GridX, GridY, col, w);
  29. }
  30. /**给予一个grid的位置,算出元素具体的在容器中位置在哪里,单位是px */
  31. calGridItemPosition(GridX, GridY) {
  32. var { margin, rowHeight } = this.props;
  33. if (!margin)
  34. margin = [0, 0];
  35. let x = Math.round(GridX * this.calColWidth() + (GridX + 1) * margin[0]);
  36. let y = Math.round(GridY * rowHeight + margin[1] * (GridY + 1));
  37. return {
  38. x: x,
  39. y: y
  40. };
  41. }
  42. shouldComponentUpdate(props, state) {
  43. return this.props.GridX !== props.GridX ||
  44. this.props.GridY !== props.GridY ||
  45. this.props.isUserMove !== props.isUserMove;
  46. }
  47. /**宽和高计算成为px */
  48. calWHtoPx(w, h) {
  49. var { margin } = this.props;
  50. if (!margin)
  51. margin = [0, 0];
  52. const wPx = Math.round(w * this.calColWidth() + (w - 1) * margin[0]);
  53. const hPx = Math.round(h * this.props.rowHeight + (h - 1) * margin[1]);
  54. return { wPx, hPx };
  55. }
  56. onDragStart(x, y) {
  57. const { w, h, UniqueKey } = this.props;
  58. if (this.props.static)
  59. return;
  60. const { GridX, GridY } = this.calGridXY(x, y);
  61. this.props.onDragStart && this.props.onDragStart({
  62. event, GridX, GridY, w, h, UniqueKey: UniqueKey + ''
  63. });
  64. }
  65. onDrag(event, x, y) {
  66. if (this.props.static)
  67. return;
  68. const { GridX, GridY } = this.calGridXY(x, y);
  69. const { w, h, UniqueKey } = this.props;
  70. this.props.onDrag && this.props.onDrag({ GridX, GridY, w, h, UniqueKey: UniqueKey + '', event });
  71. }
  72. onDragEnd() {
  73. if (this.props.static)
  74. return;
  75. if (this.props.onDragEnd)
  76. this.props.onDragEnd(this.props.UniqueKey + '');
  77. }
  78. render() {
  79. const { w, h, style, bounds, GridX, GridY } = this.props;
  80. const { x, y } = this.calGridItemPosition(GridX, GridY);
  81. const { wPx, hPx } = this.calWHtoPx(w, h);
  82. return (React.createElement(Dragger, { style: Object.assign({}, style, { width: wPx, height: hPx, position: 'absolute', transition: this.props.isUserMove ? '' : 'all .2s' }), onDragStart: this.onDragStart, onMove: this.onDrag, onDragEnd: this.onDragEnd, x: x, y: y, isUserMove: this.props.isUserMove, bounds: bounds },
  83. React.createElement("div", { style: { width: wPx, height: hPx } }, React.Children.map(this.props.children, (child) => child))));
  84. }
  85. }
  86. GridItem.defaultProps = {
  87. col: 12,
  88. containerWidth: 500,
  89. containerPadding: [0, 0],
  90. margin: [10, 10],
  91. rowHeight: 30,
  92. w: 1,
  93. h: 1
  94. };