|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +class Views::Docs::DataTable < Views::Base |
| 4 | + Row = Struct.new(:id, :name, :email, :salary, :status, keyword_init: true) |
| 5 | + |
| 6 | + SAMPLE_ROWS = [ |
| 7 | + Row.new(id: 1, name: "Alice", email: "alice@example.com", salary: 90_000, status: "Active"), |
| 8 | + Row.new(id: 2, name: "Bob", email: "bob@example.com", salary: 75_000, status: "Inactive"), |
| 9 | + Row.new(id: 3, name: "Carol", email: "carol@example.com", salary: 85_000, status: "Active") |
| 10 | + ].freeze |
| 11 | + |
| 12 | + def view_template |
| 13 | + div(class: "max-w-2xl mx-auto w-full py-10 space-y-10") do |
| 14 | + component = "DataTable" |
| 15 | + render Docs::Header.new( |
| 16 | + title: component, |
| 17 | + description: "A Hotwire-first data table. Every interaction (sort, search, pagination) is a Rails request answered with HTML, swapped via Turbo Frame. Row selection uses form-first submission." |
| 18 | + ) |
| 19 | + |
| 20 | + Heading(level: 2) { "Usage" } |
| 21 | + |
| 22 | + render Docs::VisualCodeExample.new(title: "Server-driven table", context: self) do |
| 23 | + @@code = <<~RUBY |
| 24 | + DataTable(id: "employees") do |
| 25 | + DataTableToolbar do |
| 26 | + DataTableSearch(path: employees_path, value: @search) |
| 27 | + DataTablePerPageSelect(path: employees_path, value: @per_page) |
| 28 | + end |
| 29 | +
|
| 30 | + div(class: "rounded-md border") do |
| 31 | + Table do |
| 32 | + TableHeader do |
| 33 | + TableRow do |
| 34 | + TableHead { "Name" } |
| 35 | + DataTableSortHead(column_key: :email, label: "Email", |
| 36 | + sort: @sort, direction: @direction, |
| 37 | + path: employees_path) |
| 38 | + TableHead(class: "text-right") { "Salary" } |
| 39 | + end |
| 40 | + end |
| 41 | + TableBody do |
| 42 | + @rows.each do |r| |
| 43 | + TableRow do |
| 44 | + TableCell { r.name } |
| 45 | + TableCell { r.email } |
| 46 | + TableCell(class: "text-right") { r.salary } |
| 47 | + end |
| 48 | + end |
| 49 | + end |
| 50 | + end |
| 51 | + end |
| 52 | +
|
| 53 | + DataTablePaginationBar do |
| 54 | + DataTableSelectionSummary(total_on_page: @rows.size) |
| 55 | + DataTablePagination(page: @page, per_page: @per_page, |
| 56 | + total_count: @total_count, path: employees_path) |
| 57 | + end |
| 58 | + end |
| 59 | + RUBY |
| 60 | + end |
| 61 | + |
| 62 | + render Docs::VisualCodeExample.new(title: "Selection + bulk actions", context: self) do |
| 63 | + @@code = <<~RUBY |
| 64 | + FORM_ID = "employees_form" |
| 65 | +
|
| 66 | + DataTable(id: "employees_select") do |
| 67 | + DataTableToolbar do |
| 68 | + DataTableSearch(path: employees_path, value: @search) |
| 69 | + DataTableBulkActions do |
| 70 | + Button(type: "submit", form: FORM_ID, |
| 71 | + formaction: bulk_delete_employees_path, |
| 72 | + formmethod: "post", |
| 73 | + variant: :destructive, size: :sm) { "Delete" } |
| 74 | + end |
| 75 | + end |
| 76 | +
|
| 77 | + DataTableForm(id: FORM_ID, action: "") do |
| 78 | + div(class: "rounded-md border") do |
| 79 | + Table do |
| 80 | + TableHeader do |
| 81 | + TableRow do |
| 82 | + TableHead(class: "w-10") { DataTableSelectAllCheckbox() } |
| 83 | + TableHead { "Name" } |
| 84 | + TableHead { "Email" } |
| 85 | + end |
| 86 | + end |
| 87 | + TableBody do |
| 88 | + @rows.each do |r| |
| 89 | + TableRow do |
| 90 | + TableCell { DataTableRowCheckbox(value: r.id) } |
| 91 | + TableCell { r.name } |
| 92 | + TableCell { r.email } |
| 93 | + end |
| 94 | + end |
| 95 | + end |
| 96 | + end |
| 97 | + end |
| 98 | + end |
| 99 | +
|
| 100 | + DataTablePaginationBar do |
| 101 | + DataTableSelectionSummary(total_on_page: @rows.size) |
| 102 | + DataTablePagination(page: @page, per_page: @per_page, |
| 103 | + total_count: @total_count, path: employees_path) |
| 104 | + end |
| 105 | + end |
| 106 | + RUBY |
| 107 | + end |
| 108 | + |
| 109 | + render Docs::VisualCodeExample.new(title: "Column visibility", context: self) do |
| 110 | + @@code = <<~RUBY |
| 111 | + DataTable(id: "employees_cols") do |
| 112 | + DataTableToolbar do |
| 113 | + DataTableColumnToggle(columns: [ |
| 114 | + {key: :email, label: "Email"}, |
| 115 | + {key: :salary, label: "Salary"} |
| 116 | + ]) |
| 117 | + end |
| 118 | +
|
| 119 | + Table do |
| 120 | + TableHeader do |
| 121 | + TableRow do |
| 122 | + TableHead { "Name" } |
| 123 | + TableHead(data: {column: "email"}) { "Email" } |
| 124 | + TableHead(data: {column: "salary"}) { "Salary" } |
| 125 | + end |
| 126 | + end |
| 127 | + TableBody do |
| 128 | + @rows.each do |r| |
| 129 | + TableRow do |
| 130 | + TableCell { r.name } |
| 131 | + TableCell(data: {column: "email"}) { r.email } |
| 132 | + TableCell(data: {column: "salary"}) { r.salary } |
| 133 | + end |
| 134 | + end |
| 135 | + end |
| 136 | + end |
| 137 | + end |
| 138 | + RUBY |
| 139 | + end |
| 140 | + |
| 141 | + render Docs::VisualCodeExample.new(title: "Expandable rows", context: self) do |
| 142 | + @@code = <<~RUBY |
| 143 | + DataTable(id: "employees_expand") do |
| 144 | + Table do |
| 145 | + TableHeader do |
| 146 | + TableRow do |
| 147 | + TableHead(class: "w-10") { } |
| 148 | + TableHead { "Name" } |
| 149 | + TableHead { "Email" } |
| 150 | + end |
| 151 | + end |
| 152 | + TableBody do |
| 153 | + @rows.each do |r| |
| 154 | + detail_id = "row-\#{r.id}-detail" |
| 155 | + TableRow do |
| 156 | + TableCell { DataTableExpandToggle(controls: detail_id, label: "Toggle \#{r.name}") } |
| 157 | + TableCell { r.name } |
| 158 | + TableCell { r.email } |
| 159 | + end |
| 160 | + TableRow(id: detail_id, class: "hidden", role: "region") do |
| 161 | + TableCell(colspan: 3, class: "bg-muted/40") do |
| 162 | + div(class: "p-4") do |
| 163 | + p { "Salary: $\#{r.salary}" } |
| 164 | + p { "Status: \#{r.status}" } |
| 165 | + end |
| 166 | + end |
| 167 | + end |
| 168 | + end |
| 169 | + end |
| 170 | + end |
| 171 | + end |
| 172 | + RUBY |
| 173 | + end |
| 174 | + |
| 175 | + render Components::ComponentSetup::Tabs.new(component_name: component) |
| 176 | + |
| 177 | + render Docs::ComponentsTable.new(component_files(component)) |
| 178 | + end |
| 179 | + end |
| 180 | +end |
0 commit comments