|
| 1 | +<% required ||= false |
| 2 | + content ||= {} |
| 3 | + translated_content ||= {} |
| 4 | + right_to_left ||= false |
| 5 | + errors ||= [] |
| 6 | + required_attributes ||= [] |
| 7 | + element_properties = schema.fetch("fields") %> |
| 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 | + |
| 32 | +<%= render "govuk_publishing_components/components/fieldset", |
| 33 | + { legend_text: "#{schema['title']}#{required ? ' (required)' : ''}", |
| 34 | + heading_size: "l", |
| 35 | + id: path.form_control_id } do %> |
| 36 | + <% |
| 37 | + # TODO: make the array key (`social_media_links`) dynamic |
| 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]" |
| 40 | + { |
| 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 | + }), |
| 46 | + } |
| 47 | + end |
| 48 | + %> |
| 49 | + |
| 50 | + <%= render "govuk_publishing_components/components/add_another", { |
| 51 | + fieldset_legend: schema["title"], |
| 52 | + add_button_text: "Add another", |
| 53 | + empty_fields: true, |
| 54 | + items: items, |
| 55 | + empty: render_fields.call({}, items.length), |
| 56 | + } %> |
| 57 | +<% end %> |
0 commit comments