Skip to content

Commit

Permalink
Convert symbol columns to links in admin tables
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MadelineCollier committed Nov 27, 2024
1 parent 6bd7f5e commit 7e8a645
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 20 deletions.
10 changes: 3 additions & 7 deletions admin/app/components/solidus_admin/ui/table/component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,11 @@
>
<% @data.rows.each do |row| %>
<tr
class="border-b border-gray-100 last:border-0 hover:bg-gray-50 cursor-pointer <%= 'bg-gray-15 text-gray-700' if @data.fade&.call(row) %>"
<% if @data.url %>
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 %>
</tr>
<% end %>
Expand Down
12 changes: 1 addition & 11 deletions admin/app/components/solidus_admin/ui/table/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,11 @@ export default class extends Controller {
}

rowClicked(event) {
// If the user clicked on a link, button, input or summary, skip the row url visit
// Skip if the user clicked on a link, button, input or summary
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)
}
}

Expand Down
4 changes: 2 additions & 2 deletions admin/app/components/solidus_admin/ui/table/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 7e8a645

Please sign in to comment.