Skip to content

A platform for community discussion. Free, open, simple.

DEV: autofocus by default DOTP component (#35986)

Joffrey JAFFEUXba8b3e5ade68
diff --git a/frontend/discourse/app/components/d-otp/index.gjs b/frontend/discourse/app/components/d-otp/index.gjs
index 5d85473fd0e..76824a4cebd 100644
--- a/frontend/discourse/app/components/d-otp/index.gjs
+++ b/frontend/discourse/app/components/d-otp/index.gjs
@@ -4,6 +4,7 @@ import { on } from "@ember/modifier";
import { action } from "@ember/object";
import { isBlank } from "@ember/utils";
import { TrackedArray } from "@ember-compat/tracked-built-ins";
+import autoFocus from "discourse/modifiers/auto-focus";
import preventScrollOnFocus from "discourse/modifiers/prevent-scroll-on-focus";
import { i18n } from "discourse-i18n";
import Slot from "./slot";
@@ -20,6 +21,10 @@ export default class DOTP extends Component {
return this.args.slots ?? DEFAULT_SLOTS;
}
+ get autofocus() {
+ return this.args.autofocus ?? true;
+ }
+
get isFilled() {
return this.otp.every((char) => !isBlank(char));
}
@@ -143,6 +148,7 @@ export default class DOTP extends Component {
{{on "blur" this.onBlur}}
{{on "paste" this.onPaste}}
aria-label={{i18n "d_otp.screen_reader" count=this.slots}}
+ {{(if this.autofocus (modifier autoFocus))}}
...attributes
/>
</div>
diff --git a/frontend/discourse/tests/integration/components/d-otp-test.gjs b/frontend/discourse/tests/integration/components/d-otp-test.gjs
index 53fa1922154..7a19dbf6632 100644
--- a/frontend/discourse/tests/integration/components/d-otp-test.gjs
+++ b/frontend/discourse/tests/integration/components/d-otp-test.gjs
@@ -80,10 +80,20 @@ module("Integration | Component | DOTP", function (hooks) {
assert.strictEqual(this.value, "12");
});
+ test("@autofocus", async function (assert) {
+ await render(<template><DOTP /></template>);
+
+ assert.dom(".d-otp-slot[data-index='0'].--is-focused").exists();
+
+ await render(<template><DOTP @autofocus={{false}} /></template>);
+
+ assert.dom(".d-otp-slot[data-index='0'].--is-focused").doesNotExist();
+ });
+
test("shows placeholder dashes for empty slots", async function (assert) {
await render(<template><DOTP /></template>);
- assert.dom(".d-otp-slot:nth-child(1)").hasText("​ -");
+ assert.dom(".d-otp-slot:nth-child(1)").hasText("​");
assert.dom(".d-otp-slot:nth-child(2)").hasText("​ -");
assert.dom(".d-otp-slot:nth-child(3)").hasText("​ -");
assert.dom(".d-otp-slot:nth-child(4)").hasText("​ -");