简体   繁体   English

antd Popconfirm onKeyDown事件如何实现

[英]How to implement antd Popconfirm onKeyDown event

I need to fire Popconfirm message not only when clicked on the Cancel button, but when pressed on Esc button on keyboard as well.我不仅需要在单击“取消”按钮时触发 Popconfirm 消息,而且还需要在按下键盘上的 Esc 按钮时触发。 Antd provides out of the box solution for onMouseEnter, onMouseLeave, onFocus, onClick events.https://ant.design/components/popconfirm/ Antd 为 onMouseEnter、onMouseLeave、onFocus、onClick 事件提供了开箱即用的解决方案。https://ant.design/components/popconfirm/

How to pass onKeyDown event to a child node of the Popconfirm component?如何将 onKeyDown 事件传递给 Popconfirm 组件的子节点?

<Popconfirm
   title={message}
   placement='top'
   onConfirm={handleConfirm}
>
    <Button>{'Cancel'}</Button>
</Popconfirm>

Popconfirm is a child of the Tooltip component ( https://ant.design/components/tooltip/#API ) which has the visible prop. PopconfirmTooltip组件 ( https://ant.design/components/tooltip/#API ) 的子组件,它具有visible属性。 With that, you can handle the visibility state outside of your Popconfirm, for example in a wrapping component, which also listenes to the key event.这样,您可以在 Popconfirm 之外处理可见性状态,例如在包装组件中,该组件也侦听关键事件。

Something like that should work:像这样的东西应该工作:

import React, {useState} from "react";
import {Popconfirm} from "antd";

const PopconfirmWrapper = ({children, message, onConfirm}) => {
    const [visible, setVisible] = useState(false);
    return <Popconfirm
        onKeyDown={() => /* maybe check the key first? */ setVisible(true)}
        title={message}
        placement='top'
        onConfirm={onConfirm}
        onVisibleChange={() => setVisible(!visible)}>
        {children}
    </Popconfirm>
}

export default PopconfirmWrapper;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM