diff --git a/examples/basic-admin/index.ts b/examples/basic-admin/index.ts index b5a7a4a..7edc52d 100644 --- a/examples/basic-admin/index.ts +++ b/examples/basic-admin/index.ts @@ -33,17 +33,6 @@ import type { IMeResult, } from "shrimp-server" -class DemoShrimp extends Shrimp { - /** 默认 getMenuTree 用 i18n key,demo 没翻译这些键,直接用模块自带的 label */ - getMenuTree(): IMenuItem[] { - const items: IMenuItem[] = [] - this.modules.forEach(m => { - items.push({ name: m.moduleKey, label: m.label, moduleKey: m.moduleKey }) - }) - return items - } -} - interface ITag { _id: string; name: string } interface IProduct { _id: string; code: string; name: string; price: number; tagIds: string[] } @@ -70,7 +59,7 @@ const superAdmin: IShrimpOperator = { } async function bootstrap() { - const shrimp = new DemoShrimp() + const shrimp = new Shrimp() // TODO(70→90): ShrimpTableModule 当前只在 dataSource.model 存在时自动登记 crudRegistry, // 这里需要手动 wire 让 MemoryShrimpCrud 的 populate 也能查到关联表 diff --git a/src/Shrimp.ts b/src/Shrimp.ts index c6dce18..a02a22e 100644 --- a/src/Shrimp.ts +++ b/src/Shrimp.ts @@ -90,13 +90,13 @@ export class Shrimp { return Array.from(map.entries()).map(([key, label]) => ({ key, label })) } - /** 菜单树,默认平铺所有模块,子类可覆盖为分组结构 */ + /** 菜单树,默认平铺所有模块(label 用模块注册时声明的 label),子类可覆盖为分组结构 */ getMenuTree(): IMenuItem[] { const items: IMenuItem[] = [] this.modules.forEach(module => { items.push({ name: module.moduleKey, - label: { t: module.moduleKey + '.menuManage' }, + label: module.label, moduleKey: module.moduleKey }) }) @@ -705,7 +705,7 @@ const userFields: IFieldConfig[] = [ export class UserModule extends ShrimpTableModule> { constructor(public parent: Shrimp | ShrimpModule, dataSource: ShrimpCrud>, fields?: IFieldConfig[]) { - super(parent, "user", dataSource, fields || userFields) + super(parent, "user", dataSource, fields || userFields, { t: "user.menuManage" }) this.initCrud() } @@ -794,7 +794,7 @@ const roleFields: IFieldConfig[] = [ export class RoleModule extends ShrimpTableModule, IRoleProto> { constructor(public parent: Shrimp | ShrimpModule, dataSource: ShrimpCrud>, fields?: IFieldConfig[]) { - super(parent, "role", dataSource, fields || roleFields) + super(parent, "role", dataSource, fields || roleFields, { t: "role.menuManage" }) this.initCrud() this.addEvent("clear", { t: "power.clear" }) this.addEvent("export", { t: "power.export" }) diff --git a/test/getMenuTree.test.ts b/test/getMenuTree.test.ts new file mode 100644 index 0000000..b014de9 --- /dev/null +++ b/test/getMenuTree.test.ts @@ -0,0 +1,27 @@ +import { describe, it, expect } from "vitest" +import { Shrimp, ShrimpTableModule, MemoryShrimpCrud, UserModule, RoleModule } from "../src" + +describe("Shrimp.getMenuTree", () => { + it("返回的 label 等于模块注册时声明的 label", () => { + const shrimp = new Shrimp() + new ShrimpTableModule(shrimp, "tag", new MemoryShrimpCrud(), [], "标签管理") + new ShrimpTableModule(shrimp, "product", new MemoryShrimpCrud(), [], { t: "product.menuManage" }) + + const tree = shrimp.getMenuTree() + + expect(tree).toEqual([ + { name: "tag", label: "标签管理", moduleKey: "tag" }, + { name: "product", label: { t: "product.menuManage" }, moduleKey: "product" }, + ]) + }) + + it("内置 UserModule / RoleModule 的菜单 label 用 user.menuManage / role.menuManage", () => { + const shrimp = new Shrimp() + new UserModule(shrimp, new MemoryShrimpCrud() as any) + new RoleModule(shrimp, new MemoryShrimpCrud() as any) + + const tree = shrimp.getMenuTree() + expect(tree.find(m => m.moduleKey === "user")?.label).toEqual({ t: "user.menuManage" }) + expect(tree.find(m => m.moduleKey === "role")?.label).toEqual({ t: "role.menuManage" }) + }) +})