2.0.0-rc.4 • Published 9 months ago

@asphalt-react/pagination v2.0.0-rc.4

Weekly downloads
-
License
UNLICENSED
Repository
-
Last release
9 months ago

Pagination

npm

Pagination component divides content into discrete pages. Use this component to show long lists of data divided across pages for a better experience. Pagination contains a list of links or buttons called page tiles to navigate through the pages. You can control the number of page tiles visible. Pagination also allows users to navigate to next, previous, first and last pages through actionable elements called steppers. They are visible by default, but you can choose to hide them.

Pagination components exports PerPage component. You can use this to set the number of records visible on each page.

You can also create a custom Pagination through a family of unit components and hooks exported by Pagination and other Asphalt React components.

Usage

import React, { useState } from "react"
import { Link } from "gatsby" // or any other router like react-router
import { Pagination, PerPage } from "@asphalt-react/pagination"

function App() {
  const [active, setActive] = useState(1)
  const [current, setCurrent] = useState(10)

  const getTileProps = (num) => ({
    to: `?page=${num}`,
  })

  const getPerPageTileProps = (recordsPerPage) => ({
    to: `?perPage=${recordsPerPage}`,
  })

  const handlePageChange = (e, pageNum) => {
    setActive(pageNum)
  }

  const handlePerPageChange = (e, records) => {
    setCurrent(records)
  }

  return (
    <div >
      <Pagination
        totalPages={50}
        as={Link}
        getTileProps={getTileProps}
        active={active}
        onChange={handlePageChange}
        slim
      />
      <div>
        <div>Items/Page: </div>
        <PerPage
          as={Link}
          getTileProps={getPerPageTileProps}
          recordsPerPage={[10, 25, 50, 100]}
          active={current}
          onChange={handlePerPageChange} 
        />
      </div>
    </div>
  )
}

Variants

Pagination has 3 variants to configure the number of page tiles:

  1. minimal - contains only active page tile.
  2. slim - contains 3 page tiles including active page tile. This is the default variant.
  3. extended - contains 5 page tiles including active page tile.

Accessibility

  • Use tab or shift+tab to navigate among pages.

Creating a custom Pagination

Pagination exports layout unit components and hooks using which you can create a custom implementation.

Components

  1. BasePagination
  2. PageItem

Hooks

usePagination

React hook to implement the functionality of Pagination in your custom implementation. You can customize the tile count and add truncation to your component. Truncation is a visual aid to let users know that some page tiles are hidden behind the truncation symbol like an ellipses.

import { BasePagination, usePagination } from "@asphalt-react/pagination"
import { ToggleButton } from "@asphalt-react/toggle-button"
import { ToggleButton } from "@asphalt-react/button"
import {
	ChevronBackward,
	ChevronForward,
	ChevronLeft,
	ChevronRight,
	More
} from "@asphalt-react/iconpack"

export const Pagination = () => {
	const { getPagesList } = usePagination({
    active,
    totalPages,
    tileCount: 5,
  })

	const pages = getPagesList({ truncate: false })

	return (
		<BasePagination>
      <PageItem>
        <Button
          link={false}
          disabled={!link && active === 1}
          nude
          icon
          compact
          system
          onClick={(e) => onChange(e, 1)}
        >
          <ChevronBackward />
        </Button>
      </PageItem>
      <PageItem>
        <Button
          link={false}
          disabled={!link && active === 1}
          nude
          icon
          compact
          system
          onClick={(e) => onChange(e, active - 1)}
        >
          <ChevronLeft />
        </Button>
      </PageItem>

      {pages.map((pageNum) =>
        <PageItem key={`page-${pageNum}`}>
          <ToggleButton
            link={false}
            seamless
            compact
            underline={false}
            onClick={(e) => onChange(e, pageNum)}
            on={pageNum === active}
          >
            {pageNum}
          </ToggleButton>
        </PageItem>
      )}

      <PageItem>
        <Button
          link={false}
          disabled={!link && active === totalPages}
          nude
          icon
          compact
          system
          onClick={(e) => onChange(e, active + 1)}
        >
          <ChevronRight />
        </Button>
      </PageItem>

      <PageItem>
        <Button
          link={false}
          disabled={!link && active === totalPages}
          nude
          icon
          compact
          system
          onClick={(e) => onChange(e, totalPages)}
        >
          <ChevronForward />
        </Button>
      </PageItem>
    </BasePagination>
	)
}
  1. usePagination accepts the following props:
    • active - active page number.
    • totalPages - total number of pages.
    • tileCount - page tile count.
  2. It returns functions which exposes certain functionality of pagination.
    • isStartTruncated() - returns a boolean value denoting truncation at start of page tiles
    • isEndTruncated - returns a boolean value denoting truncation at end of page tiles
    • getPagesList({ truncate = false }) - returns an array of page numbers which can be used to render the page tiles of pagination. Accepts truncate as a boolean argument.
    • The output of getPagesList contains -1, specifying the position of truncation.

