Prevent table columns' width change on form toggle

This commit is contained in:
2026-11-05 18:00:34 +01:00
parent 4940e6de86
commit 3655a74b94
23 changed files with 150 additions and 90 deletions
+1
View File
@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" id="icon" viewBox="0 0 24 24"><path d="M19.78,2.2L24,6.42L8.44,22L0,13.55L4.22,9.33L8.44,13.55L19.78,2.2M19.78,5L8.44,16.36L4.22,12.19L2.81,13.55L8.44,19.17L21.19,6.42L19.78,5Z" /></svg>

After

Width:  |  Height:  |  Size: 227 B

@@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" id="icon" viewBox="0 0 24 24"><path d="M12,20C7.59,20 4,16.41 4,12C4,7.59 7.59,4 12,4C16.41,4 20,7.59 20,12C20,16.41 16.41,20 12,20M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2M13,7H11V11H7V13H11V17H13V13H17V11H13V7Z" /></svg>

Before

Width:  |  Height:  |  Size: 297 B

+30 -12
View File
@@ -72,10 +72,20 @@
fill: var(--color-border-gray) !important;
pointer-events: none !important;
}
/* Styles set `display` without distinguishing between [hidden] elements, making
* them visible. */
/* For hidden elements that need to influence layout and/or are only temporarily
* hidden use [hidden=until-found] instead of [hidden] to:
* - avoid hiding with `display: none` as `display` may be overridden by
* selectors with greater specificity and element may become visible,
* - use `visibility: collapse` for hiding, to avoid excessive resizing.
* TODO: change to `until-found` after upgrade to Rails > 8.1.4 (#59338)
* - change hidden values in index <tr>s
* - check if toggleAttribute("hidden") works properly (use turbo action #hide?)
[hidden=until-found] {
visibility: collapse;
}*/
[hidden] {
display: none !important;
display: revert;
visibility: collapse;
}
@@ -229,6 +239,7 @@ pre {
display: flex;
fill: var(--color-gray);
font-weight: bold;
justify-content: center;
}
.button {
border: 1px solid var(--color-gray);
@@ -252,6 +263,7 @@ table .button {
font-weight: normal;
height: 100%;
padding: 0.4em;
width: 100%;
}
.button:focus-visible,
.tab:focus-visible,
@@ -460,25 +472,26 @@ header {
.tabular-form table td:last-child {
padding-inline-end: 0;
}
.tabular-form table :is(form, input, select, textarea):only-child {
margin-inline-start: 0;
.tabular-form table :is(input, select, textarea) {
margin-inline: 0;
}
/* FIXME: <button> has different height (28px) inside <td> than <a> (29px) */
.items-table {
border: none;
border-radius: 0.25em;
border-spacing: 0;
font-size: 0.85rem;
text-align: left;
}
.items-table:has(tbody > tr > td) {
.items-table:has(> tbody > tr > td) {
border: 1px solid var(--color-border-gray);
border-radius: 0.25em;
}
.items-table:has(tbody > tr > td) caption {
.items-table:has(> tbody > tr > td) caption {
display: none;
}
.items-table tbody:not(:has(tr > td)){
.items-table tbody:not(:has(> tr > td)) {
visibility: collapse;
}
.items-table th,
@@ -496,13 +509,18 @@ header {
.items-table td {
border-top: 1px solid var(--color-border-gray);
height: 2.4em;
padding: 0.1em 0 0.1em calc(1em + var(--depth) * 0.8em);
padding: 0.1em 0.5em;
}
.items-table td[style*="--depth"] {
padding-inline-start: calc(1em + var(--depth) * 0.8em);
}
.items-table td:has(> .button, > :where(form) > .button),
.items-table td:last-child {
padding-inline-end: 0.1em;
}
.items-table :is(form, input, select, textarea):only-child {
margin-inline-start: calc(-0.4em - 0.9px);
/* Vertically align text inside form controls with ordinary data cells. */
.items-table :is(input, select, textarea) {
margin-inline: calc(-0.4em - 0.9px);
}
/* For <a> to fill table cell completely, we use an `::after` pseudoelement. */
/* TODO: expand to whole row? will require adjusting z-index on inputs/buttons */
+3 -2
View File
@@ -127,8 +127,7 @@ module ApplicationHelper
private
def submit_default_value
svg_name = object ? (object.persisted? ? 'update' : 'plus-circle-outline') : ''
@template.svg_tag(svg_name, super)
@template.svg_tag('check-outline', I18n.t(:"save"))
end
end
@@ -151,6 +150,8 @@ module ApplicationHelper
def svg_tag(source, label = nil, options = {})
label, options = nil, label if label.is_a? Hash
svg_tag = tag.svg(**options) do
# TODO: move pictograms to 1 file with unique IDs instead of `icon`
# TODO: add test/rake task to detect unused and missing pictograms
tag.use(href: image_path("#{source}.svg") + '#icon')
end
label.blank? ? svg_tag : svg_tag + tag.span(label)
+1 -1
View File
@@ -82,7 +82,7 @@ window.numberValidate = numberValidate
function formProcessKey(event) {
switch (event.key) {
case "Escape":
event.currentTarget.querySelector("a[name=cancel]").click()
event.currentTarget.querySelector("a[name=discard]").click()
break
// TODO: allow new line inside <textarea> (submit with ctrl-enter?)
case "Enter":
+2
View File
@@ -23,6 +23,8 @@ class Quantity < ApplicationRecord
# characters); this doesn't seem to apply to :string (implemented as varchar)
validates :description, length: {maximum: type_for_attribute(:description).limit}
# TODO: initialize `depth` to `parent.depth + 1`
# Update :depths of progenies after parent change
before_save if: :parent_changed? do
self[:depth] = parent&.depth&.succ || 0
+1 -1
View File
@@ -33,7 +33,7 @@
<div class="flex reverse">
<%= form.button id: :create_measurement_button, disabled: true -%>
<%= image_link_to t(:cancel), "close-outline", measurements_path, name: :cancel,
<%= image_link_to t(:discard), "close-outline", measurements_path, name: :discard,
class: 'auxiliary dangerous', onclick: render_turbo_stream('form_close') %>
</div>
<% end %>
+9 -9
View File
@@ -1,16 +1,16 @@
<%= tabular_fields_for @quantity, form: form_tag do |form| %>
<%- tag.tr id: row, class: "form", onkeydown: "formProcessKey(event)",
data: {link: link, form: form_tag, hidden_row: hidden_row} do %>
<%# locals: (record: Quantity.new, **attrs) %>
<%= tabular_fields_for record, form: attrs.dig(:data, :form) do |form| %>
<%- tag.tr class: "form", onkeydown: "formProcessKey(event)", **attrs do %>
<td style="--depth:<%= @quantity.depth %>">
<td style="--depth:<%= form.object.depth %>">
<%= form.text_field :name, autofocus: true, class: 'hstretch', required: true %>
<td><%= form.text_area :description, class: 'hstretch', escape: false, rows: 1 %>
<td class="flex">
<%= form.button %>
<%= image_link_to t(:cancel), "close-outline", quantities_path,
class: 'dangerous', name: :cancel,
onclick: render_turbo_stream('form_close', {row: row}) %>
<td><%= form.button %>
<td>
<%= image_link_to t(:discard), "close-outline", quantities_path,
class: 'dangerous', name: :discard,
onclick: render_turbo_stream('form_close', {id: attrs[:id]}) %>
<td><!-- TODO: add aria-disabled drag handle with proper vertical alignment? -->
<% end %>
<% end %>
+1 -1
View File
@@ -1,2 +1,2 @@
<%= turbo_stream.close_form row %>
<%= turbo_stream.close_form id %>
<%= turbo_stream.update :flashes %>
+2 -2
View File
@@ -12,11 +12,11 @@
<td><%= quantity.description %>
<% if current_user.at_least(:active) %>
<td class="flex">
<td>
<%= image_link_to t('.new_subquantity'), 'plus-outline',
new_quantity_path(quantity), id: dom_target(:new, quantity, :link),
onclick: 'this.blur();', data: {turbo_stream: true} %>
<td>
<%= image_button_to_if quantity.destroyable?, t('.destroy'), 'delete-outline',
quantity_path(quantity), method: :delete %>
+8 -7
View File
@@ -1,12 +1,13 @@
<% ids = {row: dom_target(:edit, @quantity),
hidden_row: dom_target(@quantity),
link: nil,
form_tag: dom_target(:edit, @quantity, :form)} %>
<% attrs = {record: @quantity,
id: dom_target(:edit, @quantity),
data: {form: dom_target(:edit, @quantity, :form),
hidden_row: dom_target(@quantity),
link: nil}} %>
<%= turbo_stream.append_all 'body' do %>
<%- tabular_form_with model: @quantity, html: {id: ids[:form_tag]} do %>
<%- tabular_form_with model: @quantity, html: {id: attrs[:data][:form]} do %>
<% end %>
<% end %>
<%= turbo_stream.hide ids[:hidden_row] %>
<%= turbo_stream.after @quantity, partial: 'form', locals: ids -%>
<%= turbo_stream.hide attrs[:data][:hidden_row] %>
<%= turbo_stream.after @quantity, partial: 'form', locals: attrs -%>
+8 -3
View File
@@ -15,17 +15,22 @@
<th class="hmintext"><%= Quantity.human_attribute_name(:name) %>
<th class="hexpand"><%= Quantity.human_attribute_name(:description) %>
<% if current_user.at_least(:active) %>
<th><%= t :actions %>
<th>
<th colspan="3"><%= t :actions %>
<tr id="quantity_" hidden
ondragover="dragOver(event)" ondrop="drop(event)"
ondragenter="dragEnter(event)" ondragleave="dragLeave(event)"
data-drop-id="quantity_"
data-drop-id-param="<%= field_name(:quantity, :parent_id) %>">
<th colspan="4"><%= t '.top_level_drop' %>
<th colspan="5"><%= t '.top_level_drop' %>
<% end %>
<%= render @quantities %>
<% if current_user.at_least(:active) %>
<tr id="endmark" hidden>
<tfoot>
<%= render partial: 'form', locals: {hidden: true} %>
<% end %>
</table>
+8 -7
View File
@@ -1,15 +1,16 @@
<% target_id = dom_target(:new, @quantity.parent || :quantity) %>
<% ids = {row: target_id,
hidden_row: nil,
link: dom_target(target_id, :link),
form_tag: dom_target(target_id, :form)} %>
<% attrs = {record: @quantity,
id: target_id,
data: {form: dom_target(target_id, :form),
hidden_row: nil,
link: dom_target(target_id, :link)}} %>
<%= turbo_stream.disable ids[:link] -%>
<%= turbo_stream.disable attrs[:data][:link] -%>
<%= turbo_stream.append_all 'body' do %>
<%- tabular_form_with model: @quantity, html: {id: ids[:form_tag]} do |form| %>
<%- tabular_form_with model: @quantity, html: {id: attrs[:data][:form]} do |form| %>
<%= form.hidden_field :parent_id if @quantity.parent_id? %>
<% end %>
<% end %>
<%= turbo_stream.after @quantity.parent || :quantity_, partial: 'form', locals: ids %>
<%= turbo_stream.after @quantity.parent || :quantity_, partial: 'form', locals: attrs %>
+9 -9
View File
@@ -1,19 +1,19 @@
<%= tabular_fields_for @unit, form: form_tag do |form| %>
<%- tag.tr id: row, class: "form", onkeydown: "formProcessKey(event)",
data: {link: link, form: form_tag, hidden_row: hidden_row} do %>
<%# locals: (record: Unit.new, **attrs) %>
<%= tabular_fields_for record, form: attrs.dig(:data, :form) do |form| %>
<%- tag.tr class: "form", onkeydown: "formProcessKey(event)", **attrs do %>
<td style="--depth:<%= @unit.base_id? ? 1 : 0 %>">
<td style="--depth:<%= form.object.base_id? ? 1 : 0 %>">
<%= form.text_field :symbol, autofocus: true, class: 'hstretch', required: true %>
<td>
<% if @unit.base_id? %>
<% if form.object.base_id? %>
<%= form.number_field :multiplier, class: 'hstretch', required: true %>
<% end %>
<td><%= form.text_area :description, class: 'hstretch', escape: false, rows: 1 %>
<td class="flex">
<%= form.button %>
<%= image_link_to t(:cancel), "close-outline", units_path, class: 'dangerous',
name: :cancel, onclick: render_turbo_stream('form_close', {row: row}) %>
<td><%= form.button %>
<td>
<%= image_link_to t(:discard), "close-outline", units_path, class: 'dangerous',
name: :discard, onclick: render_turbo_stream('form_close', {id: attrs[:id]}) %>
<td><!-- TODO: add aria-disabled drag handle with proper vertical alignment? -->
<% end %>
<% end %>
+1 -1
View File
@@ -1,2 +1,2 @@
<%= turbo_stream.close_form row %>
<%= turbo_stream.close_form id %>
<%= turbo_stream.update :flashes %>
+2 -2
View File
@@ -14,13 +14,13 @@
<td><%= unit.description %>
<% if current_user.at_least(:active) %>
<td class="flex">
<td>
<% unless unit.base_id? %>
<%= image_link_to t('.new_subunit'), 'plus-outline', new_unit_path(unit),
id: dom_target(:new, unit, :link), onclick: 'this.blur();',
data: {turbo_stream: true} %>
<% end %>
<td>
<%= image_button_to_if unit.movable?, t('.destroy'), 'delete-outline',
unit_path(unit), method: :delete %>
+8 -7
View File
@@ -1,12 +1,13 @@
<% ids = {row: dom_target(:edit, @unit),
hidden_row: dom_target(@unit),
link: nil,
form_tag: dom_target(:edit, @unit, :form)} %>
<% attrs = {record: @unit,
id: dom_target(:edit, @unit),
data: {form: dom_target(:edit, @unit, :form),
hidden_row: dom_target(@unit),
link: nil}} %>
<%= turbo_stream.append_all 'body' do %>
<%- tabular_form_with model: @unit, html: {id: ids[:form_tag]} do %>
<%- tabular_form_with model: @unit, html: {id: attrs[:data][:form]} do %>
<% end %>
<% end %>
<%= turbo_stream.hide ids[:hidden_row] %>
<%= turbo_stream.after @unit, partial: 'form', locals: ids -%>
<%= turbo_stream.hide attrs[:data][:hidden_row] %>
<%= turbo_stream.after @unit, partial: 'form', locals: attrs -%>
+12 -4
View File
@@ -16,18 +16,26 @@
<th class="hminnumber"><%= Unit.human_attribute_name(:multiplier) %>
<th class="hexpand"><%= Unit.human_attribute_name(:description) %>
<% if current_user.at_least(:active) %>
<th><%= t :actions %>
<th>
<th colspan="3"><%= t :actions %>
<tr id="unit_" hidden
ondragover="dragOver(event)" ondrop="drop(event)"
ondragenter="dragEnter(event)" ondragleave="dragLeave(event)"
data-drop-id="unit_" data-drop-id-param="<%= field_name(:unit, :base_id) %>">
<th colspan="5"><%= t '.top_level_drop' %>
<th colspan="6"><%= t '.top_level_drop' %>
<% end %>
<%= render @units %>
<%# Hidden rows with IDs allow simplier row insertion by using only
<% if current_user.at_least(:active) %>
<%# Hidden header and `#endmark` rows allows simplier row insertion by using only
turbo_stream #after/#before instead of #after + #prepend/#before + #append. %>
<%# TODO: replace with custom turbo_stream action, e.g. #afterrow that #prepend-s
if ID has `rows` property (`rows` in object) and #after otherwise? %>
<tr id="endmark" hidden>
<tfoot>
<%# Insert form to prevent actions column width change on form toggle. %>
<%= render partial: 'form', locals: {hidden: true} %>
<% end %>
</table>
+9 -7
View File
@@ -1,15 +1,17 @@
<% target_id = dom_target(:new, @unit.base || :unit) %>
<% ids = {row: target_id,
hidden_row: nil,
link: dom_target(target_id, :link),
form_tag: dom_target(target_id, :form)} %>
<% attrs = {record: @unit,
id: target_id,
data: {form: dom_target(target_id, :form),
hidden_row: nil,
link: dom_target(target_id, :link)}} %>
<%= turbo_stream.disable ids[:link] -%>
<%= turbo_stream.disable attrs[:data][:link] -%>
<%# TODO: disable base unit delete button if subunit; same for quantities %>
<%= turbo_stream.append_all 'body' do %>
<%- tabular_form_with model: @unit, html: {id: ids[:form_tag]} do |form| %>
<%- tabular_form_with model: @unit, html: {id: attrs[:data][:form]} do |form| %>
<%= form.hidden_field :base_id if @unit.base_id? %>
<% end %>
<% end %>
<%= turbo_stream.after @unit.base || :unit_, partial: 'form', locals: ids %>
<%= turbo_stream.after @unit.base || :unit_, partial: 'form', locals: attrs %>
+1
View File
@@ -3,3 +3,4 @@
<% end %>
<%= turbo_stream.remove @unit %>
<%= turbo_stream.before @before || :endmark, @unit %>
<%# TODO: move new subunit form as well if open %>
+2 -3
View File
@@ -88,8 +88,6 @@ en:
unconfirmed_email_html: >
Awaiting confirmation for:<br><em>(since %{confirmation_sent_at})</em>
submit:
create: Create
update: Update
user:
update: Update profile
layouts:
@@ -181,10 +179,11 @@ en:
add: Add
apply: Apply
back: Back
cancel: Cancel
delete: Delete
discard: Discard
:no: 'no'
register: Register
save: Save
sign_in: Sign in
recover_password: Recover password
resend_confirmation: Resend confirmation
+14 -7
View File
@@ -76,7 +76,7 @@ class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
# Override to count only data rows, to make it consistent with #match_row.
def match_row_count(size)
XPath.descendant(:tr).where(XPath.descendant(:td)).count.equals(size)
XPath.descendant(:tbody).descendant(:tr)[XPath.descendant(:td)].count.equals(size)
end
end
@@ -92,15 +92,22 @@ class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
# Based on Capybara :table and :table_row selectors.
Capybara.add_selector(:table_cell, locator_type: [String, Symbol]) do
xpath do |locator|
# TODO: make sure to only include visible column headers
column = XPath.string.n.is(locator.to_s)
header_xp = XPath.ancestor(:table)[1].descendant(:tr)[1].descendant(:th)[column]
position_xp = XPath.position.equals(header_xp.preceding_sibling.count.plus(1))
XPath.descendant(:td).where(header_xp.boolean & position_xp)
xpath = XPath.descendant(:td)
case locator
when String, Symbol
# TODO: make sure to only include visible column headers
column = XPath.string.n.is(locator.to_s)
header_xp = XPath.ancestor(:table)[1].descendant(:tr)[1].descendant(:th)[column]
position_xp = XPath.position.equals(header_xp.preceding_sibling.count.plus(1))
xpath.where(header_xp.boolean & position_xp)
else
xpath
end
end
expression_filter(:fillable) do |xpath, value|
value ? xpath.where(XPath.descendant(:input, :select, :textarea)) : xpath
value ? xpath[XPath.descendant(:input, :select, :textarea)
.where(!XPath.attr(:type).one_of('submit', 'hidden'))] : xpath
end
node_filter(:value) do |node, expected|
+18 -4
View File
@@ -79,7 +79,7 @@ class UnitsTest < ApplicationSystemTestCase
[name, value]
end
click_on t('helpers.submit.create')
click_on t(:save)
end
attributes = attributes.to_h
@@ -152,7 +152,7 @@ class UnitsTest < ApplicationSystemTestCase
click_on t('units.index.new_unit')
end
find(:table_cell, column(:symbol), fillable: true).fill_in with: unit.symbol
click_on t('helpers.submit.create')
click_on t(:save)
within :table_row, index+1 do
assert_selector :table_cell, column(:symbol), exact_text: symbol
@@ -190,7 +190,7 @@ class UnitsTest < ApplicationSystemTestCase
else
assert_matches_selector link, :link, disabled: true
end
click_on t(:cancel)
click_on t(:discard)
assert_no_selector '.flash.alert'
# TODO: change to assert_selector :table, rows: ...
@@ -258,13 +258,27 @@ class UnitsTest < ApplicationSystemTestCase
sign_in
click_on t('units.index.new_unit')
within 'tbody' do
find(:link_or_button, exact_text: t(:cancel)).click
find(:link_or_button, exact_text: t(:discard)).click
assert_no_selector :fillable_field
end
click_on t('units.index.new_unit')
assert_selector 'tbody > tr:has(input, textarea)'
end
test "new and edit forms open without changing table columns width" do
sign_in
width = if has_selector?(:table_row)
-> { all(:table_row).sample.all(:table_cell).map { |c| c.rect.width } }
else
link_labels.slice!(:new_unit)
-> { find(:table).rect.width }
end
assert_no_changes width do
all(:link, exact_text: link_labels.values.sample).sample.click
assert_selector :table_cell, fillable: true
end
end
test "rebase" do
# TODO
assert true