Rewrite stream rendering to avoid client-side expanding

* adding streams in client breaks things (e.g. autofocus)
* some tasks need to be performed in one stream action to avoid
  flickering (e.g. table row substitution)
This commit is contained in:
2024-02-11 18:31:06 +01:00
parent 759a0b31b7
commit 1198add901
7 changed files with 107 additions and 38 deletions

View File

@@ -1,5 +1,7 @@
<%= fields_for @unit do |form| %>
<tr id="<%= dom_id(@unit) %>" onkeydown="processKey(event)">
<%= tag.tr id: dom_id(@unit), class: "form", onkeydown: "processKey(event)",
data: {link_id: link_id} do %>
<td class="<%= class_names({subunit: @unit.base}) %>">
<%= form.text_field :symbol, form: :unit_form, required: true, autofocus: true, size: 12,
maxlength: @unit.class.columns_hash['symbol'].limit, autocomplete: "off" %>
@@ -19,8 +21,8 @@
<td class="actions">
<%= form.submit form: :unit_form %>
<%= image_link_to t(:cancel), "close-circle-outline", units_path, class: 'dangerous',
name: :cancel, onclick: render_turbo_stream('form_close', {id: id}) %>
name: :cancel, onclick: render_turbo_stream('form_close', {link_id: link_id}) %>
</td>
</tr>
<% end %>
<!-- TODO: display error_messages_for unit -->
<% end %>

View File

@@ -1,3 +1,2 @@
<%= @unit.persisted? ? turbo_stream.replace(@unit) : turbo_stream.remove(@unit) %>
<%= turbo_stream.enable id %>
<%= turbo_stream.focus id %>
<%= turbo_stream.close_form @unit %>
<%#= turbo_stream.focus link_id %>

View File

@@ -1,7 +1,7 @@
<%# TODO: make sure turbo_stream layout is used in new/edit %>
<%= turbo_stream.replace @unit, partial: 'form', locals: {id: dom_id(@unit, :edit)} %>
<%= turbo_stream.update :unit_form_frame do %>
<%= form_with model: @unit, html: {id: :unit_form} do %>
<% end %>
<% end unless @unit.errors.present? %>
<% end %>
<%= turbo_stream.replace_form @unit, partial: 'form', locals: {link_id: dom_id(@unit, :edit)} %>

View File

@@ -1,20 +1,9 @@
<% options = {partial: 'form', locals: {id: dom_id(@unit.base || @unit, :add)}} %>
<% link_id = dom_id(@unit.base || @unit, :add) %>
<%= turbo_stream.disable link_id -%>
<% if @unit.errors.present? %>
<%= turbo_stream.replace @unit, **options -%>
<% else %>
<%= turbo_stream.disable options[:locals][:id] -%>
<%= turbo_stream.click_all 'tbody a[name=cancel]' -%>
<%#= turbo_stream.blur_all %>
<% if @unit.base.nil? %>
<%= turbo_stream.prepend :units, **options -%>
<% else %>
<%= turbo_stream.after @unit.base, **options -%>
<% end %>
<%= turbo_stream.update :unit_form_frame do %>
<%= form_with model: @unit, html: {id: :unit_form} do %>
<% end %>
<%= turbo_stream.update :unit_form_frame do %>
<%= form_with model: @unit, html: {id: :unit_form} do %>
<% end %>
<% end %>
<%= turbo_stream.insert_form (@unit.base || :units), partial: 'form', locals: {link_id: link_id} %>