|
6 | 6 | required_attributes ||= [] |
7 | 7 | element_properties = schema.fetch("fields") %> |
8 | 8 |
|
| 9 | +<% |
| 10 | + render_fields = ->(social_media_link, index) do |
| 11 | + element_fields = capture do |
| 12 | + element_properties.each do |property_key, property_schema| |
| 13 | + block = default_array.block_factory.build_block(property_schema["block"]) |
| 14 | +%> |
| 15 | + <%= render block, { |
| 16 | + schema: property_schema, |
| 17 | + content: social_media_link[property_key], |
| 18 | + # TODO: support translations |
| 19 | + # translated_content: social_media_link[property_key], |
| 20 | + path: ConfigurableContentBlocks::Path.new(["social_media_links", index, property_key]), |
| 21 | + required: required_attributes.include?(property_key), |
| 22 | + required_attributes: property_schema["block"] == "default_array" ? required_attributes : [], |
| 23 | + right_to_left: right_to_left, |
| 24 | + errors: errors, |
| 25 | + } %> |
| 26 | +<% |
| 27 | + end # element_properties.each loop |
| 28 | + end # element_fields = capture |
| 29 | + end # render_fields definition |
| 30 | +%> |
| 31 | + |
9 | 32 | <%= render "govuk_publishing_components/components/fieldset", |
10 | 33 | { legend_text: "#{schema['title']}#{required ? ' (required)' : ''}", |
11 | 34 | heading_size: "l", |
12 | 35 | id: path.form_control_id } do %> |
13 | 36 | <% |
14 | 37 | # TODO: make the array key (`social_media_links`) dynamic |
15 | | - items = content["social_media_links"].map do |social_media_link| |
16 | | - %> |
17 | | - <% element_fields = capture do %> |
18 | | - <% element_properties.each do |property_key, property_schema| %> |
19 | | - <% #raise [property_key, content.to_h, schema.inspect].inspect %> |
20 | | - <% block = default_array.block_factory.build_block(property_schema["block"]) %> |
21 | | - <%= render block, { |
22 | | - schema: property_schema, |
23 | | - content: social_media_link[property_key], |
24 | | - # translated_content: social_media_link[property_key], |
25 | | - path: ConfigurableContentBlocks::Path.new([property_key]), |
26 | | - required: required_attributes.include?(property_key), |
27 | | - required_attributes: property_schema["block"] == "default_array" ? required_attributes : [], |
28 | | - right_to_left: right_to_left, |
29 | | - errors: errors, |
30 | | - } %> |
31 | | - <% end %> |
32 | | - <% end %> |
33 | | - |
34 | | - <% |
| 38 | + items = content["social_media_links"].map.with_index do |social_media_link, index| |
| 39 | + # raise path.form_control_name.inspect # "edition[block_content][social_media_links]" |
35 | 40 | { |
36 | | - fields: element_fields, |
37 | | - # TODO: the noJS experience: |
38 | | - # destroy_checkbox: render("govuk_publishing_components/components/checkboxes", { |
39 | | - # name: "#{item_path.form_control_name}[_destroy]", |
40 | | - # items: [{ label: "Remove", value: "1" }], |
41 | | - # }), |
| 41 | + fields: render_fields.call(social_media_link, index), |
| 42 | + destroy_checkbox: render("govuk_publishing_components/components/checkboxes", { |
| 43 | + name: "#{path.form_control_name}[#{index}][_destroy]", |
| 44 | + items: [{ label: "Remove", value: "1" }], |
| 45 | + }), |
42 | 46 | } |
43 | | - %> |
44 | | - <% end %> |
| 47 | + end |
| 48 | + %> |
45 | 49 |
|
46 | 50 | <%= render "govuk_publishing_components/components/add_another", { |
47 | 51 | fieldset_legend: schema["title"], |
48 | 52 | add_button_text: "Add another", |
49 | 53 | empty_fields: true, |
50 | 54 | items: items, |
51 | | - # TODO: the noJS experience: |
52 | | - empty: "TBC", # render_fields.call({}, path.push(items.length.to_s), []), |
| 55 | + empty: render_fields.call({}, items.length), |
53 | 56 | } %> |
54 | 57 | <% end %> |
0 commit comments