File tree 4 files changed +62
-3
lines changed
spec/unit/views/components 4 files changed +62
-3
lines changed Original file line number Diff line number Diff line change @@ -196,6 +196,16 @@ index do
196
196
end
197
197
```
198
198
199
+ ## Custom tbody data attributes
200
+
201
+ In order to add special data attributes to table tbody pass a ` :tbody_data ` option of the ` index ` method.
202
+
203
+ ``` ruby
204
+ index tbody_data: { controller: ' stimulus-controller' } do
205
+ # columns
206
+ end
207
+ ```
208
+
199
209
## Custom row class
200
210
201
211
In order to add special class to table rows pass the proc object as a ` :row_class ` option
@@ -206,3 +216,14 @@ index row_class: ->elem { 'active' if elem.active? } do
206
216
# columns
207
217
end
208
218
```
219
+
220
+ ## Custom row data attributes
221
+
222
+ In order to add special data attributes to table rows pass the proc object as a ` :row_data ` option of the ` index ` method.
223
+
224
+ ``` ruby
225
+ index row_data: -> elem {
8000
' element-id' => elem.id } do
226
+ # columns
227
+ end
228
+ ```
229
+
Original file line number Diff line number Diff line change @@ -16,7 +16,9 @@ def build(obj, *attrs)
16
16
@resource_class ||= @collection . klass if @collection . respond_to? :klass
17
17
18
18
@columns = [ ]
19
+ @tbody_data = options . delete ( :tbody_data )
19
20
@row_class = options . delete ( :row_class )
21
+ @row_data = options . delete ( :row_data )
20
22
21
23
build_table
22
24
super ( options )
@@ -91,10 +93,10 @@ def build_table_header(col)
91
93
end
92
94
93
95
def build_table_body
94
- @tbody = tbody do
96
+ @tbody = tbody data : @tbody_data do
95
97
# Build enough rows for our collection
96
98
@collection . each do |elem |
97
- tr ( id : dom_id_for ( elem ) , class : @row_class &.call ( elem ) )
99
+ tr ( id : dom_id_for ( elem ) , class : @row_class &.call ( elem ) , data : @row_data &. call ( elem ) )
98
100
end
99
101
end
100
102
end
Original file line number Diff line number Diff line change @@ -215,7 +215,9 @@ def build(page_presenter, collection)
215
215
sortable : true ,
216
216
i18n : active_admin_config . resource_class ,
217
217
paginator : page_presenter [ :paginator ] != false ,
218
- row_class : page_presenter [ :row_class ]
218
+ tbody_data : page_presenter [ :tbody_data ] ,
219
+ row_class : page_presenter [ :row_class ] ,
220
+ row_data : page_presenter [ :row_data ]
219
221
}
220
222
221
223
if page_presenter . block
Original file line number Diff line number Diff line change 294
294
end
295
295
end
296
296
297
+ context "when tbody_data" do
298
+ let ( :table ) do
299
+ render_arbre_component assigns , helpers do
300
+ table_for ( collection , tbody_data : { size : collection . size } ) do
301
+ column :starred
302
+ end
303
+ end
304
+ end
305
+
306
+ it "should render data-size attribute within tbody tag" do
307
+ tbody = table . find_by_tag ( "tbody" ) . first
308
+ expect ( tbody . attributes ) . to include ( data : { size : 3 } )
309
+ end
310
+ end
311
+
297
312
context "when row_class" do
298
313
let ( :table ) do
299
314
render_arbre_component assigns , helpers do
313
328
end
314
329
end
315
330
331
+ context "when row_data" do
332
+ let ( :table ) do
333
+ render_arbre_component assigns , helpers do
334
+ table_for ( collection , row_data : -> e { { title : e . title } } ) do
335
+ column :title
336
+ end
337
+ end
338
+ end
339
+
340
+ it "should render data-title attribute within collection row" do
341
+ trs = table . find_by_tag ( "tr" )
342
+ expect ( trs . size ) . to eq 4
343
+ expect ( trs . first . attributes . to_s ) . to eq ""
344
+ expect ( trs . second . attributes ) . to include ( data : { title : "First Post" } )
345
+ expect ( trs . third . attributes ) . to include ( data : { title : "Second Post" } )
346
+ expect ( trs . fourth . attributes ) . to include ( data : { title : "Third Post" } )
347
+ end
348
+ end
349
+
316
350
context "when i18n option is specified" do
317
351
around do |example |
318
352
with_translation %i[ activerecord attributes post title ] , "Name" do
You can’t perform that action at this time.
0 commit comments