Class: LightningUiKit::PaginationComponent

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

Instance Method Summary collapse

Methods inherited from BaseComponent

#merge_classes

Methods included from HeroiconHelper

#heroicon

Constructor Details

#initialize(current_page:, total_pages:, path:, page_param: "page", params: {}, with_arrows: false, **options) ⇒ PaginationComponent

Returns a new instance of PaginationComponent.



2
3
4
5
6
7
8
9
10
# File 'app/components/lightning_ui_kit/pagination_component.rb', line 2

def initialize(current_page:, total_pages:, path:, page_param: "page", params: {}, with_arrows: false, **options)
  @current_page = current_page
  @total_pages = total_pages
  @with_arrows = with_arrows
  @path = path
  @page_param = page_param
  @params = params
  @options = options
end

Instance Method Details



27
28
29
30
31
32
# File 'app/components/lightning_ui_kit/pagination_component.rb', line 27

def arrow_link_classes
  "lui:min-w-[2.25rem] lui:aspect-square lui:flex lui:items-center lui:justify-center lui:rounded-lg lui:border \
   lui:focus:outline-hidden lui:focus:outline lui:focus:outline-2 lui:focus:outline-offset-2 lui:focus:outline-focus \
   lui:data-[disabled]:opacity-50 lui:data-[disabled]:pointer-events-none lui:border-transparent lui:text-foreground \
   lui:hover:bg-surface-hover"
end

#data(disabled: false, active: false) ⇒ Object



12
13
14
15
16
17
# File 'app/components/lightning_ui_kit/pagination_component.rb', line 12

def data(disabled: false, active: false)
  {}.tap do |data|
    data[:disabled] = true if disabled
    data[:active] = true if active
  end
end


19
20
21
22
23
24
25
# File 'app/components/lightning_ui_kit/pagination_component.rb', line 19

def link_classes
  "lui:min-w-[2.25rem] lui:flex lui:items-center lui:justify-center lui:rounded-lg lui:border lui:text-base/6 lui:font-semibold lui:px-[calc(--spacing(3.5)-1px)] \
   lui:py-[calc(--spacing(2.5)-1px)] lui:sm:px-[calc(--spacing(3)-1px)] lui:sm:py-[calc(--spacing(1.5)-1px)] lui:sm:text-sm/6 \
   lui:focus:outline-hidden lui:focus:outline lui:focus:outline-2 lui:focus:outline-offset-2 lui:focus:outline-focus \
   lui:data-[disabled]:opacity-50 lui:data-[disabled]:pointer-events-none lui:border-transparent lui:text-foreground \
   lui:data-[active]:bg-surface-hover lui:hover:bg-surface-hover"
end

#pages_with_gapsObject



34
35
36
37
38
39
40
41
42
43
44
# File 'app/components/lightning_ui_kit/pagination_component.rb', line 34

def pages_with_gaps
  return (1..@total_pages).to_a if @total_pages <= 7

  (1..@total_pages).to_a.each_with_object([]) do |page, pages|
    if page == 1 || page == @total_pages || page == @current_page || (page - @current_page).abs <= 2
      pages << page
    elsif pages.last != :gap
      pages << :gap
    end
  end
end

#url(page) ⇒ Object



46
47
48
49
# File 'app/components/lightning_ui_kit/pagination_component.rb', line 46

def url(page)
  query = @params.merge(@page_param => page).to_query
  "#{@path}?#{query}"
end