mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-21 08:16:36 +00:00
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
This commit is contained in:
parent
92165d776a
commit
af2d575bf0
@ -20,6 +20,15 @@ function findNestedItem(
|
|||||||
}, obj);
|
}, 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<A>(
|
export function nestedArrayMove<A>(
|
||||||
obj: A,
|
obj: A,
|
||||||
oldIndex: number,
|
oldIndex: number,
|
||||||
@ -27,14 +36,18 @@ export function nestedArrayMove<A>(
|
|||||||
oldPath?: ItemPath,
|
oldPath?: ItemPath,
|
||||||
newPath?: ItemPath
|
newPath?: ItemPath
|
||||||
): A {
|
): 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 from = oldPath ? findNestedItem(newObj, oldPath) : newObj;
|
||||||
const to = newPath ? findNestedItem(newObj, newPath, true) : 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];
|
const item = from.splice(oldIndex, 1)[0];
|
||||||
to.splice(newIndex, 0, item);
|
to.splice(newIndex, 0, item);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user