-
Environment
QuestionHello, I need some help with button component Is there a way to prevent enter key press to execute click action? <Button onClick={this.onAdd}/>With the simple code above, when I click on my button then press enter onAdd is executed twice. First time after the click, second time after pressing enter. According to the following code present in abstractButton.tsx file, i would say it's not possible, isn't it? protected handleKeyUp = (e: React.KeyboardEvent<any>) => {
// HACKHACK: https://github.com/palantir/blueprint/issues/4165
/* eslint-disable deprecation/deprecation */
if (Keys.isKeyboardClick(e.which)) {
this.setState({ isActive: false });
this.buttonRef?.click();
}
this.currentKeyDown = undefined;
this.props.onKeyUp?.(e);
};Thanks for you help. Maxime |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
|
A click and then an Enter keypress seems like an interaction that should trigger the button handler twice. But if you want to only handle the click event in |
Beta Was this translation helpful? Give feedback.
-
|
Hello Adi, Thanks for your help. You are right about the interaction that should trigger the button handler twice. I thought of this solution however the event I receive in
Is this correct or do I misunderstand? Maxime |
Beta Was this translation helpful? Give feedback.
-
|
Ah sorry, yes, it will be a click event when pressing Enter as well because we programmatically call |
Beta Was this translation helpful? Give feedback.
A click and then an Enter keypress seems like an interaction that should trigger the button handler twice.
But if you want to only handle the click event in
this.onAdd, then you can inspect theeventargument in theonClickcallback. If it's a keypress rather than a click event, ignore it in your handler.