GridItem.js 4.3 KB

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