Class: Shadcn::PaginationComponent

Inherits:
BaseComponent
  • Object
show all
Defined in:
app/components/shadcn/pagination_component.rb

Overview

Pagination component for navigating paged content Matches shadcn/ui Pagination component

Supports three usage patterns:

  1. Slot-based API (full control):
<%= render Shadcn::PaginationComponent.new do |pagination| %>
<% pagination.with_pagination_content do |content| %>
  <% content.with_previous(href: "/page/1") %>
  <% content.with_item(href: "/page/1") { "1" } %>
  <% content.with_item(href: "/page/2", active: true) { "2" } %>
  <% content.with_item(href: "/page/3") { "3" } %>
  <% content.with_ellipse %>
  <% content.with_next_page(href: "/page/3") %>
<% end %>
<% end %>
  1. Collection-based API (Kaminari/will_paginate): <%= render Shadcn::PaginationComponent.new(collection: @posts) %>

  2. Pagy-based API: <%= render Shadcn::PaginationComponent.new(pagy: @pagy) %>

Constant Summary collapse

BASE_CLASSES =
"mx-auto flex w-full justify-center"

Instance Attribute Summary

Attributes inherited from BaseComponent

#class_name, #data, #html_options

Instance Method Summary collapse

Methods inherited from BaseComponent

#content

Methods included from Rails::Helpers::ClassNameHelper

#cn

Constructor Details

#initialize(collection: nil, pagy: nil, url_builder: nil, window: 2, **options) ⇒ PaginationComponent

Returns a new instance of PaginationComponent.

Parameters:

  • collection (Object, nil) (defaults to: nil)

    Kaminari or will_paginate collection

  • pagy (Object, nil) (defaults to: nil)

    Pagy object

  • url_builder (Proc, nil) (defaults to: nil)

    Lambda to generate page URLs, receives page number

  • window (Integer) (defaults to: 2)

    Number of pages to show around current page



38
39
40
41
42
43
44
# File 'app/components/shadcn/pagination_component.rb', line 38

def initialize(collection: nil, pagy: nil, url_builder: nil, window: 2, **options)
  super(**options)
  @collection = collection
  @pagy = pagy
  @url_builder = url_builder || ->(page) { "?page=#{page}" }
  @window = window
end

Instance Method Details

#before_renderObject



46
47
48
49
50
51
52
53
# File 'app/components/shadcn/pagination_component.rb', line 46

def before_render
  # Pre-compute auto-generated content if needed
  if auto_generate?
    @should_render = total_pages > 1
  else
    @should_render = true
  end
end