Skip to content

Commit

Permalink
fix: dragging working not correct
Browse files Browse the repository at this point in the history
The logic for calculating the positions of top and left in PR react-grid-layout#1323 is problematic. The compensation calculation for the containerPadding attribute should be placed in calcXY instead of being processed every time during dragging
  • Loading branch information
jay0815 committed Feb 5, 2024
1 parent f8be0d9 commit c023171
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions lib/GridItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -503,27 +503,26 @@ export default class GridItem extends React.Component<Props, State> {
const { offsetParent } = node;

if (offsetParent) {
const { margin, rowHeight, containerPadding } = this.props;
const { margin, rowHeight } = this.props;
const bottomBoundary =
offsetParent.clientHeight - calcGridItemWHPx(h, rowHeight, margin[1]);
top = clamp(top - containerPadding[1], 0, bottomBoundary);
top = clamp(top, 0, bottomBoundary);

const colWidth = calcGridColWidth(positionParams);
const rightBoundary =
containerWidth - calcGridItemWHPx(w, colWidth, margin[0]);
left = clamp(left - containerPadding[0], 0, rightBoundary);
left = clamp(left, 0, rightBoundary);
}
}

const newPosition: PartialPosition = { top, left };
this.setState({ dragging: newPosition });

// Call callback with this data
const { containerPadding } = this.props;
const { x, y } = calcXY(
positionParams,
top - containerPadding[1],
left - containerPadding[0],
top,
left,
w,
h
);
Expand All @@ -546,15 +545,15 @@ export default class GridItem extends React.Component<Props, State> {
if (!this.state.dragging) {
throw new Error("onDragEnd called before onDragStart.");
}
const { w, h, i, containerPadding } = this.props;
const { w, h, i } = this.props;
const { left, top } = this.state.dragging;
const newPosition: PartialPosition = { top, left };
this.setState({ dragging: null });

const { x, y } = calcXY(
this.getPositionParams(),
top - containerPadding[1],
left - containerPadding[0],
top,
left,
w,
h
);
Expand Down

0 comments on commit c023171

Please sign in to comment.