fix: when removing one item and then adding another one that newly added one can not be moved (#1025)

* fix: when removing one item and then adding another one that newly added one can not be moved

* chore: address pull request feedback

* fix: deepsource canceled
This commit is contained in:
Meier Lukas
2024-08-29 22:26:02 +02:00
committed by GitHub
parent 94984ac11b
commit 2738f40c5e

View File

@@ -80,8 +80,16 @@ export const useGridstack = (section: Omit<Section, "items">, itemIds: string[])
isDynamic: section.kind === "dynamic",
});
const itemRefKeys = Object.keys(itemRefs.current);
// define items in itemRefs for easy access and reference to items
if (Object.keys(itemRefs.current).length !== itemIds.length) {
if (itemRefKeys.length !== itemIds.length) {
// Remove items that are not in the itemIds
// Otherwise when an item is removed and then another item is added, this foreach below will not run.
itemRefKeys.forEach((id) => {
if (!itemIds.includes(id)) {
delete itemRefs.current[id];
}
});
itemIds.forEach((id) => {
itemRefs.current[id] = itemRefs.current[id] ?? createRef();
});