DEV: using Enter on a focused button should trigger action (#15564)

This commit is contained in:
Joffrey JAFFEUX 2022-01-13 14:24:52 +01:00 committed by GitHub
parent a407d42984
commit 25722e0b08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 8 deletions

View File

@ -109,10 +109,24 @@ export default Component.extend({
if (this.onKeyDown) {
e.stopPropagation();
this.onKeyDown(e);
} else if (e.key === "Enter") {
this._triggerAction(e);
return false;
}
},
click(event) {
this._triggerAction(event);
return false;
},
mouseDown(event) {
if (this.preventFocus) {
event.preventDefault();
}
},
_triggerAction(event) {
let { action } = this;
if (action) {
@ -141,13 +155,5 @@ export default Component.extend({
event.preventDefault();
event.stopPropagation();
return false;
},
mouseDown(event) {
if (this.preventFocus) {
event.preventDefault();
}
},
});

View File

@ -7,6 +7,7 @@ import {
query,
queryAll,
} from "discourse/tests/helpers/qunit-helpers";
import { triggerKeyEvent } from "@ember/test-helpers";
import I18n from "I18n";
import hbs from "htmlbars-inline-precompile";
@ -219,4 +220,49 @@ discourseModule("Integration | Component | d-button", function (hooks) {
);
},
});
componentTest("onKeyDown callback", {
template: hbs`{{d-button action=action onKeyDown=onKeyDown}}`,
beforeEach() {
this.set("foo", null);
this.set("onKeyDown", () => {
this.set("foo", "bar");
});
this.set("action", () => {
this.set("foo", "baz");
});
},
async test(assert) {
await triggerKeyEvent(".btn", "keydown", 32);
assert.strictEqual(this.foo, "bar");
await triggerKeyEvent(".btn", "keydown", 13);
assert.strictEqual(this.foo, "bar");
},
});
componentTest("press Enter", {
template: hbs`{{d-button action=action}}`,
beforeEach() {
this.set("foo", null);
this.set("action", () => {
this.set("foo", "bar");
});
},
async test(assert) {
await triggerKeyEvent(".btn", "keydown", 32);
assert.strictEqual(this.foo, null);
await triggerKeyEvent(".btn", "keydown", 13);
assert.strictEqual(this.foo, "bar");
},
});
});