Skip to content

Commit 44a5e56

Browse files
author
xinfei1
committed
fix bug: when 2x2 or 1x2 collosion bug for horizontal
1 parent 5a20442 commit 44a5e56

File tree

6 files changed

+116
-149
lines changed

6 files changed

+116
-149
lines changed

README.md

+28-12
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,49 @@
11
## react-grid-layout-between
2-
A draggable grid layout , can between two or more Layouts, for React.
3-
Based on React-DnD,support to switch Compaction Type: horizontal || vertical.
2+
![react 16.8.6](https://img.shields.io/badge/react-%5E16.8.6-brightgreen.svg)
3+
![npm 6.9.0](https://img.shields.io/badge/npm-v6.9.0-blue.svg)
4+
![react-dnd](https://img.shields.io/badge/reactDnD-%5E2.6.0-7289da.svg)
5+
6+
A draggable grid layout , can between two or more Layouts, for React. Based on [React-DnD](https://github.com/react-dnd/react-dnd).
47

58
Live Demo : http://demo.sunxinfei.com/
69

7-
feature:
10+
Features:
11+
12+
- [x] DnD widgets between layouts
13+
14+
- [x] 100% React
15+
16+
- [x] Draggable widgets
17+
18+
- [x] Configurable packing: horizontal, vertical
819

9-
- [x] DnD card between layouts
20+
- [x] Bounds checking for dragging
1021

11-
- [x] switch horizontal || vertical layout
22+
- [x] Responsive breakpoints
1223

13-
- [x] auto layout when window resize
24+
- [x] Separate layouts per responsive breakpoint
1425

15-
- [x] add drag preview
26+
- [x] Grid Items placed using CSS Transforms
1627

17-
- [ ] Drag card colliseion by gravity center
28+
- [x] Drag Custom Preview
1829

19-
- [ ] add static card
30+
- [ ] Drag widgets colliseion by gravity center
2031

21-
- [ ] add MultiDrag
32+
- [ ] Static widgets
2233

23-
- [ ] get <reac-grid-layout-between /> component
34+
- [ ] Resizable widgets
35+
36+
- [ ] get `<reac-grid-layout-between />` component
2437

2538
bugs:
26-
- [ ] when 2x2 or 1x2 collosion bug for horizontal
39+
- [x] ~~when 2x2 or 1x2 collosion bug for horizontal~~
40+
2741
### Run Project
42+
2843
```
2944
npm install
3045
```
46+
3147
```
3248
npm run start
3349
```

public/index.html

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
</head>
2424
<body>
2525
<noscript>You need to enable JavaScript to run this app.</noscript>
26+
<div style="margin: 10px;">
27+
<a target="_blank" href="https://github.com/SunXinFei/react-grid-layout-between">View project on GitHub</a>
28+
</div>
2629
<div id="root"></div>
2730
<!--
2831
This HTML file is a template.

src/components/groupItem.jsx

-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ class Demo extends Component {
6969
}
7070
if (this.props.layout.containerWidth !== clientWidth) {
7171
this.props.handleLoad();
72-
// console.log('handle');
7372
}
7473
}
7574
//创建卡片

src/utils/collision.js

+4-12
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,9 @@ export const layoutCheck = (function () {
4545
fristItemID,
4646
compactType = 'vertical'
4747
) {
48-
let keyArr = [];
49-
let movedItem = [];
50-
let axis = 'gridx';
51-
if (compactType === "vertical") {
52-
axis = "gridy"
53-
}
48+
let keyArr = [],
49+
movedItem = [];
50+
const axis = compactType === "vertical"? "gridy": 'gridx';
5451

5552
let newlayout = layout.map((item, index) => {
5653
if (item.id !== cardID) {
@@ -59,12 +56,7 @@ export const layoutCheck = (function () {
5956
keyArr.push(item.id);
6057
let offsetXY = item[axis] + 1;
6158
// 移动模块位于循环检测方块中
62-
let widthOrHeight = 0;
63-
if (axis === "gridx") {
64-
widthOrHeight = item.width;
65-
} else {
66-
widthOrHeight = item.height;
67-
}
59+
const widthOrHeight = axis === "gridx"? item.width: item.height;
6860
//判断当前卡片的坐标和目标卡片加上宽度/高度是否有重叠,防止重叠产生
6961
//在vertical情况下,是判断类似拖拽(0,1)宽1高1的方块与(0,0)宽1高2的纵向长方形,此时的交叠,纵向长方形保持不动,
7062
//在horizontal情况下,是判断类似拖拽(1,0)宽1高1的方块与(0,0)宽2高1的横向长方形,此时的交叠,横向长方形保持不动,

src/utils/compact.js

+14-54
Original file line numberDiff line numberDiff line change
@@ -89,28 +89,19 @@ const getSpaceArea = (finishedLayout, item, cols) => {
8989
* 需优化的地方:如果移动中的卡片坐标应该一直在一个区域范围内,而不应该任意位置拖拽
9090
* @param {Array} layout
9191
* @param {Int} cols
92-
* @param {String} movingCardID 移动中的元素
93-
* @returns {layout} 最新layout布局
92+
* @param {Object} moveCard 移动中的元素
93+
* @returns {Array} 最新layout布局
9494
*/
95-
export const compactLayoutHorizontal = function (layout, cols, movingCardID) {
95+
export const compactLayoutHorizontal = function (layout, cols, moveCard) {
9696
let sorted = sortLayout(layout);
9797
const compareList = [];
98-
const needCompact = Array(layout.length);
99-
let arr = [];
100-
let moveCard;
101-
//进行坐标重置,移动中的卡片除外
102-
for (let i = 0; i < sorted.length; i++) {
103-
if (movingCardID === sorted[i].id) {
104-
moveCard = sorted[i];
105-
continue;
106-
}
107-
arr.push(sorted[i]);
108-
}
109-
//获得当前组内的最大的y值,并赋值给移动卡片,防止分组Y值无限变大
98+
const needCompact = new Array(layout.length);
99+
const movingCardID = moveCard ? moveCard.id : null;
100+
//获得当前组内的最大的y值,并赋值给移动卡片,防止group的Y值(高度)无限变大
110101
if (moveCard) {
111-
moveCard.gridy = Math.min(layoutBottom(arr), moveCard.gridy);
102+
moveCard.gridy = Math.min(layoutBottom(sorted), moveCard.gridy);
112103
}
113-
//将非移动的卡片进行坐标重置
104+
//将非移动中的卡片进行坐标重置
114105
for (let i = 0; i < sorted.length; i++) {
115106
if (movingCardID !== sorted[i].id) {
116107
sorted[i].gridy = 0;
@@ -120,11 +111,11 @@ export const compactLayoutHorizontal = function (layout, cols, movingCardID) {
120111
//进行重新放置,移动中卡片除外
121112
for (let i = 0, length = sorted.length; i < length; i++) {
122113
let finished;
123-
if (movingCardID === sorted[i].id) {
124-
finished = sorted[i];
125-
} else {
126-
finished = getSpaceArea(compareList, sorted[i], cols);
127-
}
114+
// if (movingCardID === sorted[i].id) {
115+
// finished = sorted[i];
116+
// } else {
117+
finished = getSpaceArea(compareList, sorted[i], cols);
118+
// }
128119
compareList.push(finished);
129120
needCompact[i] = finished;
130121
}
@@ -156,35 +147,4 @@ export const compactLayoutHorizontal = function (layout, cols, movingCardID) {
156147
// needCompact[i] = finished;
157148
// }
158149
// return needCompact;
159-
// };
160-
161-
// export const compactLayoutHorizontal = function( layout, cols ){
162-
// let sorted = sortLayout(layout);
163-
// const compareList = []
164-
// const needCompact = Array(layout.length)
165-
166-
// for(let i=0;i<sorted.length;i++){
167-
// sorted[i].gridy = 0;
168-
// sorted[i].gridx = 0;
169-
// }
170-
// let rowCount = 0;
171-
// for(let i=0, length=sorted.length; i<length; i++){
172-
// //获得某行已存在卡片的最大累加宽度
173-
// const compareListRow = _.filter(compareList,(c)=>{
174-
// return c.gridy == rowCount
175-
// });
176-
// const ll = layoutHorizontalRowLength(compareListRow);
177-
// //如果当前最大宽度加上当前卡片宽度大于cols,则放入下一行,
178-
// //否则设置gridx
179-
// if(ll+sorted[i].width > cols){
180-
// rowCount++;
181-
// sorted[i].gridy = rowCount;
182-
// }else{
183-
// sorted[i].gridy = rowCount;
184-
// sorted[i].gridx = ll;
185-
// }
186-
// compareList.push(sorted[i]);
187-
// needCompact[i] = sorted[i];
188-
// }
189-
// return needCompact;
190-
// }
150+
// };

0 commit comments

Comments
 (0)