Skip to content

Commit

Permalink
fix(dropdown): flat children as item children (#1870)
Browse files Browse the repository at this point in the history
  • Loading branch information
uyarn authored Oct 17, 2022
1 parent d0316ec commit 0ea5277
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
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

0 comments on commit 0ea5277

Please sign in to comment.