Skip to content

Commit 2ec0b8d

Browse files
authored
Merge pull request #400 from matestack/20200311_henning_unique-names-for-radio-buttons
update form_select radio buttons to have a unique name for different …
2 parents c7cd192 + 4e823a2 commit 2ec0b8d

File tree

3 files changed

+146
-10
lines changed

3 files changed

+146
-10
lines changed

app/concepts/matestack/ui/core/form/select/select.haml

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,26 @@
2626
%div{id: component_id, ref: "select.multiple.#{attr_key}", "init-value": init_value}
2727
- if options[:options].is_a?(Hash)
2828
- options[:options].each do |key, value|
29-
%input{@tag_attributes,
29+
%input{@tag_attributes.except(:id),
30+
id: id_for_option(value),
3031
type: :checkbox,
3132
"#{model_binding}": input_key,
3233
"@change": "inputChanged(\"#{attr_key}\")",
3334
"value-type": options_type,
3435
name: value,
3536
value: key}/
36-
%label=value
37+
%label{for: id_for_option(value)}=value
3738
- if options[:options].is_a?(Array)
3839
- options[:options].each do |value|
39-
%input{@tag_attributes,
40+
%input{@tag_attributes.except(:id),
41+
id: id_for_option(value),
4042
type: :checkbox,
4143
"#{model_binding}": input_key,
4244
"@change": "inputChanged(\"#{attr_key}\")",
4345
"value-type": options_type,
4446
name: value,
4547
value: value}/
46-
%label=value
48+
%label{for: id_for_option(value)}=value
4749
%span{class: "errors", "v-if": error_key }
4850
%span{class: "error", "v-for": "error in #{error_key}"}
4951
{{ error }}
@@ -53,24 +55,26 @@
5355
%div{id: component_id, ref: "select.#{attr_key}", "init-value": init_value}
5456
- if options[:options].is_a?(Hash)
5557
- options[:options].each do |key, value|
56-
%input{@tag_attributes,
58+
%input{@tag_attributes.except(:id),
5759
type: :radio,
60+
id: id_for_option(value),
5861
"#{model_binding}": input_key,
5962
"@change": "inputChanged(\"#{attr_key}\")",
6063
"value-type": options_type,
61-
name: value,
64+
name: "#{attr_key}_#{key}",
6265
value: key}/
63-
%label=value
66+
%label{for: id_for_option(value)}=value
6467
- if options[:options].is_a?(Array)
6568
- options[:options].each do |value|
66-
%input{@tag_attributes,
69+
%input{@tag_attributes.except(:id),
6770
type: :radio,
71+
id: id_for_option(value),
6872
"#{model_binding}": input_key,
6973
"@change": "inputChanged(\"#{attr_key}\")",
7074
"value-type": options_type,
71-
name: value,
75+
name: "#{attr_key}_#{value}",
7276
value: value}/
73-
%label=value
77+
%label{for: id_for_option(value)}=value
7478
%span{class: "errors", "v-if": error_key }
7579
%span{class: "error", "v-for": "error in #{error_key}"}
7680
{{ error }}

app/concepts/matestack/ui/core/form/select/select.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ class Select < Matestack::Ui::Core::Component::Static
33

44
REQUIRED_KEYS = [:options]
55

6+
def setup
7+
if @tag_attributes[:id].nil?
8+
@tag_attributes[:id] = attr_key
9+
end
10+
end
11+
612
def input_key
713
'data["' + options[:key].to_s + '"]'
814
end
@@ -68,6 +74,10 @@ def init_value
6874
end
6975
end
7076

77+
def id_for_option value
78+
return "#{@tag_attributes[:id]}_#{value}"
79+
end
80+
7181

7282
end
7383
end
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
require_relative "../../../../support/utils"
2+
include Utils
3+
4+
class TestController < ActionController::Base
5+
6+
before_action :check_params
7+
8+
def check_params
9+
expect_params(params.permit!.to_h)
10+
end
11+
12+
def expect_params(params)
13+
end
14+
15+
end
16+
17+
describe "Form Component", type: :feature, js: true do
18+
19+
before :all do
20+
class FormTestController < TestController
21+
def success_submit
22+
render json: { message: "server says: form submitted successfully" }, status: 200
23+
end
24+
25+
def success_submit_with_transition
26+
render json: {
27+
message: "server says: form submitted successfully",
28+
transition_to: form_test_page_4_path(id: 42)
29+
}, status: 200
30+
end
31+
32+
def failure_submit
33+
render json: {
34+
message: "server says: form had errors",
35+
errors: { foo: ["seems to be invalid"] }
36+
}, status: 400
37+
end
38+
end
39+
40+
Rails.application.routes.append do
41+
post '/success_form_test/:id', to: 'form_test#success_submit', as: 'form_select_radio_spec_success_form_test'
42+
post '/success_form_test_with_transition/:id', to: 'form_test#success_submit_with_transition', as: 'form_select_radio_spec_success_form_test_with_transition'
43+
post '/failure_form_test/:id', to: 'form_test#failure_submit', as: 'form_select_radio_spec_failure_form_test'
44+
end
45+
Rails.application.reload_routes!
46+
47+
class ModelFormTestController < TestController
48+
def model_submit
49+
@test_model = TestModel.create(model_params)
50+
if @test_model.errors.any?
51+
render json: {
52+
message: "server says: something went wrong!",
53+
errors: @test_model.errors
54+
}, status: :unprocessable_entity
55+
else
56+
render json: {
57+
message: "server says: form submitted successfully!"
58+
}, status: :ok
59+
end
60+
end
61+
62+
protected
63+
64+
def model_params
65+
params.require(:test_model).permit(:title, :description, :status, some_data: [], more_data: [])
66+
end
67+
end
68+
69+
Rails.application.routes.append do
70+
post '/model_form_test', to: 'model_form_test#model_submit', as: 'form_select_radio_spec_model_form_test'
71+
end
72+
Rails.application.reload_routes!
73+
end
74+
75+
before :each do
76+
allow_any_instance_of(FormTestController).to receive(:expect_params)
77+
end
78+
79+
80+
describe "Radio Button" do
81+
82+
it "generates unique names for each radio button group and value" do
83+
84+
class ExamplePage < Matestack::Ui::Page
85+
86+
def response
87+
components {
88+
form form_config, :include do
89+
form_select id: 'group-one-radio', key: :array_input_one, type: :radio, options: ['foo','bar']
90+
form_select id: 'group-two-radio', key: :array_input_two, type: :radio, options: ['foo', 'bar']
91+
form_submit do
92+
button text: 'Submit me!'
93+
end
94+
end
95+
}
96+
end
97+
98+
def form_config
99+
return {
100+
for: :my_object,
101+
method: :post,
102+
path: :form_select_radio_spec_success_form_test_path,
103+
params: {
104+
id: 42
105+
}
106+
}
107+
end
108+
109+
end
110+
111+
visit '/example'
112+
113+
expect(page).to have_selector('#group-one-radio_foo[name="array_input_one_foo"]')
114+
expect(page).to have_selector('#group-one-radio_bar[name="array_input_one_bar"]')
115+
116+
expect(page).to have_selector('#group-two-radio_foo[name="array_input_two_foo"]')
117+
expect(page).to have_selector('#group-two-radio_bar[name="array_input_two_bar"]')
118+
end
119+
120+
end
121+
122+
end

0 commit comments

Comments
 (0)