Props

totalPages

Total number of available pages.

typerequireddefault
numbertrueN/A

active

Current page number.

typerequireddefault
numberfalse1

onChange

Callback to be called when page changes.

onChange receives an object with the following shape.

  • event - browser event
  • page - active page number
({ event, page }) => {}
typerequireddefault
funcfalseN/A

as

Html element/React component to replace the default element. Using this prop, you can use your router's link element, such as "react-router-dom"'s Link element.

typerequireddefault
elementTypefalse"a"

getTileProps

Function to generate props such as "href", "id", data-attributes etc. for page tiles.

typerequireddefault
funcfalseN/A

link

Render a link.

typerequireddefault
boolfalsetrue

stepper

Toggle visibility for next and previous stepper.

typerequireddefault
boolfalsetrue

previousStepperProps

Function to generate props such as "href", "id", data-attributes etc. for the previous stepper element.

typerequireddefault
funcfalseN/A

nextStepperProps

Function to generate props such as "href", "id", data-attributes etc. for the next stepper element.

typerequireddefault
funcfalseN/A

edgeStepper

Toggle visibility for left and right edge stepper.

typerequireddefault
boolfalsetrue

leftEdgeStepperProps

Function to generate props such as "href", "id", data-attributes etc. for the left edge stepper element.

typerequireddefault
funcfalseN/A

rightEdgeStepperProps

Function to generate props such as "href", "id", data-attributes etc. for the right edge stepper element.

typerequireddefault
funcfalseN/A

minimal

Variant to show current active tile, stepper and edge stepper.

typerequireddefault
boolfalsefalse

slim

Variant to show 3 page tiles, stepper and edge stepper.

typerequireddefault
boolfalsefalse

extended

Variant to show 5 page tiles, stepper and edge stepper.

typerequireddefault
boolfalsefalse

nextStepperLabel

aria-label for next stepper.

typerequireddefault
stringfalse"go to next page"

previousStepperLabel

aria-label for previous stepper.

typerequireddefault
stringfalse"go to previous page"

leftEdgeStepperLabel

aria-label for left edge stepper.

typerequireddefault
stringfalse"go to first page"

rightEdgeStepperLabel

aria-label for right edge stepper.

typerequireddefault
stringfalse"go to last page"

PerPage

Set number of records visible on each page.

Props

recordsPerPage

Array of numbers representing PerPage indices.

typerequireddefault
arrayOffalse10, 25, 50, 100

as

Html element/React component to replace the default element. Using this prop, you can use your router's link element, such as "react-router-dom"'s Link element.

typerequireddefault
elementTypefalse"a"

active

Number of records per page

typerequireddefault
numberfalse10

getTileProps

Function to generate props such as "href", "id" for the custom link element passed in as prop.

typerequireddefault
funcfalseN/A

onChange

Function to be called when per page value changes

onChange receives an object with the following signature

  • event - click event
  • records - number of records to show per page
 ({event, record}) => void
typerequireddefault
funcfalseN/A

link

Render a link

typerequireddefault
boolfalsetrue

BasePagination

The base container unit component.

Props

children

Container for the BasePagination content.

typerequireddefault
nodefalseN/A

gap

Adds gap between child elements

typerequireddefault
boolfalsetrue

PageItem

Renders the list element for page tiles.

Props

children

Content for a page list item.

typerequireddefault
nodetrueN/A