Class: CCS::Components::GovUK::Pagination

Inherits:
Base
  • Object
show all
Defined in:
lib/ccs/components/govuk/pagination.rb,
lib/ccs/components/govuk/pagination/item.rb,
lib/ccs/components/govuk/pagination/item/tag.rb,
lib/ccs/components/govuk/pagination/increment.rb,
lib/ccs/components/govuk/pagination/item/form.rb,
lib/ccs/components/govuk/pagination/item/ellipsis.rb,
lib/ccs/components/govuk/pagination/increment/next.rb,
lib/ccs/components/govuk/pagination/increment/previous.rb

Overview

GOV.UK Pagination

This helper is used for generating the pagination component from the GDS - Components - Pagination

Defined Under Namespace

Classes: Increment, Item

Constant Summary collapse

DEFAULT_ATTRIBUTES =

The default attributes for the pagination

{ class: 'govuk-pagination' }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(pagination_previous: nil, pagination_items: nil, pagination_next: nil, **options) ⇒ Pagination

Returns a new instance of Pagination.

Parameters:

  • pagination_previous (Hash) (defaults to: nil)

    attributes for the pagination previous, see Previous#initialize for more details.

  • pagination_items (Array<Hash>) (defaults to: nil)

    An array of options for the pagination items. See Item::Tag#initialize and Item::Form#initialize for details of the items in the array.

  • pagination_next (Hash) (defaults to: nil)

    attributes for the pagination next, see Next#initialize for more details.

  • options (Hash)

    options that will be used in customising the HTML

Options Hash (**options):

  • :classes (String)

    additional CSS classes for the pagination HTML

  • :form (ActionView::Helpers::FormBuilder)

    form builder used to create pagination buttons

  • :attributes (Hash)

    any additional attributes that will added as part of the HTML



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ccs/components/govuk/pagination.rb', line 43

def initialize(pagination_previous: nil, pagination_items: nil, pagination_next: nil, **options)
  super(**options)

  block_is_level = pagination_items.blank? && (pagination_previous.present? || pagination_next.present?)

  @options[:attributes][:class] << ' govuk-pagination--block' if block_is_level
  (@options[:attributes][:aria] ||= {})[:label] ||= 'Pagination'

  @pagination_previous = Increment::Previous.new(block_is_level: block_is_level, form: @options[:form], context: @context, **pagination_previous) if pagination_previous
  @pagination_next = Increment::Next.new(block_is_level: block_is_level, form: @options[:form], context: @context, **pagination_next) if pagination_next
  initialize_pagination_items(pagination_items, @options[:form])
end

Instance Method Details

#renderActiveSupport::SafeBuffer

Generates the HTML for the GOV.UK pagination component

Returns:

  • (ActiveSupport::SafeBuffer)


62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ccs/components/govuk/pagination.rb', line 62

def render
  tag.nav(**options[:attributes]) do
    concat(pagination_previous.render) if pagination_previous
    if pagination_items
      concat(tag.ul(class: 'govuk-pagination__list') do
        pagination_items.each { |pagination_item| concat(pagination_item.render) }
      end)
    end
    concat(pagination_next.render) if pagination_next
  end
end