# frozen_string_literal: true module Koi module Tables # Custom header row component, in order to override the default header cell component # for number columns, we add a class to the header cell to allow for custom styling class HeaderRowComponent < Katalyst::Tables::HeaderRowComponent # Renders a boolean column header # @param method [Symbol] the method to call on the record to get the value # @param attributes [Hash] additional arguments are applied as html attributes to the th element # @option attributes [String] :label (nil) The label options to display in the header # @option attributes [Hash] :link ({}) The link options for the sorting link # @option attributes [String] :width (:xs) The width of the column, can be +:xs+, +:s+, +:m+, +:l+ or nil # # @example Render a boolean column header # <% row.boolean :active %> # => Active # # @example Render a boolean column header with a custom label # <% row.boolean :active, label: "Published" %> # => Published # # @example Render a boolean column header with medium width # <% row.boolean :active, width: :m %> # # => Active # # @see Koi::Tables::BodyRowComponent#boolean def boolean(method, **attributes, &block) header_cell(method, component: Header::BooleanComponent, **attributes, &block) end # Renders a date column header # @param method [Symbol] the method to call on the record to get the value # @param attributes [Hash] additional arguments are applied as html attributes to the th element # @option attributes [String] :label (nil) The label options to display in the header # @option attributes [Hash] :link ({}) The link options for the sorting link # @option attributes [String] :width (:s) The width of the column, can be +:xs+, +:s+, +:m+, +:l+ or nil # # @example Render a date column header # <% row.date :published_on %> # => Published on # # @example Render a date column header with a custom label # <% row.date :published_on, label: "Date" %> # => Date # # @example Render a date column header with small width # <% row.date :published_on, width: :s %> # # => Published on # # @see Koi::Tables::BodyRowComponent#date def date(method, **attributes, &block) header_cell(method, component: Header::DateComponent, **attributes, &block) end # Renders a datetime column header # @param method [Symbol] the method to call on the record to get the value # @param attributes [Hash] additional arguments are applied as html attributes to the th element # @option attributes [String] :label (nil) The label options to display in the header # @option attributes [Hash] :link ({}) The link options for the sorting link # @option attributes [String] :width (:m) The width of the column, can be +:xs+, +:s+, +:m+, +:l+ or nil # # @example Render a datetime column header # <% row.datetime :created_at %> # => Created at # # @example Render a datetime column header with a custom label # <% row.datetime :created_at, label: "Published at" %> # => Published at # # @example Render a datetime column header with small width # <% row.datetime :created_at, width: :s %> # # => Created at # # @see Koi::Tables::BodyRowComponent#datetime def datetime(method, **attributes, &block) header_cell(method, component: Header::DateTimeComponent, **attributes, &block) end # Renders a number column header # @param method [Symbol] the method to call on the record to get the value # @param attributes [Hash] additional arguments are applied as html attributes to the th element # @option attributes [String] :label (nil) The label options to display in the header # @option attributes [Hash] :link ({}) The link options for the sorting link # @option attributes [String] :width (:xs) The width of the column, can be +:xs+, +:s+, +:m+, +:l+ or nil # # @example Render a number column header # <% row.number :comment_count %> # => Comments # # @example Render a number column header with a custom label # <% row.number :comment_count, label: "Comments" %> # => Comments # # @example Render a number column header with medium width # <% row.number :comment_count, width: :m %> # # => Comment Count # # @see Koi::Tables::BodyRowComponent#number def number(method, **attributes, &block) header_cell(method, component: Header::NumberComponent, **attributes, &block) end # Renders a currency column header # @param method [Symbol] the method to call on the record to get the value # @param attributes [Hash] additional arguments are applied as html attributes to the th element # @option attributes [String] :label (nil) The label options to display in the header # @option attributes [Hash] :link ({}) The link options for the sorting link # @option attributes [String] :width (:s) The width of the column, can be +:xs+, +:s+, +:m+, +:l+ or nil # # @example Render a currency column header # <% row.currency :price %> # => Price # # @example Render a currency column header with a custom label # <% row.currency :price, label: "Amount($)" %> # => Amount($) # # @example Render a currency column header with medium width # <% row.currency :price, width: :m %> # # => Price # # @see Koi::Tables::BodyRowComponent#currency def currency(method, **attributes, &block) header_cell(method, component: Header::CurrencyComponent, **attributes, &block) end # Renders a rich text column header # @param method [Symbol] the method to call on the record to get the value # @param attributes [Hash] additional arguments are applied as html attributes to the th element # @option attributes [String] :label (nil) The label options to display in the header # @option attributes [Hash] :link ({}) The link options for the sorting link # @option attributes [String] :width (nil) The width of the column, can be +:xs+, +:s+, +:m+, +:l+ or nil # # @example Render a rich text column header # <% row.rich_text :content %> # => Content # # @example Render a rich text column header with a custom label # <% row.rich_text :content, label: "Post content" %> # => Post content # # @example Render a rich text column header with large width # <% row.rich_text :content, width: :l %> # # => Content # # @see Koi::Tables::BodyRowComponent#rich_text def rich_text(method, **attributes, &block) header_cell(method, component: Header::TextComponent, **attributes, &block) end # Renders a link column header # @param method [Symbol] the method to call on the record to get the value # @param attributes [Hash] additional arguments are applied as html attributes to the th element # @option attributes [String] :label (nil) The label options to display in the header # @option attributes [Hash] :link ({}) The link options for the sorting link # @option attributes [String] :width (nil) The width of the column, can be +:xs+, +:s+, +:m+, +:l+ or nil # # @example Render a link column header # <% row.link :link %> # => Link # # @example Render a link column header with a custom label # <% row.link :link, label: "Post" %> # => Post # # @example Render a link column header with small width # <% row.link :content, width: :s %> # # => Content # # @see Koi::Tables::BodyRowComponent#link def link(method, **attributes, &block) header_cell(method, component: Header::LinkComponent, **attributes, &block) end # Renders a text column header # @param method [Symbol] the method to call on the record to get the value # @param attributes [Hash] additional arguments are applied as html attributes to the th element # @option attributes [String] :label (nil) The label options to display in the header # @option attributes [Hash] :link ({}) The link options for the sorting link # @option attributes [String] :width (nil) The width of the column, can be +:xs+, +:s+, +:m+, +:l+ or nil # # @example Render a text column header # <% row.text :content %> # => Content # # @example Render a text column header with a custom label # <% row.text :content, label: "Description" %> # => Description # # @example Render a text column header with large width # <% row.text :content, width: :l %> # # => Content # # @see Koi::Tables::BodyRowComponent#text def text(method, **attributes, &block) header_cell(method, component: Header::TextComponent, **attributes, &block) end # Renders a attachment column header # @param method [Symbol] the method to call on the record to get the value # @param attributes [Hash] additional arguments are applied as html attributes to the th element # @option attributes [String] :label (nil) The label options to display in the header # @option attributes [Hash] :link ({}) The link options for the sorting link # @option attributes [String] :width (nil) The width of the column, can be +:xs+, +:s+, +:m+, +:l+ or nil # # @example Render a attachment column header # <% row.attachment :attachment %> # => Attachment # # @example Render a attachment column header with a custom label # <% row.attachment :attachment, label: "Document" %> # => Document # # @example Render a attachment column header with small width # <% row.attachment :attachment, width: :s %> # # => Attachment # # @see Koi::Tables::BodyRowComponent#attachment def attachment(method, **attributes, &block) header_cell(method, component: Header::AttachmentComponent, **attributes, &block) end private def header_cell(method, component: HeaderCellComponent, **attributes, &block) with_column(component.new(@table, method, link: @link_attributes, **attributes), &block) end end end end