Skip to content

Commit

Permalink
feat(theme:menu): add last argument of find (#1834)
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk authored Sep 17, 2024
1 parent c4e07f2 commit c581b94
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
8 changes: 7 additions & 1 deletion packages/theme/src/services/menu/menu.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('Service: Menu', () => {
},
{ text: 'text', externalLink: '//ng-alain.com' },
{ text: 'text', link: '/demo2', i18n: 'text' },
{ text: 'sub', children: [] },
{ text: 'sub', children: [{ text: 'text', link: '/test', badge: 10 }] },
{ text: 'hide', link: '/hide', hide: true }
];

Expand Down Expand Up @@ -342,6 +342,12 @@ describe('Service: Menu', () => {
const res = srv.find({ url: `/always-first-item`, cb: _ => true });
expect(res).toBe(srv.menus[0]);
});
it('return last item', () => {
const first = srv.find({ url: `/test` });
expect((first as NzSafeAny)._parent == null).toBe(true);
const last = srv.find({ url: `/test`, last: true });
expect((last as NzSafeAny)._parent != null).toBe(true);
});
});

describe('#open', () => {
Expand Down
13 changes: 11 additions & 2 deletions packages/theme/src/services/menu/menu.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,14 @@ export class MenuService implements OnDestroy {
* 是否忽略隐藏的项,默认:`false`
*/
ignoreHide?: boolean;
/**
* Whether to return the last one, default: `false`
*
* 是否返回最后一个,默认:`false`
*/
last?: boolean;
}): Menu | null {
const opt = { recursive: false, ignoreHide: false, ...options };
const opt = { recursive: false, ignoreHide: false, last: false, ...options };
if (opt.key != null) {
return this.getItem(opt.key);
}
Expand All @@ -223,12 +229,15 @@ export class MenuService implements OnDestroy {

while (!item && url) {
this.visit(opt.data ?? this.data, i => {
if (!opt.last && item != null) {
return;
}
if (opt.ignoreHide && i.hide) {
return;
}
if (opt.cb) {
const res = opt.cb(i);
if (!item && typeof res === 'boolean' && res) {
if (typeof res === 'boolean' && res) {
item = i;
}
}
Expand Down

0 comments on commit c581b94

Please sign in to comment.