Commit adff788de54a46fd035b569892135be377dd4f92

Authored by Netfan
Committed by GitHub
1 parent d34467d3

perf(tree): improve the beforeRightClick callback to support more configuration of the menu (#608)

src/components/Tree/src/index.vue
@@ -23,11 +23,12 @@ @@ -23,11 +23,12 @@
23 import { filter } from '/@/utils/helper/treeHelper'; 23 import { filter } from '/@/utils/helper/treeHelper';
24 24
25 import { useTree } from './useTree'; 25 import { useTree } from './useTree';
26 - import { useContextMenu, ContextMenuItem } from '/@/hooks/web/useContextMenu'; 26 + import { useContextMenu } from '/@/hooks/web/useContextMenu';
27 import { useExpose } from '/@/hooks/core/useExpose'; 27 import { useExpose } from '/@/hooks/core/useExpose';
28 import { useDesign } from '/@/hooks/web/useDesign'; 28 import { useDesign } from '/@/hooks/web/useDesign';
29 29
30 import { basicProps } from './props'; 30 import { basicProps } from './props';
  31 + import { CreateContextOptions } from '/@/components/ContextMenu';
31 32
32 interface State { 33 interface State {
33 expandedKeys: Keys; 34 expandedKeys: Keys;
@@ -128,18 +129,20 @@ @@ -128,18 +129,20 @@
128 129
129 async function handleRightClick({ event, node }: Recordable) { 130 async function handleRightClick({ event, node }: Recordable) {
130 const { rightMenuList: menuList = [], beforeRightClick } = props; 131 const { rightMenuList: menuList = [], beforeRightClick } = props;
131 - let rightMenuList: ContextMenuItem[] = []; 132 + let contextMenuOptions: CreateContextOptions = { event, items: [] };
132 133
133 if (beforeRightClick && isFunction(beforeRightClick)) { 134 if (beforeRightClick && isFunction(beforeRightClick)) {
134 - rightMenuList = await beforeRightClick(node); 135 + let result = await beforeRightClick(node, event);
  136 + if (Array.isArray(result)) {
  137 + contextMenuOptions.items = result;
  138 + } else {
  139 + Object.assign(contextMenuOptions, result);
  140 + }
135 } else { 141 } else {
136 - rightMenuList = menuList; 142 + contextMenuOptions.items = menuList;
137 } 143 }
138 - if (!rightMenuList.length) return;  
139 - createContextMenu({  
140 - event,  
141 - items: rightMenuList,  
142 - }); 144 + if (!contextMenuOptions.items?.length) return;
  145 + createContextMenu(contextMenuOptions);
143 } 146 }
144 147
145 function setExpandedKeys(keys: Keys) { 148 function setExpandedKeys(keys: Keys) {
src/components/Tree/src/props.ts
1 import type { PropType } from 'vue'; 1 import type { PropType } from 'vue';
2 -import type { ReplaceFields, ActionItem, Keys, CheckKeys } from './types'; 2 +import type { ReplaceFields, ActionItem, Keys, CheckKeys, ContextMenuOptions } from './types';
3 import type { ContextMenuItem } from '/@/hooks/web/useContextMenu'; 3 import type { ContextMenuItem } from '/@/hooks/web/useContextMenu';
4 import type { TreeDataItem } from 'ant-design-vue/es/tree/Tree'; 4 import type { TreeDataItem } from 'ant-design-vue/es/tree/Tree';
5 import { propTypes } from '/@/utils/propTypes'; 5 import { propTypes } from '/@/utils/propTypes';
@@ -53,7 +53,7 @@ export const basicProps = { @@ -53,7 +53,7 @@ export const basicProps = {
53 }, 53 },
54 54
55 beforeRightClick: { 55 beforeRightClick: {
56 - type: Function as PropType<(...arg: any) => ContextMenuItem[]>, 56 + type: Function as PropType<(...arg: any) => ContextMenuItem[] | ContextMenuOptions>,
57 default: null, 57 default: null,
58 }, 58 },
59 59
src/components/Tree/src/types.ts
1 import type { TreeDataItem } from 'ant-design-vue/es/tree/Tree'; 1 import type { TreeDataItem } from 'ant-design-vue/es/tree/Tree';
  2 +import { ContextMenuItem } from '/@/hooks/web/useContextMenu';
2 export interface ActionItem { 3 export interface ActionItem {
3 render: (record: Recordable) => any; 4 render: (record: Recordable) => any;
4 show?: boolean | ((record: Recordable) => boolean); 5 show?: boolean | ((record: Recordable) => boolean);
@@ -40,3 +41,9 @@ export interface InsertNodeParams { @@ -40,3 +41,9 @@ export interface InsertNodeParams {
40 list?: TreeDataItem[]; 41 list?: TreeDataItem[];
41 push?: 'push' | 'unshift'; 42 push?: 'push' | 'unshift';
42 } 43 }
  44 +
  45 +export interface ContextMenuOptions {
  46 + icon?: string;
  47 + styles?: any;
  48 + items?: ContextMenuItem[];
  49 +}