From 7e96eae1ad96e1f8202563ad60e207673bda83a8 Mon Sep 17 00:00:00 2001 From: Madeline Collier Date: Mon, 25 Nov 2024 14:35:08 +0100 Subject: [PATCH] Convert symbol columns to links in admin tables Previously, when provided a symbol, the admin table component would simply call `data.public_send(cell)` (with cell being the symbol). This would result in a basic string as the content of the table row. We want to stop relying on the additional Stimulus click handlers that captured those plain text clicks and redirected the users to wherever the `row_url` location was. Now, the `render_data_cell` method handles wrapping the plain text in a link, with the href being the `row_url` location. Symbols can still be provided as columns and the component takes care of generating the proper semantic UI elements for us. --- .../solidus_admin/ui/table/component.html.erb | 10 +++------- .../solidus_admin/ui/table/component.js | 19 ------------------- .../solidus_admin/ui/table/component.rb | 4 ++-- 3 files changed, 5 insertions(+), 28 deletions(-) diff --git a/admin/app/components/solidus_admin/ui/table/component.html.erb b/admin/app/components/solidus_admin/ui/table/component.html.erb index 69a155cd66d..9e685cbcbf2 100644 --- a/admin/app/components/solidus_admin/ui/table/component.html.erb +++ b/admin/app/components/solidus_admin/ui/table/component.html.erb @@ -138,15 +138,11 @@ > <% @data.rows.each do |row| %> - data-action="click-><%= stimulus_id %>#rowClicked" - data-<%= stimulus_id %>-url-param="<%= @data.url.call(row) %>" - <%= "data-sortable-url=#{@sortable.url.call(row)}" if @sortable&.url %> - <% end %> + class="border-b border-gray-100 last:border-0 hover:bg-gray-50 <%= 'bg-gray-15 text-gray-700' if @data.fade&.call(row) %>" + <%= "data-sortable-url=#{@sortable.url.call(row)}" if @sortable&.url %> > <% @data.columns.each do |column| %> - <%= render_data_cell(column, row) %> + <%= render_data_cell(column, row, @data.url&.call(row)) %> <% end %> <% end %> diff --git a/admin/app/components/solidus_admin/ui/table/component.js b/admin/app/components/solidus_admin/ui/table/component.js index 66837d4ce75..f0277eef3ec 100644 --- a/admin/app/components/solidus_admin/ui/table/component.js +++ b/admin/app/components/solidus_admin/ui/table/component.js @@ -97,25 +97,6 @@ export default class extends Controller { this.render() } - rowClicked(event) { - // If the user clicked on a link, button, input or summary, skip the row url visit - if (event.target.closest("td").contains(event.target.closest("a,select,textarea,button,input,summary"))) return - - if (this.modeValue === "batch") { - this.toggleCheckbox(event.currentTarget) - } else { - const url = new URL(event.params.url, "http://dummy.com") - const params = new URLSearchParams(url.search) - const frameId = params.get('_turbo_frame') - const frame = frameId ? { frame: frameId } : {} - // remove the custom _turbo_frame param from url search: - params.delete('_turbo_frame') - url.search = params.toString() - - window.Turbo.visit(url.pathname + url.search, frame) - } - } - toggleCheckbox(row) { const checkbox = this.checkboxTargets.find(selection => row.contains(selection)) diff --git a/admin/app/components/solidus_admin/ui/table/component.rb b/admin/app/components/solidus_admin/ui/table/component.rb index 79e08f9d494..b41c505c0c5 100644 --- a/admin/app/components/solidus_admin/ui/table/component.rb +++ b/admin/app/components/solidus_admin/ui/table/component.rb @@ -158,10 +158,10 @@ def render_header_cell(cell, **attrs) }, **attrs) end - def render_data_cell(column, data) + def render_data_cell(column, data, url = nil) cell = column.data cell = cell.call(data) if cell.respond_to?(:call) - cell = data.public_send(cell) if cell.is_a?(Symbol) + cell = link_to data.public_send(cell), url, class: "underline cursor-pointer" if cell.is_a?(Symbol) cell = cell.render_in(self) if cell.respond_to?(:render_in) cell = tag.div(cell, class: "flex items-center gap-1.5 justify-start overflow-x-hidden") if column.wrap