UX: flat-btn should also respond to Enter (#15584)

This commit is contained in:
Joffrey JAFFEUX 2022-01-14 15:51:31 +01:00 committed by GitHub
parent 8b3d50713d
commit c758e2cdd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 1 deletions

View File

@ -14,7 +14,15 @@ export default Component.extend({
}
},
keyDown(event) {
if (event.key === "Enter") {
this.action?.();
return false;
}
},
click() {
return this.attrs.action();
this.action?.();
return false;
},
});

View File

@ -0,0 +1,47 @@
import componentTest, {
setupRenderingTest,
} from "discourse/tests/helpers/component-test";
import { discourseModule } from "discourse/tests/helpers/qunit-helpers";
import { click, triggerKeyEvent } from "@ember/test-helpers";
import hbs from "htmlbars-inline-precompile";
discourseModule("Integration | Component | flat-button", function (hooks) {
setupRenderingTest(hooks);
componentTest("press Enter", {
template: hbs`{{flat-button action=action}}`,
beforeEach() {
this.set("foo", null);
this.set("action", () => {
this.set("foo", "bar");
});
},
async test(assert) {
await triggerKeyEvent(".btn-flat", "keydown", 32);
assert.strictEqual(this.foo, null);
await triggerKeyEvent(".btn-flat", "keydown", 13);
assert.strictEqual(this.foo, "bar");
},
});
componentTest("click", {
template: hbs`{{flat-button action=action}}`,
beforeEach() {
this.set("foo", null);
this.set("action", () => {
this.set("foo", "bar");
});
},
async test(assert) {
await click(".btn-flat");
assert.strictEqual(this.foo, "bar");
},
});
});