Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(theme:menu): add last argument of find #1834

Merged
merged 3 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading