Skip to content

Commit c1e20df

Browse files
committed
Ensure selected topic select option for current page
1 parent 5a10add commit c1e20df

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed

app/javascript/controllers/current-page.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,19 @@ export default class extends Controller {
88
connect() {
99
console.log('Connected');
1010

11-
this.updateActiveLink();
12-
this.updateSelectedOption();
13-
this.updateActiveLink = this.updateActiveLink.bind(this);
11+
this.updateActive();
12+
this.updateActive = this.updateActive.bind(this);
1413

15-
window.addEventListener('turbo:before-render', this.updateActiveLink);
16-
window.addEventListener('turbo:before-frame-render', this.updateActiveLink);
14+
window.addEventListener('turbo:before-render', this.updateActive);
15+
window.addEventListener('turbo:before-frame-render', this.updateActive);
1716
}
1817

1918
disconnect() {
20-
window.removeEventListener('turbo:before-render', this.updateActiveLink);
21-
window.removeEventListener(
22-
'turbo:before-frame-render',
23-
this.updateActiveLink,
24-
);
19+
window.removeEventListener('turbo:before-render', this.updateActive);
20+
window.removeEventListener('turbo:before-frame-render', this.updateActive);
2521
}
2622

27-
updateActiveLink(event) {
23+
updateActive(event) {
2824
console.log('updateActiveLink');
2925
this.element.querySelectorAll('a').forEach((link) => {
3026
if (link.getAttribute('href') === this.currentUrl) {
@@ -35,9 +31,7 @@ export default class extends Controller {
3531
link.closest('li').classList.remove('active');
3632
}
3733
});
38-
}
3934

40-
updateSelectedOption() {
4135
this.element.querySelectorAll('option').forEach((option) => {
4236
if (option.getAttribute('value') === this.currentUrl) {
4337
option.selected = true;

app/views/topics/nav.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Topics
22
class Nav < ApplicationComponent
33
include Phlex::Rails::Helpers::CurrentPage
44
include Phlex::Rails::Helpers::DOMID
5+
include Phlex::Rails::Helpers::Request
56
include PhlexConcerns::FlexBlock
67

78
attr_reader :topics, :attributes
@@ -33,7 +34,7 @@ def topics_select
3334
topics.each do |topic|
3435
option(
3536
value: topic_path(topic),
36-
selected: current_page?(topic_path(topic))
37+
selected: request&.path == topic_path(topic)
3738
) do
3839
topic.name
3940
end

0 commit comments

Comments
 (0)