aviflombaum / shadcn-rails

https://shadcn.rails-components.com
MIT License
470 stars 32 forks source link

Improve options parsing for all helper methods #32

Open aviflombaum opened 10 months ago

aviflombaum commented 10 months ago
  def table_row(**options, &block)
    content_tag :tr,
      options.reverse_merge(
        class: tw("border-b transition-colors hover:bg-muted/50 data-[state=selected]:bg-muted", options[:class])
      ),
      & block
  end

By using reverse merge and passing in the entire options to the content tag (or to content tag to be used in partials), we can glob all things like data or aria or whatever else is passed into options correctly while still providing defaults, including merging tailwind classes with custom classes provided. As opposed to:

  def render_table(caption = nil, **options, &block)
    options[:class] = tw("w-full text-sm border-b", options[:class])
    content_tag :table, class: options[:class] do
      if caption.present?
        content_tag :caption, caption, class: "mt-4 text-sm text-muted-foreground " do
          capture(&block)
        end
      else
        capture(&block)
      end
    end
  end

Here by explicitly only handling the class of options, something like data: {controller:} would be missed.

bibstha commented 3 weeks ago

Shouldn't we use merge instead of the reverse_merge? The reverse_merge here introduces a but that I fixed in https://github.com/aviflombaum/shadcn-rails/pull/67