Skip to content

Allow locking certain items in place #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/Example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export default class Example extends React.Component<{}, ExampleState> {
itemKey="name"
template={PlanetItem}
list={this.state.list}
lockedItems={['Venus', 'Earth']}
onMoveEnd={(newList) => this._onListChange(newList)}
container={() =>
useContainer ? this._container.current! : document.body
Expand Down
42 changes: 40 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export interface Props<I, C, T> {
commonProps?: C;
onDragStart?: (draggedItem: I) => void;
onDragEnd?: (draggedItem: I) => void;
lockedItems?: ReadonlyArray<string>;
}
interface State {
useAbsolutePositioning: boolean;
Expand Down Expand Up @@ -167,6 +168,11 @@ export default class DraggableList<
pressY: number | undefined,
pageY: number
) {
const { lockedItems } = this.props;
if (lockedItems && lockedItems.includes(itemKey)) {
return; // Prevent dragging if the item is locked
}

if (document.documentElement)
document.documentElement.style.cursor = 'move';
window.addEventListener('mouseup', this._handleMouseUp);
Expand Down Expand Up @@ -467,7 +473,7 @@ export default class DraggableList<
}

private _getVisualListDuringDrag(): ReadonlyArray<I> {
const { list } = this.props;
const { list, lockedItems } = this.props;
const { dragging, lastDrag } = this.state;
if (!dragging || !lastDrag)
throw new Error(
Expand All @@ -477,12 +483,44 @@ export default class DraggableList<
const dragListIndex = this._getDragListIndex();
const dragVisualIndex = this._getDragVisualIndex();

return update(list, {
if (lockedItems && lockedItems.includes(lastDrag.itemKey)) {
return list; // Return the original list if the dragged item is locked
}

// Create a new list with the dragged item moved to the new position
let newList = update(list, {
$splice: [
[dragListIndex, 1],
[dragVisualIndex, 0, list[dragListIndex]],
],
});

// Ensure locked items remain in their original positions
if (lockedItems) {
// Iterate over locked items in reverse order
// to avoid multiple consecutive locked items
// to move each other.
[...lockedItems].reverse().forEach((lockedItemKey) => {
const lockedIndex = list.findIndex(
(item) => this._getKeyFn()(item) === lockedItemKey
);
const currentLockedIndex = newList.findIndex(
(item) => this._getKeyFn()(item) === lockedItemKey
);

if (lockedIndex !== currentLockedIndex) {
const lockedItem = newList[currentLockedIndex];
newList = update(newList, {
$splice: [
[currentLockedIndex, 1],
[lockedIndex, 0, lockedItem],
],
});
}
});
}

return newList;
}

private _getItemHeight(key: string): HeightData {
Expand Down