From f850c1befce22a690dbcab0cf05a57c1c27ceca2 Mon Sep 17 00:00:00 2001 From: jinye Date: Mon, 3 Jun 2024 17:39:56 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20contextMenu=E6=94=AF=E6=8C=81=E9=98=BB?= =?UTF-8?q?=E6=AD=A2=E5=85=B3=E9=97=AD=E9=92=A9=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../amis-ui/src/components/ContextMenu.tsx | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/packages/amis-ui/src/components/ContextMenu.tsx b/packages/amis-ui/src/components/ContextMenu.tsx index 67c277840..baf1d237d 100644 --- a/packages/amis-ui/src/components/ContextMenu.tsx +++ b/packages/amis-ui/src/components/ContextMenu.tsx @@ -49,6 +49,7 @@ interface ContextMenuState { align?: 'left' | 'right'; onClose?: (ctx: ContextMenu) => void; contentClassName?: string; + preventClose?: (e: Event) => boolean; } export class ContextMenu extends React.Component< @@ -130,6 +131,7 @@ export class ContextMenu extends React.Component< onClose?: (ctx: ContextMenu) => void, options?: { contentClassName?: string; + preventClose?: (e: Event) => boolean; } ) { if (this.state.isOpened) { @@ -147,7 +149,8 @@ export class ContextMenu extends React.Component< cursorY, menus: menus, onClose, - contentClassName: options?.contentClassName + contentClassName: options?.contentClassName, + preventClose: options?.preventClose }, () => { this.handleEnter(this.menuRef.current as HTMLElement); @@ -162,14 +165,18 @@ export class ContextMenu extends React.Component< cursorY: info.y, menus: menus, onClose, - contentClassName: options?.contentClassName + contentClassName: options?.contentClassName, + preventClose: options?.preventClose }); } this.prevInfo = info; } @autobind - close() { + close(e: Event) { + if (this.state.preventClose?.(e)) { + return; + } this.menuEntered = false; this.resizeObserver?.disconnect(); const onClose = this.state.onClose; @@ -181,7 +188,8 @@ export class ContextMenu extends React.Component< cursorX: -99999, cursorY: -99999, menus: [], - contentClassName: '' + contentClassName: '', + preventClose: undefined }, () => { onClose?.(this); @@ -201,7 +209,7 @@ export class ContextMenu extends React.Component< } if (this.state.isOpened) { e.preventDefault(); - this.close(); + this.close(e); } } @@ -229,7 +237,7 @@ export class ContextMenu extends React.Component< handleKeyDown(e: KeyboardEvent) { if (e.keyCode === 27 && this.state.isOpened) { e.preventDefault(); - this.close(); + this.close(e); } } @@ -386,6 +394,7 @@ export async function openContextMenus( onClose?: (ctx: ContextMenu) => void, options?: { contentClassName?: string; + preventClose?: (e: Event) => boolean; } ) { return ContextMenu.getInstance().then(instance =>