Skip to content

Commit 5e33c5b

Browse files
committed
[FIX] Actions: ensure the sequence is applied on action children
Currently, the helper 'createActions' pre-sorts a list of menuRegistry entries but this sort is not applied to the children (which can be generated dynamically). It never showed up as we rarely add children entries from different sources and their sequence often matches the order of insertion in the registry. Task: 5452669 X-original-commit: 7c8456d
1 parent 7f5195e commit 5e33c5b

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

src/actions/action.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ export function createAction(item: ActionSpec): Action {
113113
return children
114114
.map((child) => (typeof child === "function" ? child(env) : child))
115115
.flat()
116-
.map(createAction);
116+
.map(createAction)
117+
.sort((a, b) => a.sequence - b.sequence);
117118
}
118119
: () => [],
119120
isReadonlyAllowed: item.isReadonlyAllowed || false,

tests/menus/menu_items_registry.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import {
5050

5151
import { Currency, Model } from "../../src";
5252

53+
import { createActions } from "../../src/actions/action";
5354
import { CellComposerStore } from "../../src/components/composer/composer/cell_composer_store";
5455
import { FONT_SIZES } from "../../src/constants";
5556
import { functionRegistry } from "../../src/functions";
@@ -116,6 +117,7 @@ describe("Top Bar Menu Item Registry", () => {
116117
id: name,
117118
name: name,
118119
execute: () => {},
120+
sequence: 1,
119121
}));
120122
});
121123
const env = makeTestEnv();
@@ -1978,3 +1980,32 @@ describe("Menu Item actions", () => {
19781980
});
19791981
});
19801982
});
1983+
1984+
test("Menu children are sorted by sequence", async () => {
1985+
const env = makeTestEnv();
1986+
const menuItems = createActions([
1987+
{
1988+
id: "menu_1",
1989+
name: "Menu 1",
1990+
sequence: 20,
1991+
children: [
1992+
{
1993+
id: "secondItem",
1994+
name: "bigger sequence Item",
1995+
sequence: 30,
1996+
execute: () => {},
1997+
},
1998+
{
1999+
id: "firstItem",
2000+
name: "lower sequence Item",
2001+
sequence: 10,
2002+
execute: () => {},
2003+
},
2004+
],
2005+
},
2006+
]);
2007+
2008+
const children = menuItems[0].children(env);
2009+
expect(children[0].id).toBe("firstItem");
2010+
expect(children[1].id).toBe("secondItem");
2011+
});

0 commit comments

Comments
 (0)