From af2d575bf02d10a1c90b5270d688b06cf4c85864 Mon Sep 17 00:00:00 2001
From: Wendelin <12148533+wendevlin@users.noreply.github.com>
Date: Thu, 10 Oct 2024 16:53:35 +0200
Subject: [PATCH] Fix ha-selector-action drag and drop (#22273)
* Fix ha-selector-action with removing memoize-one
* Fix array-move to update parent reference.
* Fix array-move if item is no array
---
src/common/util/array-move.ts | 23 ++++++++++++++++++-----
1 file changed, 18 insertions(+), 5 deletions(-)
diff --git a/src/common/util/array-move.ts b/src/common/util/array-move.ts
index 0985f764c0..33ce6bb8e0 100644
--- a/src/common/util/array-move.ts
+++ b/src/common/util/array-move.ts
@@ -20,6 +20,15 @@ function findNestedItem(
}, obj);
}
+function updateNestedItem(obj: any, path: ItemPath): any {
+ const lastKey = path.pop()!;
+ const parent = findNestedItem(obj, path);
+ parent[lastKey] = Array.isArray(parent[lastKey])
+ ? [...parent[lastKey]]
+ : [parent[lastKey]];
+ return obj;
+}
+
export function nestedArrayMove(
obj: A,
oldIndex: number,
@@ -27,14 +36,18 @@ export function nestedArrayMove(
oldPath?: ItemPath,
newPath?: ItemPath
): A {
- const newObj = (Array.isArray(obj) ? [...obj] : { ...obj }) as A;
+ let newObj = (Array.isArray(obj) ? [...obj] : { ...obj }) as A;
+
+ if (oldPath) {
+ newObj = updateNestedItem(newObj, [...oldPath]);
+ }
+ if (newPath) {
+ newObj = updateNestedItem(newObj, [...newPath]);
+ }
+
const from = oldPath ? findNestedItem(newObj, oldPath) : newObj;
const to = newPath ? findNestedItem(newObj, newPath, true) : newObj;
- if (!Array.isArray(from) || !Array.isArray(to)) {
- return obj;
- }
-
const item = from.splice(oldIndex, 1)[0];
to.splice(newIndex, 0, item);