Skip to content

Commit f363ae5

Browse files
authored
FIX: Allow Enter key to work inside select-kit within modals (#36596)
When selecting an item in a select-kit dropdown (e.g., the assignee chooser in the assign modal), pressing Enter would close the modal without completing the selection. This was caused by commit 1d2ae51 which added `{ capture: true }` to the modal's keydown listener to prevent keypresses from leaking outside modals. However, this caused the modal to intercept Enter before the select-kit could process it, triggering the primary button click prematurely. Add a check for `.select-kit` elements in `shouldTriggerClickOnEnter()` so the modal defers to select-kit's own Enter key handling. Ref - meta/t/390910
1 parent 300ece3 commit f363ae5

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

frontend/discourse/app/components/d-modal.gjs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,12 @@ export default class DModal extends Component {
100100
return false;
101101
}
102102

103-
// skip when in a form or a textarea element
103+
// skip when in a form, textarea, or select-kit element
104104
if (
105105
event.target.closest("form") ||
106106
document.activeElement?.closest("form") ||
107-
document.activeElement?.nodeName === "TEXTAREA"
107+
document.activeElement?.nodeName === "TEXTAREA" ||
108+
document.activeElement?.closest(".select-kit")
108109
) {
109110
return false;
110111
}

frontend/discourse/tests/integration/components/d-modal-test.gjs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,37 @@ module("Integration | Component | d-modal", function (hooks) {
222222

223223
assert.true(actionCalled, "pressing enter triggers the default button");
224224
});
225+
226+
test("enter inside select-kit does not trigger default action", async function (assert) {
227+
let actionCalled = false;
228+
const someAction = () => {
229+
actionCalled = true;
230+
};
231+
232+
await render(
233+
<template>
234+
<DModal @inline={{true}}>
235+
<:body>
236+
<div class="select-kit">
237+
<input class="filter-input" type="text" />
238+
</div>
239+
</:body>
240+
<:footer>
241+
<DButton
242+
@action={{someAction}}
243+
@translatedLabel="Perform action"
244+
class="btn-primary"
245+
/>
246+
</:footer>
247+
</DModal>
248+
</template>
249+
);
250+
251+
await triggerKeyEvent(".filter-input", "keydown", "Enter");
252+
253+
assert.false(
254+
actionCalled,
255+
"pressing enter inside select-kit does not trigger the default button"
256+
);
257+
});
225258
});

plugins/discourse-assign/spec/system/assign_topic_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@
5151
expect(find("#topic .assigned-to")).to have_content(admin2.username)
5252
end
5353

54+
it "can assign using keyboard navigation" do
55+
visit "/t/#{topic.id}"
56+
57+
topic_page.click_assign_topic
58+
assign_modal.select_assignee_with_keyboard(admin2)
59+
assign_modal.confirm
60+
61+
expect(assign_modal).to be_closed
62+
63+
expect(topic_page).to have_assigned(user: admin2, at_post: 2)
64+
expect(find("#topic .assigned-to")).to have_content(admin2.username)
65+
end
66+
5467
context "when prioritize_full_name_in_ux setting is enabled" do
5568
before { SiteSetting.prioritize_full_name_in_ux = true }
5669

plugins/discourse-assign/spec/system/page_objects/modals/assign.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ def assignee=(assignee)
99
find("li[data-value='#{assignee}']").click
1010
end
1111

12+
def select_assignee_with_keyboard(assignee)
13+
assignee = assignee.is_a?(Group) ? assignee.name : assignee.username
14+
input = find(".control-group input")
15+
input.fill_in(with: assignee)
16+
find("li[data-value='#{assignee}']")
17+
input.send_keys(:down)
18+
input.send_keys(:enter)
19+
end
20+
1221
def status=(status)
1322
find("#assign-status").click
1423
find("[data-value='#{status}']").click

0 commit comments

Comments
 (0)