|
| 1 | +<% required ||= false |
| 2 | + content ||= [] |
| 3 | + errors ||= [] |
| 4 | + items = content.is_a?(Hash) ? content.values : Array(content) |
| 5 | + properties = schema.dig("items", "properties") || {} |
| 6 | + social_media_services = SocialMediaService.all |
| 7 | + |
| 8 | + render_fields = ->(item, item_path, item_errors) do |
| 9 | + capture do |
| 10 | + properties.each do |key, prop| |
| 11 | + field_path = item_path.push(key) |
| 12 | + field_errors = item_errors.select { |e| e.attribute.to_s.end_with?(".#{key}") } |
| 13 | + |
| 14 | + if prop["input_type"] == "social_media_service_select" |
| 15 | + concat(render("govuk_publishing_components/components/select", { |
| 16 | + id: field_path.form_control_id, |
| 17 | + label: prop["title"], |
| 18 | + name: "#{item_path.form_control_name}[#{key}]", |
| 19 | + heading_size: "s", |
| 20 | + options: [{ text: "", value: "" }] + |
| 21 | + social_media_services.map do |service| |
| 22 | + { |
| 23 | + text: service.name, |
| 24 | + value: service.id, |
| 25 | + selected: item[key].to_s == service.id.to_s, |
| 26 | + } |
| 27 | + end, |
| 28 | + full_width: true, |
| 29 | + error_items: field_errors.map { |e| { text: e.message } }, |
| 30 | + })) |
| 31 | + else |
| 32 | + concat(render("govuk_publishing_components/components/input", { |
| 33 | + id: field_path.form_control_id, |
| 34 | + label: { text: prop["title"] }, |
| 35 | + name: "#{item_path.form_control_name}[#{key}]", |
| 36 | + value: item[key], |
| 37 | + error_items: field_errors.map { |e| { text: e.message } }, |
| 38 | + })) |
| 39 | + end |
| 40 | + end |
| 41 | + end |
| 42 | + end %> |
| 43 | + |
| 44 | +<%= render "govuk_publishing_components/components/fieldset", { |
| 45 | + legend_text: "#{schema['title']}#{required ? ' (required)' : ''}", |
| 46 | + heading_size: "l", |
| 47 | +} do %> |
| 48 | + <div class="app-c-array-of-hashes"> |
| 49 | + <%= render "govuk_publishing_components/components/add_another", { |
| 50 | + fieldset_legend: "Account", |
| 51 | + add_button_text: "Add account", |
| 52 | + empty_fields: true, |
| 53 | + items: items.each_with_index.filter_map do |item, index| |
| 54 | + next if item["_destroy"] == "1" |
| 55 | + |
| 56 | + item_path = path.push(index.to_s) |
| 57 | + item_errors = errors.select { |e| e.attribute.to_s.start_with?(item_path.validation_error_attribute) } |
| 58 | + |
| 59 | + { |
| 60 | + fields: render_fields.call(item, item_path, item_errors), |
| 61 | + destroy_checkbox: render("govuk_publishing_components/components/checkboxes", { |
| 62 | + name: "#{item_path.form_control_name}[_destroy]", |
| 63 | + items: [{ label: "Remove", value: "1" }], |
| 64 | + }), |
| 65 | + } |
| 66 | + end, |
| 67 | + empty: render_fields.call({}, path.push(items.length.to_s), []), |
| 68 | + } %> |
| 69 | + </div> |
| 70 | + <style> |
| 71 | + .app-c-array-of-hashes .js-add-another__add-button { |
| 72 | + margin-top: 20px; |
| 73 | + } |
| 74 | + </style> |
| 75 | +<% end %> |
0 commit comments