简体   繁体   English

再次按回车点击按钮

[英]Pressing enter clicks button again

I have this button that I click to print a document.我有这个按钮,我单击它来打印文档。 but the issues is it stays in focus I think and if I press enter key by mistake it prints it again {basically the button is getting pressed again on enter key.但问题是我认为它保持焦点,如果我错误地按下回车键,它会再次打印{基本上按钮再次按下回车键。 I am already using e.preventDefault() so I don't know why it is doing it.我已经在使用 e.preventDefault() 所以我不知道它为什么这样做。 Is it possible to remopve this behaviour?是否有可能消除这种行为?

Basically I only want handleOrderPrint() to run on click and not when Enter key is pressed.基本上我只希望 handleOrderPrint() 在点击时运行,而不是在按下 Enter 键时运行。

<Button onClick={e => handleOrderPrint(e)} type="primary">Print</Button>}
 const handleOrderPrint = (e: any) => {
    e.preventDefault();
    ipcRenderer.send('generatePickingDocs', order);
    setPrinted(true);
  };

Edit----------------> Already tried this:编辑----------------> 已经尝试过这个:

const btnEl = React.useRef(null);
  const handleOrderPrint = (e: any) => {
    e.preventDefault();
    if (e.keyCode !== 13) {
      console.log("Keycode", e.keyCode);
      ipcRenderer.send('generatePickingDocs', order);
      setPrinted(true);
    }
  };
<Button ref={btnEl} onClick={e => handleOrderPrint(e)} type="primary">Print</Button>

Add the onKeyPress handler and check if Enter key is pressed.添加onKeyPress处理程序并检查是否按下了Enter键。 Use Event.preventDefault() .使用Event.preventDefault()

 const { useRef, useEffect } = React; const App = (props) => { const ref = useRef(); // set focus to button for testing useEffect(() => ref.current.focus(), []); const print = msg => console.log(msg); const handleClick = e => print('Printing...'); const handleKeyPress = e => { if (e.key === 'Enter') { e.preventDefault(); return; } } return ( <div> <button ref={ref} onKeyPress={handleKeyPress} onClick={handleClick} > Print </button> </div> ); }; // Render it ReactDOM.render( <App />, document.getElementById("react") );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script> <div id="react"></div>

I believe you can use printed as button disable trigger我相信您可以将printed用作按钮禁用触发器

 const [printed, setPrinted] = React.useState(false); 
 const handleOrderPrint = (e: any) => {
        e.preventDefault();
        ipcRenderer.send('generatePickingDocs', order);
        setPrinted(true);
      };
 return (
 <Button disabled={printed} onClick={e => handleOrderPrint(e)} type="primary">Print</Button>)

Edited已编辑

How about using ref如何使用ref

 const [printed, setPrinted] = React.useState(false); 
 const btnEl = React.useRef(null);
 const handleOrderPrint = (e: any) => {
        e.preventDefault();
        btnEl.blur();
        ipcRenderer.send('generatePickingDocs', order);
        setPrinted(true);
      };
 return (
 <Button ref={btnEl} onClick={e => handleOrderPrint(e)} type="primary">Print</Button>)

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

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