mirror of
https://gitee.com/baidu/amis.git
synced 2024-12-02 11:58:10 +08:00
feat: contextMenu支持阻止关闭钩子
This commit is contained in:
parent
ffd251c240
commit
c6c3169351
@ -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,19 @@ 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;
|
||||
}
|
||||
e?.preventDefault?.();
|
||||
this.menuEntered = false;
|
||||
this.resizeObserver?.disconnect();
|
||||
const onClose = this.state.onClose;
|
||||
@ -181,7 +189,8 @@ export class ContextMenu extends React.Component<
|
||||
cursorX: -99999,
|
||||
cursorY: -99999,
|
||||
menus: [],
|
||||
contentClassName: ''
|
||||
contentClassName: '',
|
||||
preventClose: undefined
|
||||
},
|
||||
() => {
|
||||
onClose?.(this);
|
||||
@ -200,8 +209,7 @@ export class ContextMenu extends React.Component<
|
||||
return;
|
||||
}
|
||||
if (this.state.isOpened) {
|
||||
e.preventDefault();
|
||||
this.close();
|
||||
this.close(e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -228,8 +236,7 @@ export class ContextMenu extends React.Component<
|
||||
@autobind
|
||||
handleKeyDown(e: KeyboardEvent) {
|
||||
if (e.keyCode === 27 && this.state.isOpened) {
|
||||
e.preventDefault();
|
||||
this.close();
|
||||
this.close(e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -386,6 +393,7 @@ export async function openContextMenus(
|
||||
onClose?: (ctx: ContextMenu) => void,
|
||||
options?: {
|
||||
contentClassName?: string;
|
||||
preventClose?: (e?: Event) => boolean;
|
||||
}
|
||||
) {
|
||||
return ContextMenu.getInstance().then(instance =>
|
||||
|
Loading…
Reference in New Issue
Block a user