Skip to content

Commit d791c48

Browse files
committed
added nested form support for bs input components
1 parent 9688966 commit d791c48

File tree

13 files changed

+117
-24
lines changed

13 files changed

+117
-24
lines changed

lib/matestack/ui/bootstrap/form/checkbox.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Matestack::Ui::Bootstrap::Form::Checkbox < Matestack::Ui::VueJs::Component
1010

1111
def response
1212
div class: "matestack-ui-bootstrap-form-checkbox" do
13-
label input_label, class: "form-label", for: attribute_key if input_label && multiple?
13+
label input_label, class: "form-label", ":for": id if input_label && multiple?
1414
render_options
1515
render_errors
1616
render_form_text unless context.form_text.nil? # otherwise renders empty div
@@ -45,7 +45,7 @@ def render_options
4545
else
4646
checkbox_wrapper do
4747
input true_false_checkbox_attributes.merge(type: :hidden, id: nil, value: 0)
48-
input true_false_checkbox_attributes.merge(type: :checkbox, id: item_id(1)).merge(bootstrap_attributes)
48+
input true_false_checkbox_attributes.merge(type: :checkbox, ":id": item_id(1)).merge(bootstrap_attributes)
4949

5050
bootstrap_label text: input_label, for_input: item_id(1)
5151
end
@@ -62,7 +62,7 @@ def checkbox_wrapper(options = {}, &block)
6262
end
6363

6464
def bootstrap_label(text:, for_input:)
65-
label text, for: for_input, class: 'form-check-label'
65+
label text, ":for": for_input, class: 'form-check-label'
6666
end
6767

6868
def render_errors
@@ -82,7 +82,7 @@ def checkbox_options
8282
end
8383

8484
def render_form_text
85-
div id: "form_text_for_#{attribute_key}", class: "form-text" do
85+
div class: "form-text form-text-for-#{attribute_key}" do
8686
plain context.form_text
8787
end
8888
end

lib/matestack/ui/bootstrap/form/input.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ class Matestack::Ui::Bootstrap::Form::Input < Matestack::Ui::VueJs::Components::
1616

1717
def response
1818
div class: "matestack-ui-bootstrap-input" do
19-
label input_label, for: attribute_key, class: "form-label" if input_label
19+
label input_label, ":for": id, class: "form-label" if input_label
2020
case context.type
2121
when :range
2222
input options.merge(input_attributes).merge(bootstrap_range_attributes)
2323
if context.show_value
24-
div id: attribute_key, class: "form-text" do
24+
div class: "form-text form-text-for-#{attribute_key}" do
2525
plain "{{ data['#{attribute_key}'] }}"
2626
end
2727
end
@@ -37,7 +37,6 @@ def response
3737

