1.0.5 • Published 5 years ago

@impedans/usepagination v1.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

@impedans/usepagination

This package comes with a hook and a display-component that's built to recieve the hook data. You can easily render your own pagination buttons using just the data from the hook, but the display component makes your life a little bit easier still.

Example

import React, { useEffect, useState } from "react";
import usePagination, { DisplayPagination } from "@impedans/usepagination";

function App(){
    const [data, setData] = useState();
    const [paginatedList, paginationProps] = usePagination(data, {
        perPage: 6,
        maxButtons: 7
    });

    useEffect(() => {
        let unMounted = false;
        fetch("https://jsonplaceholder.typicode.com/todos/")
            .then(res => res.json())
            .then(res => {
               if (!unMounted) setData(res);
            })
            .catch(err => console.error(err));
        return () => {
            unMounted = true;
        }
    }, []);

    return (
        <main>
            <button onClick={() => paginationProps.goToPage(1)}>Go to page 1</button>
            <ul>
                {paginatedList.map(item => <li>{item.name}</li>}
            </ul>
            <DisplayPagination {...paginationProps} pageNumFunc={(pageNum) => `Page #${pageNum}`} />
        </main>
    );
}

API: usePagination

Props

Prop-nameDescriptionType
listYour list to be paginatedArray
optionsAn object with optional optionsOptionsObject

OptionsObject (all keys are optional)

KeyDescriptionType
perPageHow many entries per page (default: 10)number
onPageChangeFunction that runs on every page change() => void
isHiddenFunction that filters hidden items from list (read more below)(item: any) => void
maxButtonsHow many buttons maximum in the pagination (except next & previous buttons) (default: 5)number

The isHidden function is used when your list hides items not by removing them but by for example adding a hidden: true property. In that case, you can supply a function like this: (item) => !item.hidden.

Output / return

The hook returns an array containing two items:

NameDescriptionType
listYour paginated listArray
paginationAn object functions and data for the paginationPaginationObject

PaginationObject

KeyDescriptionType
pagesAn array with all the page-numbersArray
activePageThe currently active pagenumber
buttonsAn array with all the buttons for rendering the paginationArray<number / string>
previousPageFunction that goes to the previous page() => void)
nextPageFunction that goes to the next page() => void)
goToPageFunction that goes to a specific page-number(pageNum: number) => void)

API: DisplayPagination

DisplayPagination takes a lot of props. First of all, it takes the entire PaginationObject as defined above, but it also takes a few more optional props:

KeyDescriptionType
containerClassnameClassname for the containerstring
nextPrevClassnameClassname for the next & previous page buttonsstring
activeClassnameClassname for the button for the active pagenumberstring
prevButtonThe contents of the previous page buttonReact.ReactNode
nextButtonThe contents of the next page buttonReact.ReactNode
pageNumFunc or renderPageNumA function that runs to render each of the page numbers (read more below)(item: number) => React.ReactNode

renderPageNum (alias pageNumFunc) runs on the inside content of the page-number buttons. This is if you want to change how they are rendered, for example if instead of showing just the page number (1, 2, 3, etc) you wanted to show "Page x" (Page 1, Page 2, Page 3, etc), then you could supply this function: (item) => 'Page ' + item

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago