|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Primer |
| 4 | + module OpenProject |
| 5 | + class DataTable |
| 6 | + # This component is part of `Primer::OpenProject::DataTable` and should |
| 7 | + # not be used as a standalone component. |
| 8 | + # |
| 9 | + # It renders the table's pagination footer: an optional "range" summary |
| 10 | + # (e.g. "1 ‒ 10 of 95") alongside the paginated navigation provided by |
| 11 | + # <%= link_to_component(Primer::OpenProject::Pagination) %>. |
| 12 | + class PaginationFooter < Primer::Component |
| 13 | + status :open_project |
| 14 | + |
| 15 | + # @param current_page [Integer] |
| 16 | + # The page that is currently being viewed (1-based). |
| 17 | + # @param total_count [Integer, nil] |
| 18 | + # The total number of items across all pages. Required (together with |
| 19 | + # `page_size`) to render the range summary. |
| 20 | + # @param page_size [Integer, nil] |
| 21 | + # The number of items rendered per page. Required (together with |
| 22 | + # `total_count`) to render the range summary. |
| 23 | + # @param aria_label [String, nil] |
| 24 | + # Accessible name for the pagination navigation landmark. |
| 25 | + # @param system_arguments [Hash] |
| 26 | + # System arguments passed to the root element. All remaining arguments |
| 27 | + # are forwarded to <%= link_to_component(Primer::OpenProject::Pagination) %>. |
| 28 | + def initialize(current_page:, total_count: nil, page_size: nil, aria_label: nil, **system_arguments) |
| 29 | + @current_page = current_page |
| 30 | + @total_count = total_count |
| 31 | + @page_size = page_size |
| 32 | + @aria_label = aria_label || I18n.t("pagination.label") |
| 33 | + @pagination_arguments = system_arguments.merge(current_page: current_page, tag: :div) |
| 34 | + end |
| 35 | + |
| 36 | + attr_reader :aria_label |
| 37 | + |
| 38 | + def render? |
| 39 | + @pagination_arguments.key?(:page_count) |
| 40 | + end |
| 41 | + |
| 42 | + def show_range? |
| 43 | + @total_count.present? && @page_size.present? && @current_page.present? |
| 44 | + end |
| 45 | + |
| 46 | + def range_start |
| 47 | + ((@current_page - 1) * @page_size) + 1 |
| 48 | + end |
| 49 | + |
| 50 | + def range_end |
| 51 | + [@current_page * @page_size, @total_count].min |
| 52 | + end |
| 53 | + end |
| 54 | + end |
| 55 | + end |
| 56 | +end |
0 commit comments