3838
def bootstrap_input_attributes
3939
{
40-
id: (options[:id] || attribute_key),
4140
class: (options[:class] || "") << (" form-control"),
4241
disabled: context.disabled,
4342
min: context.min,
@@ -111,7 +110,7 @@ def input_error_class
111110
end
112111

113112
def render_form_text
114-
div id: "form_text_for_#{attribute_key}", class: "form-text" do
113+
div class: "form-text form-text-for-#{attribute_key}" do
115114
plain context.form_text
116115
end
117116
end

lib/matestack/ui/bootstrap/form/radio.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ class Matestack::Ui::Bootstrap::Form::Radio < Matestack::Ui::VueJs::Components::
1010

1111
def response
1212
div class: "matestack-ui-bootstrap-radio" do
13-
label input_label, class: "form-label" if input_label
13+
label input_label, class: "form-label", ":for": id if input_label
1414

1515
radio_options.to_a.each_with_index do |item, index|
1616
div class: "form-check #{'form-check-inline' if context.variant == :inline}" do
1717
input options.merge(radio_attributes(item)).merge(bootstrap_radio_attributes)
18-
label item_label(item), class: "form-check-label", for: item_id(item_value(item))
18+
label item_label(item), class: "form-check-label", ":for": item_id(item_value(item))
1919
if index == radio_options.to_a.size - 1
2020
render_errors
2121
end
@@ -37,7 +37,7 @@ def bootstrap_radio_attributes
3737
end
3838

3939
def render_form_text
40-
div id: "form_text_for_#{attribute_key}", class: "form-text" do
40+
div class: "form-text form-text-for-#{attribute_key}" do
4141
plain context.form_text
4242
end
4343
end

lib/matestack/ui/bootstrap/form/select.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Matestack::Ui::Bootstrap::Form::Select < Matestack::Ui::VueJs::Components:
1010

1111
def response
1212
div class: "matestack-ui-bootstrap-form-select" do
13-
label input_label, for: attribute_key, class: "form-label" if input_label
13+
label input_label, ":for": id, class: "form-label" if input_label
1414
select select_attributes.merge(bootstrap_select_attributes) do
1515
render_options
1616
end
@@ -55,7 +55,7 @@ def input_error_class
5555
end
5656

5757
def render_form_text
58-
div id: "form_text_for_#{attribute_key}", class: "form-text" do
58+
div class: "form-text form-text-for-#{attribute_key}" do
5959
plain context.form_text
6060
end
6161
end

lib/matestack/ui/bootstrap/form/switch.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Matestack::Ui::Bootstrap::Form::Switch < Matestack::Ui::Bootstrap::Form::C
1010

1111
def response
1212
div class: "matestack-ui-bootstrap-switch" do
13-
label input_label, class: "form-label", for: attribute_key if input_label && multiple?
13+
label input_label, class: "form-label", ":for": id if input_label && multiple?
1414
render_options
1515
render_errors
1616
plain context.form_text

lib/matestack/ui/bootstrap/form/textarea.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Matestack::Ui::Bootstrap::Form::Textarea < Matestack::Ui::VueJs::Component
1212

1313
def response
1414
div class: "matestack-ui-bootstrap-textarea" do
15-
label input_label, for: attribute_key, class: "form-label" if input_label
15+
label input_label, ":for": id, class: "form-label" if input_label
1616
textarea options.merge(textarea_attributes).merge(bootstrap_textarea_attributes)
1717
render_errors
1818
render_form_text if context.form_text
@@ -21,7 +21,6 @@ def response
2121

2222
def bootstrap_textarea_attributes
2323
{
24-
id: (options[:id] || attribute_key),
2524
class: (options[:class] || "") << (" form-control"),
2625
rows: context.rows,
2726
cols: context.cols,
@@ -42,7 +41,7 @@ def input_error_class
4241
end
4342

4443
def render_form_text
45-
div id: "form_text_for_#{attribute_key}", class: "form-text" do
44+
div class: "form-text form-text-for-#{attribute_key}" do
4645
plain context.form_text
4746
end
4847
end

spec/test/form/checkbox_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@
228228
end
229229
end
230230
visit example_path
231-
expect(page).to have_xpath('//form//div[@id="form_text_for_foo" and contains(@class, "form-text") and contains(text(), "some notes")]')
231+
expect(page).to have_xpath('//form//div[contains(@class, "form-text-for-foo") and contains(@class, "form-text") and contains(text(), "some notes")]')
232232
end
233233

234234
it 'renders bootstrap checkbox button as inline variant' do

spec/test/form/input_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
end
101101
end
102102
visit example_path
103-
expect(page).to have_xpath('//form//div[@id="form_text_for_foo" and contains(@class, "form-text") and contains(text(), "some notes")]')
103+
expect(page).to have_xpath('//form//div[contains(@class, "form-text-for-foo") and contains(@class, "form-text") and contains(text(), "some notes")]')
104104
end
105105

106106
it 'renders basic bootstrap input field accepting float numbers' do
@@ -156,7 +156,7 @@
156156
visit example_path
157157

158158
expect(page).to have_selector('form > div.matestack-ui-bootstrap-input > input[type="range"][min="5"][max="11"][step="2"]')
159-
expect(page).to have_xpath('//form//div[@id="form_text_for_foo" and contains(@class, "form-text") and contains(text(), "some notes")]')
159+
expect(page).to have_xpath('//form//div[contains(@class, "form-text-for-foo") and contains(@class, "form-text") and contains(text(), "some notes")]')
160160
end
161161

162162
end
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
require 'rails_helper'
2+
require_relative "../../support/form_test_controller"
3+
4+
RSpec.describe "Nested Form Support Spec", type: :feature, js: true do
5+
include Utils
6+
7+
before :all do
8+
Rails.application.routes.append do
9+
scope "nested_form_support_spec" do
10+
post '/input_success_form_test', to: 'form_test#success_submit', as: 'input_success_form_test'
11+
post '/input_failure_form_test', to: 'form_test#failure_submit', as: 'input_failure_form_test'
12+
end
13+
end
14+
Rails.application.reload_routes!
15+
end
16+
17+
before :each do
18+
allow_any_instance_of(FormTestController).to receive(:expect_params)
19+
end
20+
21+
it 'properly renders all input components with dynamically adjusted IDs' do
22+
form_config = get_form_config(path: input_success_form_test_path)
23+
24+
ExamplePage.define_method(:order_item_form) do |order_item = OrderItem.new|
25+
form_fields_for order_item, key: :order_items_attributes do
26+
27+
bs_form_input key: :a, type: :number, label: "Label A"
28+
bs_form_select key: :b, options: [1, 2], label: "Label B"
29+
bs_form_switch key: :c, options: [1, 2], label: "Label C"
30+
bs_form_switch key: :d, label: "Label D"
31+
bs_form_radio key: :e, options: [1, 2], label: "Label E"
32+
bs_form_textarea key: :f, type: :number, label: "Label F"
33+
bs_form_checkbox key: :g, label: "Label G"
34+
bs_form_checkbox key: :h, options: [1, 2], label: "Label H"
35+
36+
form_fields_for_remove_item do
37+
bs_btn type: :button, variant: :danger do
38+
bs_icon name: "trash"
39+
end
40+
end
41+
end
42+
end
43+
44+
matestack_render do
45+
matestack_form form_config do
46+
47+
form_fields_for_add_item key: :order_items_attributes, prototype: method(:order_item_form) do
48+
bs_btn "add", type: :button, variant: :primary, outline: true, class: "mt-2"
49+
end
50+
51+
bs_form_submit text: "Submit"
52+
end
53+
end
54+
55+
visit example_path
56+
57+
click_on "add"
58+
click_on "add"
59+
60+
[0, 1].each do |i|
61+
expect(page).to have_selector("form > div > div.matestack-form-fields-for#order_items_attributes_child_#{i} > div.matestack-ui-bootstrap-input > label[for=\"a_order_items_attributes_child_#{i}\"]")
62+
expect(page).to have_selector("form > div > div.matestack-form-fields-for#order_items_attributes_child_#{i} > div.matestack-ui-bootstrap-input > input.form-control#a_order_items_attributes_child_#{i}")
63+
64+
expect(page).to have_selector("form > div > div.matestack-form-fields-for#order_items_attributes_child_#{i} > div.matestack-ui-bootstrap-form-select > label[for=\"b_order_items_attributes_child_#{i}\"]")
65+
expect(page).to have_selector("form > div > div.matestack-form-fields-for#order_items_attributes_child_#{i} > div.matestack-ui-bootstrap-form-select > select.form-select#b_order_items_attributes_child_#{i}")
66+
67+
expect(page).to have_selector("form > div > div.matestack-form-fields-for#order_items_attributes_child_#{i} > div.matestack-ui-bootstrap-switch > label[for=\"c_order_items_attributes_child_#{i}\"]")
68+
expect(page).to have_selector("form > div > div.matestack-form-fields-for#order_items_attributes_child_#{i} > div.matestack-ui-bootstrap-switch > div.form-switch > input.form-check-input#c_order_items_attributes_child_#{i}_1")
69+
expect(page).to have_selector("form > div > div.matestack-form-fields-for#order_items_attributes_child_#{i} > div.matestack-ui-bootstrap-switch > div.form-switch > input.form-check-input#c_order_items_attributes_child_#{i}_2")
70+
71+
expect(page).to have_selector("form > div > div.matestack-form-fields-for#order_items_attributes_child_#{i} > div.matestack-ui-bootstrap-switch > div.form-switch > input.form-check-input#d_order_items_attributes_child_#{i}_1")
72+
expect(page).to have_selector("form > div > div.matestack-form-fields-for#order_items_attributes_child_#{i} > div.matestack-ui-bootstrap-switch > div.form-switch > label[for=\"d_order_items_attributes_child_#{i}_1\"]")
73+
74+
expect(page).to have_selector("form > div > div.matestack-form-fields-for#order_items_attributes_child_#{i} > div.matestack-ui-bootstrap-radio > label[for=\"e_order_items_attributes_child_#{i}\"]")
75+
expect(page).to have_selector("form > div > div.matestack-form-fields-for#order_items_attributes_child_#{i} > div.matestack-ui-bootstrap-radio > div.form-check > input.form-check-input#e_order_items_attributes_child_#{i}_1")
76+
expect(page).to have_selector("form > div > div.matestack-form-fields-for#order_items_attributes_child_#{i} > div.matestack-ui-bootstrap-radio > div.form-check > label[for=\"e_order_items_attributes_child_#{i}_1\"]")
77+
expect(page).to have_selector("form > div > div.matestack-form-fields-for#order_items_attributes_child_#{i} > div.matestack-ui-bootstrap-radio > div.form-check > input.form-check-input#e_order_items_attributes_child_#{i}_2")
78+
expect(page).to have_selector("form > div > div.matestack-form-fields-for#order_items_attributes_child_#{i} > div.matestack-ui-bootstrap-radio > div.form-check > label[for=\"e_order_items_attributes_child_#{i}_2\"]")
79+
80+
expect(page).to have_selector("form > div > div.matestack-form-fields-for#order_items_attributes_child_#{i} > div.matestack-ui-bootstrap-textarea > label[for=\"f_order_items_attributes_child_#{i}\"]")
81+
expect(page).to have_selector("form > div > div.matestack-form-fields-for#order_items_attributes_child_#{i} > div.matestack-ui-bootstrap-textarea > textarea.form-control#f_order_items_attributes_child_#{i}")
82+
83+
expect(page).to have_selector("form > div > div.matestack-form-fields-for#order_items_attributes_child_#{i} > div.matestack-ui-bootstrap-form-checkbox > div.form-check > input.form-check-input#g_order_items_attributes_child_#{i}_1")
84+
expect(page).to have_selector("form > div > div.matestack-form-fields-for#order_items_attributes_child_#{i} > div.matestack-ui-bootstrap-form-checkbox > div.form-check > label[for=\"g_order_items_attributes_child_#{i}_1\"]")
85+
86+
expect(page).to have_selector("form > div > div.matestack-form-fields-for#order_items_attributes_child_#{i} > div.matestack-ui-bootstrap-form-checkbox > label[for=\"h_order_items_attributes_child_#{i}\"]")
87+
expect(page).to have_selector("form > div > div.matestack-form-fields-for#order_items_attributes_child_#{i} > div.matestack-ui-bootstrap-form-checkbox > div.form-check > input.form-check-input#h_order_items_attributes_child_#{i}_1")
88+
expect(page).to have_selector("form > div > div.matestack-form-fields-for#order_items_attributes_child_#{i} > div.matestack-ui-bootstrap-form-checkbox > div.form-check > input.form-check-input#h_order_items_attributes_child_#{i}_2")
89+
end
90+
91+
end
92+
93+
94+
95+
end

spec/test/form/radio_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@
175175
end
176176
end
177177
visit example_path
178-
expect(page).to have_xpath('//form//div[@id="form_text_for_foo" and contains(@class, "form-text") and contains(text(), "some notes")]')
178+
expect(page).to have_xpath('//form//div[contains(@class, "form-text-for-foo") and contains(@class, "form-text") and contains(text(), "some notes")]')
179179
end
180180

181181
it 'renders bootstrap radio buttons as inline variant' do

0 commit comments

Comments
 (0)