Skip to content

Commit 313cb61

Browse files
committed
feat(LocaleSelect): Make auto_submit toggable
We need to be able to disable the auto-submit feature if we use the LocaleSelect in a form that submits via a button (ie. the users form of alchemy-devise). (cherry picked from commit 5f3c4e4)
1 parent a01a6b0 commit 313cb61

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

app/components/alchemy/admin/locale_select.rb

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,19 @@ module Alchemy
22
module Admin
33
# Renders a locale select tag for switching the backend locale.
44
class LocaleSelect < ViewComponent::Base
5-
attr_reader :name
5+
attr_reader :name, :auto_submit
66

7-
def initialize(name = :admin_locale)
7+
def initialize(name = :admin_locale, auto_submit: true)
88
@name = name
9+
@auto_submit = auto_submit
910
end
1011

1112
def call
1213
form_tag(helpers.url_for, method: :get) do
13-
content_tag("alchemy-auto-submit") do
14-
select_tag(
15-
name,
16-
options_for_select(
17-
translations_for_select,
18-
::I18n.locale
19-
)
20-
)
14+
if auto_submit
15+
content_tag("alchemy-auto-submit", locale_select)
16+
else
17+
locale_select
2118
end
2219
end
2320
end
@@ -28,6 +25,16 @@ def render?
2825

2926
private
3027

28+
def locale_select
29+
select_tag(
30+
name,
31+
options_for_select(
32+
translations_for_select,
33+
::I18n.locale
34+
)
35+
)
36+
end
37+
3138
def available_locales
3239
@_available_locales ||= Alchemy::I18n.available_locales.sort!
3340
end

spec/components/alchemy/admin/locale_select_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,18 @@
3333
expect(page).to have_selector("select[name='language']")
3434
end
3535
end
36+
37+
it "should return a select with auto submit wrapper" do
38+
expect(page).to have_selector("alchemy-auto-submit select[name='admin_locale']")
39+
end
40+
41+
context "with auto_submit false" do
42+
let(:render) { render_inline described_class.new(:admin_locale, auto_submit: false) }
43+
44+
it "should return a select without auto submit wrapper" do
45+
expect(page).not_to have_selector("alchemy-auto-submit")
46+
expect(page).to have_selector("select[name='admin_locale']")
47+
end
48+
end
3649
end
3750
end

0 commit comments

Comments
 (0)