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

fix(dropdown): flat children as item children #1870

Merged
merged 1 commit into from
Oct 17, 2022
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
4 changes: 2 additions & 2 deletions src/dropdown/_example/slot.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
</t-dropdown-menu>
</t-dropdown-item>

<t-dropdown-item :value="2"> 操作二 </t-dropdown-item>
<t-dropdown-item :value="2">操作二 </t-dropdown-item>

<t-dropdown-item :value="3">操作三</t-dropdown-item>
</t-dropdown-menu>
Expand All @@ -73,7 +73,7 @@ import { MessagePlugin } from 'tdesign-vue-next';
const clickHandler = (data) => {
console.log(data, 'data');
if (data.value !== 212) MessagePlugin.success(`选中【${data.content}`);
if (data.value !== 212) MessagePlugin.success(`选中【${data.value}`);
};
const handleClick = () => {
MessagePlugin.success(`点击 操作2-1-2`);
Expand Down
6 changes: 4 additions & 2 deletions src/dropdown/dropdown-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export default defineComponent({
renderContent = (
<div key={idx}>
<DropdownItem
class={[`${dropdownClass.value}__item`, `${dropdownClass.value}__item--suffix`]}
style={optionItem.style}
class={[`${dropdownClass.value}__item`, `${dropdownClass.value}__item--suffix`, optionItem.class]}
value={optionItem.value}
theme={optionItem.theme}
active={optionItem.active}
Expand Down Expand Up @@ -83,7 +84,8 @@ export default defineComponent({
renderContent = (
<div key={idx}>
<DropdownItem
class={`${dropdownClass.value}__item`}
style={optionItem.style}
class={[`${dropdownClass.value}__item`, optionItem.class]}
value={optionItem.value}
theme={optionItem.theme}
active={optionItem.active}
Expand Down
13 changes: 8 additions & 5 deletions src/dropdown/hooks/useDropdownOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@ export const getOptionsFromChildren = (menuGroup: any): DropdownOption[] => {
.map((item) => {
const groupChildren = item.children?.default?.();

const contentIdx = groupChildren?.findIndex?.(
(v: VNode) => typeof v.children === 'string' && (v.type as { description: string }).description !== 'Comment',
// 当前节点的渲染内容
const contentCtx = groupChildren?.filter?.(
(v: VNode) => !['TDropdownMenu', 'TDropdownItem'].includes((v.type as { name: string })?.name),
);
const childrenContent = groupChildren?.filter?.(
// 嵌套菜单的节点
const childrenCtx = groupChildren?.filter?.(
(v: VNode) =>
typeof v.children !== 'string' &&
['TDropdownMenu', 'TDropdownItem'].includes((v.type as { name: string })?.name),
);

return {
...item.props,
content: groupChildren?.[contentIdx]?.children || groupChildren,
children: childrenContent?.length > 0 ? getOptionsFromChildren(childrenContent) : null,
content: contentCtx || groupChildren,
children: childrenCtx?.length > 0 ? getOptionsFromChildren(childrenCtx) : null,
};
})
.filter((v) => !!v.content);
Expand Down