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: contentTabs order support #2056

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 18 additions & 13 deletions src/client/theme-default/slots/ContentTabs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,24 @@ const ContentTabs: FC<IContentTabsProps> = ({
{intl.formatMessage({ id: 'content.tabs.default' })}
</button>
</li>
{tabs!.map((tab) => (
<li
key={tab.key}
onClick={() => onChange(tab)}
data-active={key === tab.key || undefined}
>
<button type="button">
{tab.titleIntlId
? intl.formatMessage({ id: tab.titleIntlId })
: tab.meta.frontmatter.title}
</button>
</li>
))}
{tabs!
.sort(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

顺序设置后是不会变的,每次渲染都重新排序会比较浪费,在 tabs 数据生成的时候做排序更合适:

  1. routeMeta.tabs = tabs.map((tabId) =>
    genTab(tabId, getRouteMetaById(tabId, opts)),
    );
  2. routeMeta.tabs = await Promise.all(
    tabs.map(async (tabId) =>
    genTab(tabId, await getRouteMetaById(tabId, opts)),
    ),
    );

另外文档里也加一下 order 的用法示例吧:https://d.umijs.org/guide/page-tab

(a, b) =>
(b.meta.frontmatter.order || 0) - (a.meta.frontmatter.order || 0),
)
.map((tab) => (
<li
key={tab.key}
onClick={() => onChange(tab)}
data-active={key === tab.key || undefined}
>
<button type="button">
{tab.titleIntlId
? intl.formatMessage({ id: tab.titleIntlId })
: tab.meta.frontmatter.title}
</button>
</li>
))}
</ul>
) : null;
};
Expand Down