A platform for community discussion. Free, open, simple.
FIX: Keep focus moved by a select-kit onChange (#44688)
Jarek Radoszb830fdafd794
diff --git a/frontend/discourse/select-kit/components/select-kit.js b/frontend/discourse/select-kit/components/select-kit.jsindex 4695a4eb472..9980c7650e0 100644--- a/frontend/discourse/select-kit/components/select-kit.js+++ b/frontend/discourse/select-kit/components/select-kit.js@@ -893,7 +893,9 @@ export default class SelectKit extends Component {if (this.selectKit.options.focusAfterOnChange) {this._safeAfterRender(() => {- this._focusFilter();+ if (!this._focusMovedAway()) {+ this._focusFilter();+ }});}}@@ -1262,6 +1264,16 @@ export default class SelectKit extends Component {}}+ // onChange may move focus on purpose, e.g. into an input it revealed+ _focusMovedAway() {+ const active = document.activeElement;+ return (+ !!active &&+ active !== document.body &&+ !this.selectKit.mainElement()?.contains(active)+ );+ }+_focusFilter(forceHeader = false) {if (!this.selectKit.mainElement()) {return;diff --git a/frontend/discourse/tests/integration/components/select-kit/combo-box-test.gjs b/frontend/discourse/tests/integration/components/select-kit/combo-box-test.gjsindex bbba85c136e..44dcb6eea68 100644--- a/frontend/discourse/tests/integration/components/select-kit/combo-box-test.gjs+++ b/frontend/discourse/tests/integration/components/select-kit/combo-box-test.gjs@@ -125,4 +125,45 @@ module("Integration | Component | SelectKit | ComboBox", function (hooks) {.dom(`.d-icon-${this.caretUpIcon}`, header).exists("uses the icon provided");});++ test("refocuses the header after a change", async function (assert) {+ setDefaultState(this, { onChange: () => {} });++ await render(+ <template>+ <ComboBox+ @content={{this.content}}+ @onChange={{this.onChange}}+ @value={{this.value}}+ />+ </template>+ );++ await this.subject.expand();+ await this.subject.selectRowByValue(2);++ assert.dom(".select-kit-header").isFocused();+ });++ test("keeps focus that onChange moved elsewhere", async function (assert) {+ setDefaultState(this, {+ onChange: () => document.querySelector(".other-input").focus(),+ });++ await render(+ <template>+ <ComboBox+ @content={{this.content}}+ @onChange={{this.onChange}}+ @value={{this.value}}+ />+ <input class="other-input" />+ </template>+ );++ await this.subject.expand();+ await this.subject.selectRowByValue(2);++ assert.dom(".other-input").isFocused();+ });});