8000 Add tbody_html and row_html options to TableFor and IndexAsTable by thibaudgg · Pull Request #8423 · activeadmin/activeadmin · GitHub
[go: up one dir, main page]

Skip to content

Add tbody_html and row_html options to TableFor and IndexAsTable #8423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2024

Conversation

thibaudgg
Copy link
Contributor
@thibaudgg thibaudgg commented Aug 4, 2024

This patch adds a tbody_html and row_html options to the TableFor (and
IndexAsTable) classes. These options allow you to pass a hash of HTML options
attributes to the tbody and tr elements.

This is useful when you want to add features to the table that require data
attributes. For example, adding a sortable row via JavaScript.

You can see how I'm currently using this approach in this app:

The existing row_class option is kept for backwards compatibility, but the
row_html will take precedence if both are provided.

Copy link
codecov bot commented Aug 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.11%. Comparing base (37f0545) to head (a56a926).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #8423   +/-   ##
=======================================
  Coverage   99.11%   99.11%           
=======================================
  Files         141      141           
  Lines        4060     4064    +4     
=======================================
+ Hits         4024     4028    +4     
  Misses         36       36           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member
@javierjulio javierjulio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thibaudgg thanks for finding the time to submit this. I'm open to suggestions on the new argument name but this has to be generic, not a "data" attributes specific key.

Comment on lines 96 to 99
@tbody = tbody data: @tbody_data do
# Build enough rows for our collection
@collection.each do |elem|
tr(id: dom_id_for(elem), class: @row_class&.call(elem))
tr(id: dom_id_for(elem), class: @row_class&.call(elem), data: @row_data&.call(elem))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to introduce "data" based keys. We should have an "html" or "html_options" based key otherwise this can only be used for data attributes and we'll be back here when needing to add support for other attributes. I'm open for suggestions on how to name it but I don't want these options being "data" specific. Something like tbody_html or tbody_html_options so other attributes can be set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I switched to the tbody_html options, as I found more used of *_html than *_html_options in the codebase.

For the row_data it's a bit more complex as we need the proc on the row element. We could add a new row_html method with an optional proc arguments, but we would also need to still support the row_class for backward compatibility. Which I guess would overwrite a class set by row_html if both options are used? What would you prefer?

Copy link
Member
@javierjulio javierjulio Aug 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you prefer to keep row_class support, I think it would be fine if it's present to reverse merge that into the new row_html value so the class in row_html would take precedence over row_class. We want to phase out row_class and for setting whichever html options desired. Thank you.

< 8000 div class="ml-5 edit-comment-hide">
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added row_html, but kept row_class for backward compatibility. I think we should be good now.

@thibaudgg thibaudgg requested a review from javierjulio August 9, 2024 09:39
thibaudgg added a commit to csa-admin-org/csa-admin that referenced this pull request Aug 9, 2024
Use my branche until the PR is merged and release.

activeadmin/activeadmin#8423
@thibaudgg thibaudgg changed the title TableFor, add tbody_data and row_data options TableFor, add tbody_html and row_html options Aug 16, 2024
@thibaudgg thibaudgg force-pushed the table-for-data branch 2 times, most recently from 7eca533 to e41e0a1 Compare August 16, 2024 09:40
thibaudgg added a commit to csa-admin-org/csa-admin that referenced this pull request Aug 16, 2024
Copy link
Member
@javierjulio javierjulio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thibaudgg I just have documentation updates, everything else looks great. We'll leave in row_class support for now and handle a proper deprecation separately. My main concern is just getting in the options to handle any attribute as you've added here. Thank you again for tackling this.

@javierjulio javierjulio changed the title TableFor, add tbody_html and row_html options Add tbody_html and row_html options to TableFor and IndexAsTable Aug 23, 2024
This patch adds a `tbody_html` and `row_html` options to the `TableFor` (and
`IndexAsTable`) classes. These options allow you to pass a hash of html options
attributes to the `tbody` and `tr` elements.

This is useful when you want to add features to the table that require data
attributes. For example, adding a sortable row via JavaScript.

You can see how I'm currently using this approach in [this app](https://github.com/acp-admin/acp-admin):
- [monkey patching](https://github.com/acp-admin/acp-admin/blob/master/config/initializers/active_admin.rb#L261-L328) (same approach as this PR)
- [using both options on a `table_for` method](https://github.com/acp-admin/acp-admin/blob/master/app/views/active_admin/deliveries/_baskets.html.arb#L10-L13) for using [Stimulus Sortable component](https://www.stimulus-components.com/docs/stimulus-sortable/).

The existing `row_class` option is kept for backwards compatibility, but the
`row_html` will take precedence if both are provided.
@thibaudgg
Copy link
Contributor Author

@javierjulio great, thanks for the review. I just updated the PR with your changes. 👍🏻

@javierjulio javierjulio merged commit 8bee931 into activeadmin:master Aug 23, 2024
24 checks passed
@thibaudgg thibaudgg deleted the table-for-data branch August 23, 2024 14:52
thibaudgg added a commit to csa-admin-org/csa-admin that referenced this pull request Aug 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0