|
4 | 4 | <% label ||= field_name.to_s.titleize %> |
5 | 5 | <% hint ||= nil %> |
6 | 6 | <% rounded ||= false %> |
| 7 | +<% multiple ||= false %> |
| 8 | +<% image ||= nil %> |
| 9 | +<% show_file_field = local_assigns.fetch(:show_file_field, true) %> |
| 10 | +<% show_existing = local_assigns.fetch(:show_existing, true) %> |
| 11 | +<% unique_id = "#{field_name}-#{SecureRandom.hex(4)}" %> |
7 | 12 |
|
8 | 13 | <%= f.label label, class: "block font-medium gap-1 text-gray-700 string required" %> |
9 | 14 | <% if hint %> |
10 | 15 | <%= f.hint hint.html_safe, wrap_with: { tag: :p, class: "text-gray-500 text-sm" } %> |
11 | 16 | <% end %> |
12 | | -<% if resource.public_send(field_name).attached? %> |
13 | | - <%= image_tag url_for(resource.public_send(field_name)), |
14 | | - alt: "#{label} Image", |
15 | | - id: "#{field_name}-preview", |
16 | | - class: "w-32 h-32 #{"rounded" if rounded }-full object-cover |
17 | | - border border-gray-300 shadow-sm img-#{"rounded" if rounded } #{field_name}" %> |
18 | | -<% else %> |
19 | | - <%= image_tag "missing.png", |
20 | | - id: "#{field_name}-preview", |
21 | | - class: "w-32 h-32 #{"rounded" if rounded }-full object-cover border border-dashed border-gray-300" %> |
22 | | -<% end %> |
23 | | -<div id="<%= field_name %>-filename" class="text-center text-sm text-gray-500"> |
24 | | - <%= resource.public_send(field_name).attachment&.filename.to_s %> |
25 | | -</div> |
26 | 17 |
|
27 | | -<div class="relative w-36"> |
28 | | - <%= f.file_field field_name, id: "#{field_name}-input", |
29 | | - class: "absolute inset-0 w-36 h-full opacity-0 cursor-pointer" %> |
30 | | - <button type="button" |
31 | | - class="w-full py-2 px-3 w-36 border border-gray-300 rounded-lg bg-white text-sm text-gray-700 hover:bg-gray-50"> |
32 | | - Choose file |
33 | | - </button> |
| 18 | +<div class="flex flex-col items-center gap-2"> |
| 19 | + <% if show_existing %> |
| 20 | + <% image_to_show = |
| 21 | + if image.present? |
| 22 | + image |
| 23 | + else |
| 24 | + attached = resource.public_send(field_name) |
| 25 | + if attached.is_a?(ActiveStorage::Attached::One) |
| 26 | + attached.attachment |
| 27 | + elsif attached.is_a?(ActiveStorage::Attached::Many) |
| 28 | + attached.attachments.first |
| 29 | + end |
| 30 | + end %> |
| 31 | + |
| 32 | + <% if image_to_show&.blob.present? %> |
| 33 | + <%= image_tag url_for(image_to_show), |
| 34 | + alt: "#{field_name.to_s.humanize} Image", |
| 35 | + id: "#{unique_id}-preview", |
| 36 | + class: "w-32 h-32 #{'rounded-full' if rounded} object-cover border border-gray-300 shadow-sm" %> |
| 37 | + <% else %> |
| 38 | + <%= image_tag "missing.png", |
| 39 | + id: "#{unique_id}-preview", |
| 40 | + class: "w-32 h-32 #{'rounded-full' if rounded} object-cover border border-dashed border-gray-300" %> |
| 41 | + <% end %> |
| 42 | + |
| 43 | + <div id="<%= unique_id %>-filename" class="text-center text-sm text-gray-500"> |
| 44 | + <%= if image_to_show&.blob.present? |
| 45 | + image_to_show.filename.to_s |
| 46 | + else |
| 47 | + "No file selected" |
| 48 | + end %> |
| 49 | + </div> |
| 50 | + <% end %> |
| 51 | + |
| 52 | + <% if show_file_field %> |
| 53 | + <div class="relative w-36"> |
| 54 | + <%= f.file_field field_name, |
| 55 | + id: "#{unique_id}-input", |
| 56 | + multiple: multiple, |
| 57 | + class: "absolute inset-0 w-36 h-full opacity-0 cursor-pointer" %> |
| 58 | + <button type="button" |
| 59 | + class="w-full py-2 px-3 w-36 border border-gray-300 rounded-lg bg-white text-sm text-gray-700 hover:bg-gray-50"> |
| 60 | + Choose file |
| 61 | + </button> |
| 62 | + </div> |
| 63 | + <% end %> |
34 | 64 | </div> |
| 65 | + |
| 66 | +<%#= link_to "🗑 Remove", |
| 67 | + remove_image_story_path(@story, image_id: image.id), |
| 68 | + data: { turbo_method: :delete, turbo_confirm: "Remove this image?" }, |
| 69 | + class: "text-red-600 hover:text-red-800 text-sm" if image %> |
| 70 | + |
35 | 71 | <%#= f.input :remove_image, as: :boolean, |
36 | 72 | label: "Remove file", |
37 | 73 | wrapper_html: { class: "flex justify-center items-center mb-0" }, |
|
43 | 79 |
|
44 | 80 | <script> |
45 | 81 | document.addEventListener("turbo:load", function() { |
46 | | - const input = document.getElementById("<%= field_name %>-input"); |
47 | | - const filenameDisplay = document.getElementById("<%= field_name %>-filename"); |
48 | | - const preview = document.getElementById("<%= field_name %>-preview"); |
| 82 | + const input = document.getElementById("<%= unique_id %>-input"); |
| 83 | + const filenameDisplay = document.getElementById("<%= unique_id %>-filename"); |
| 84 | + const preview = document.getElementById("<%= unique_id %>-preview"); |
49 | 85 |
|
50 | 86 | if (!input) return; |
51 | 87 |
|
|
0 commit comments