Skip to content

Commit a12f73d

Browse files
committed
added dynamic IDs for form components and labels in context of dynamically added nested forms
1 parent a8af659 commit a12f73d

File tree

7 files changed

+32
-26
lines changed

7 files changed

+32
-26
lines changed

lib/matestack/ui/vue_js/components/form/base.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ def error_config
3434
end
3535

3636
def id
37-
ctx.id || key
37+
if ctx.id.present?
38+
"'#{ctx.id}'"
39+
else
40+
"'#{key}'+$parent.nestedFormRuntimeId"
41+
end
3842
end
3943

4044
def multiple
@@ -50,7 +54,7 @@ def placeholder
5054
def attributes
5155
(options || {}).merge({
5256
ref: "input.#{attribute_key}",
53-
id: id,
57+
":id": id,
5458
type: ctx.type,
5559
multiple: ctx.multiple,
5660
placeholder: ctx.placeholder,

lib/matestack/ui/vue_js/components/form/checkbox.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ def vue_props
3737
def render_checkbox_options
3838
checkbox_options.to_a.each do |item|
3939
input checkbox_attributes(item)
40-
label item_label(item), for: item_id(item)
40+
label item_label(item), ":for": item_id(item)
4141
end
4242
end
4343

4444
def checkbox_attributes(item)
4545
{
46-
id: item_id(item),
46+
":id": item_id(item),
4747
type: :checkbox,
4848
name: item_label(item),
4949
value: item_value(item),
@@ -57,9 +57,9 @@ def checkbox_attributes(item)
5757
end
5858

5959
def render_true_false_checkbox
60-
input true_false_checkbox_attributes.merge(type: :hidden, id: nil, value: 0)
61-
input true_false_checkbox_attributes.merge(type: :checkbox, id: item_id(1))
62-
label input_label, for: item_id(1) if input_label
60+
input true_false_checkbox_attributes.merge(type: :hidden, ":id": nil, value: 0)
61+
input true_false_checkbox_attributes.merge(type: :checkbox, ":id": item_id(1))
62+
label input_label, ":for": item_id(1) if input_label
6363
end
6464

6565
def true_false_checkbox_attributes
@@ -88,18 +88,18 @@ def checkbox_options
8888
def item_value(item)
8989
item.is_a?(Array) ? item.last : item
9090
end
91-
91+
9292
def item_label(item)
9393
item.is_a?(Array) ? item.first : item
9494
end
9595

9696
def item_id(item)
97-
"#{id}_#{item_value(item).to_s.gsub(" ", '_')}"
97+
"#{id}+'_#{item_value(item).to_s.gsub(" ", '_')}'"
9898
end
9999

100100
end
101101
end
102102
end
103103
end
104104
end
105-
end
105+
end

lib/matestack/ui/vue_js/components/form/form.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ const componentDef = {
1919
hideNestedForm: false,
2020
nestedFormRuntimeTemplates: {},
2121
nestedFormRuntimeTemplateDomElements: {},
22-
deletedNestedNewInstanceFormsCount: {}
22+
deletedNestedNewInstanceFormsCount: {},
23+
nestedFormRuntimeId: ""
2324
};
2425
},
2526
methods: {
@@ -359,9 +360,10 @@ const componentDef = {
359360
if (this.props["fields_for"] != undefined) {
360361
this.isNestedForm = true;
361362

362-
var id = parseInt(self.$el.id.replace("child-", ""))
363+
var id = parseInt(self.$el.id.replace("child-", ""));
364+
this.nestedFormRuntimeId = Math.floor(Math.random() * 1000);;
363365

364-
this.data = {}
366+
this.data = { "_destroy": false };
365367

366368
//initialize nestedForm data in parent form if required
367369
if(this.$parent.data[this.props["fields_for"]] == undefined){

lib/matestack/ui/vue_js/components/form/input.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Input < Matestack::Ui::VueJs::Components::Form::Base
88

99
def response
1010
div class: 'matestack-ui-core-form-input' do
11-
label input_label, for: id if input_label
11+
label input_label, ":for": id if input_label
1212
input input_attributes
1313
render_errors
1414
end
@@ -34,4 +34,4 @@ def vue_props
3434
end
3535
end
3636
end
37-
end
37+
end

lib/matestack/ui/vue_js/components/form/radio.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def response
1616
def render_options
1717
radio_options.to_a.each do |item|
1818
input radio_attributes(item)
19-
label item_label(item), for: item_id(item)
19+
label item_label(item), ":for": item_id(item)
2020
end
2121
end
2222

@@ -33,7 +33,7 @@ def vue_props
3333

3434
def radio_attributes(item)
3535
attributes.merge({
36-
id: item_id(item),
36+
":id": item_id(item),
3737
name: item_name(item),
3838
value: item_value(item),
3939
type: :radio,
@@ -51,13 +51,13 @@ def radio_options
5151
def item_value(item)
5252
item.is_a?(Array) ? item.last : item
5353
end
54-
54+
5555
def item_label(item)
5656
item.is_a?(Array) ? item.first : item
5757
end
5858

5959
def item_id(item)
60-
"#{id || key}_#{item_value(item)}"
60+
"#{id || key}+'_#{item_value(item)}'"
6161
end
6262

6363
def item_name(item)
@@ -73,4 +73,4 @@ def v_model_type
7373
end
7474
end
7575
end
76-
end
76+
end

lib/matestack/ui/vue_js/components/form/select.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Select < Matestack::Ui::VueJs::Components::Form::Base
88

99
def response
1010
div class: 'matestack-ui-core-form-select' do
11-
label input_label, for: id if input_label
11+
label input_label, ":for": id if input_label
1212
select select_attributes do
1313
render_options
1414
end
@@ -39,7 +39,7 @@ def vue_props
3939
def select_attributes
4040
attributes.merge({
4141
multiple: multiple,
42-
id: id,
42+
":id": id,
4343
ref: "select#{'.multiple' if multiple}.#{key}",
4444
'value-type': value_type(select_options.first),
4545
'init-value': init_value,
@@ -57,7 +57,7 @@ def select_options
5757
def item_value(item)
5858
item.is_a?(Array) ? item.last : item
5959
end
60-
60+
6161
def item_label(item)
6262
item.is_a?(Array) ? item.first : item
6363
end
@@ -85,4 +85,4 @@ def disabled_options
8585
end
8686
end
8787
end
88-
end
88+
end

lib/matestack/ui/vue_js/components/form/textarea.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Textarea < Matestack::Ui::VueJs::Components::Form::Base
88

99
def response
1010
div class: 'matestack-ui-core-form-textarea' do
11-
label input_label, for: id if input_label
11+
label input_label, ":for": id if input_label
1212
textarea textarea_attributes
1313
render_errors
1414
end
@@ -34,4 +34,4 @@ def vue_props
3434
end
3535
end
3636
end
37-
end
37+
end

0 commit comments

Comments
 (0)