diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index 18f3cd7adb..8e78a94247 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -200,33 +200,6 @@ div.low_priority_warning { text-align: center; } -.distribution-title { - display: flex; -} - -legend.with-request { - float: none; - width: 80%; - display: inline-block; -} - -div.distribution-request-unit { - display: inline-block; - flex: 1; - text-align: right; - margin-right: 20px; - font-size: 1.5em; -} - -.li-requested { - font-size: 1.5em; - width: 100px; - min-width: 100px; - text-align: right; - margin-right: 20px; - white-space: nowrap; -} - .git-version { color: gray; } diff --git a/app/assets/stylesheets/resources/distributions.scss b/app/assets/stylesheets/resources/distributions.scss new file mode 100644 index 0000000000..908a4fa257 --- /dev/null +++ b/app/assets/stylesheets/resources/distributions.scss @@ -0,0 +1,47 @@ +.distribution-line-items { + .distribution-line-item-row { + margin-top: .5rem; + } + + .distribution-line-item-inputs { + display: grid; + grid-template-columns: minmax(0, 1fr) 11rem; + gap: 1rem; + + &.with-request { + grid-template-columns: minmax(0, 1fr) 11rem 7rem; + } + } + + .li-name, + .li-quantity, + .li-requested { + min-width: 0; + } + + .distribution-line-item-inputs label, + .form-label { + display: block; + font-weight: bold; + margin-bottom: .5rem; + white-space: nowrap; + } + + .distribution-requested-value { + display: flex; + align-items: center; + min-height: calc(1.5em + .75rem + 2px); + } + + @media (max-width: 575.98px) { + .distribution-line-item-inputs, + .distribution-line-item-inputs.with-request { + grid-template-columns: minmax(0, 1fr); + gap: 0; + } + + .li-requested { + margin-bottom: 1rem; + } + } +} diff --git a/app/views/distributions/_form.html.erb b/app/views/distributions/_form.html.erb index ea43d69faa..41927efaf1 100644 --- a/app/views/distributions/_form.html.erb +++ b/app/views/distributions/_form.html.erb @@ -43,21 +43,15 @@ <%= f.input :comment, label: "Comment" %> -
-
- Items in this distribution - <% if distribution.request %> -
Quantity - Total Units
-
Requested
- <% end %> -
+
+ Items in this distribution
- <%= render 'line_items/line_item_fields', form: f, changes_disallowed: @changes_disallowed %> + <%= render 'line_items/line_item_fields', form: f, changes_disallowed: @changes_disallowed, distribution: true %>
<% unless @changes_disallowed %> <% end %> diff --git a/app/views/line_items/_line_item_fields.html.erb b/app/views/line_items/_line_item_fields.html.erb index 99f6adb8a8..a3f3968724 100644 --- a/app/views/line_items/_line_item_fields.html.erb +++ b/app/views/line_items/_line_item_fields.html.erb @@ -1,11 +1,12 @@ <% if local_assigns[:changes_disallowed] %>
This <%= form.object.class.name.downcase %> is too old to edit inventory. You can only change non-inventory fields.
<% end %> +<% distribution = local_assigns[:distribution] %> <%= form.simple_fields_for :line_items, defined?(object) ? object : nil do |field| %> <% requested = field.object.requested_item %>
-
-
+
+
<% if requested.blank? %> <% unless local_assigns[:changes_disallowed] %>
@@ -16,35 +17,40 @@ <% end %> <% end %> -
+
<%= field.input :item_id, disabled: requested.present? || local_assigns[:changes_disallowed], collection: @item_labels_with_quantities || @items, prompt: "Choose an item", include_blank: "", - label: false, + label: distribution ? "Item" : false, input_html: { class: "my-0 line_item_name", "data-controller": "select2", "data-select2-config-value": '{"width": "100%"}' } %> <% if requested.present? %> <%= field.input :item_id, as: :hidden %> <% end %> -
+
<%= field.input :quantity, as: :string, disabled: local_assigns[:changes_disallowed], placeholder: "Quantity", - label: false, + label: distribution ? "Quantity - Total Units" : false, input_html: { class: "quantity my-0", data: { quantity: "" } } %>
<% if form.object.respond_to?(:request) && form.object.request %> -
- <% if requested&.request_unit.present? %> - <%= pluralize(requested.quantity, requested.request_unit) %> - <% elsif requested %> - <%= requested.quantity %> - <% else %> - N/A +
+ <% if distribution %> +
Requested
<% end %> +
+ <% if requested&.request_unit.present? %> + <%= pluralize(requested.quantity, requested.request_unit) %> + <% elsif requested %> + <%= requested.quantity %> + <% else %> + N/A + <% end %> +
<% end %>
diff --git a/spec/system/distribution_line_item_layout_system_spec.rb b/spec/system/distribution_line_item_layout_system_spec.rb new file mode 100644 index 0000000000..51aae1c246 --- /dev/null +++ b/spec/system/distribution_line_item_layout_system_spec.rb @@ -0,0 +1,89 @@ +RSpec.describe "Distribution line item layout", type: :system do + let(:organization) { create(:organization) } + let(:user) { create(:user, organization:) } + let(:partner) { create(:partner, organization:, name: "Layout Partner") } + let(:storage_location) { create(:storage_location, organization:, name: "Layout Storage") } + let(:item) { create(:item, organization:, name: "Layout Diapers") } + + before do + sign_in(user) + TestInventory.create_inventory(organization, storage_location.id => {item.id => 100}) + end + + shared_examples "responsive line items" do + it "keeps each label with its field at narrow and desktop widths" do + select "Layout Storage", from: "From storage location" + expect(page).to have_css("#__add_line_item:not(.disabled)") + [375, 600, 1200].each do |width| + current_window.resize_to(width, 900) + expect(page).to have_css(".li-name .select2-container") + + layout = page.evaluate_script(<<~JS) + (() => { + const row = document.querySelector('.distribution-line-item-inputs'); + const bounds = element => element.getBoundingClientRect(); + const fields = [...row.children]; + return { + labelsFit: fields.every(field => { + const label = field.querySelector('label, .form-label'); + const text = document.createRange(); + text.selectNodeContents(label); + return bounds(label).height < parseFloat(getComputedStyle(label).fontSize) * 2 && + label.scrollWidth <= field.clientWidth + 1 && + bounds(text).left >= bounds(field).left - 1 && + bounds(text).right <= bounds(field).right + 1; + }), + gridFitsViewport: bounds(row).left >= 0 && + bounds(row).right <= document.documentElement.clientWidth + 1, + fieldsFit: fields.every(field => bounds(field).left >= bounds(row).left && + bounds(field).right <= bounds(row).right + 1), + labelsAlign: fields.every(field => { + const label = field.querySelector('label, .form-label'); + const value = field.querySelector('.select2-container, input, .distribution-requested-value'); + return Math.abs(bounds(label).left - bounds(value).left) <= 1 && + bounds(label).bottom <= bounds(value).top + 1; + }), + dropdownFits: bounds(row.querySelector('.select2-container')).width <= + bounds(row.querySelector('.li-name')).width + 1 + }; + })() + JS + + expect(layout).to eq("labelsFit" => true, "gridFitsViewport" => true, "fieldsFit" => true, "labelsAlign" => true, "dropdownFits" => true) + end + + current_window.resize_to(375, 900) + click_on "Add Another Item" + expect(page).to have_css(".distribution-line-item-inputs", count: 2) + within all(".distribution-line-item-inputs").last do + expect(page).to have_field("Item") + fill_in "Quantity - Total Units", with: "5" + expect(page).to have_field("Quantity - Total Units", with: "5") + find(".select2-selection").click + end + find(".select2-results__option", text: "Layout Diapers").click + within all(".line_item_section").last do + expect(page).to have_content("Layout Diapers") + click_on "Remove" + end + expect(page).to have_css(".distribution-line-item-inputs", count: 1) + end + end + + context "on a regular distribution" do + before { visit new_distribution_path } + + include_examples "responsive line items" + end + + context "when fulfilling a request" do + before do + request = create(:request, organization:, partner:, request_items: [{"item_id" => item.id, "quantity" => 10}]) + create(:item_request, request:, item:, quantity: 10) + visit request_path(request) + click_on "Fulfill request" + end + + include_examples "responsive line items" + end +end