0.1.3 • Published 4 years ago

pagination-handle v0.1.3

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

Pagination Handle

Generic Pagination Handling JavaScript Library

Build Status Coverage Status

Table of Contents:

  • General
    • Links
    • Build Size
  • Usage
    • Import/Setup
    • Get Pagination State
    • Use the Generic Attributes to create your own element/component
    • Manual Navigation & Callback
    • API
  • Contribution/Development
    • Primary Tech Stack
    • CLI Command
    • Folder Structure

General

Links

Build Size (ESM/UMD/CJS)

  • Minified Size: 8KB
  • Gzipped: 2.7KB

Usage

Import/Setup:

  • via ES Module (Js/Ts/Tsx file)
import PgnHandle from 'pagination-handle';
const pgnHandle = new PgnHandle();
  • via CommonJs (Js file)
const PgnHandle = require('pagination-handle');
const pgnHandle = new PgnHandle();
  • via UMD (Js file)
const pgnHandle = new PaginationHandle();
  • via Script Tag (HTML)
<script src="<path>/<to>/<library>/umd/main.min.js"></script>
<script>
    const pgnHandle = new PaginationHandle();
</script>

Get Pagination State:

const sampleData = [ 'a', 'b', 'c' ];
const totalRecord = sampleData.length;
const pgnOption = {
    page: 0,                // start at 1st page
    increment: [ 1, 2 ],    // typically for select dropdown: 1 per page, 2 per page
    incrementIdx: 0,        // means increment is 1 from above
    maxSpread: 3          
};

// using builtin Type under `PgnType` namespace (no import required)
let someOption: PgnType.IOption;

// get the pagination state only
const pgnState = pgnHandle.getState(totalRecord, pgnOption);
const {
    perPage,
    totalPage,
    curr,
    pageNo,
    startIdx,
    endIdx,
    first,
    prev,
    next,
    last,
    totalRecord,
    startRecord,
    endRecord,
    ltSpread,
    rtSpread,
    maxSpread
} = pgnState;

// get the corresponding "visible" data slice
const sampleDataSlice = sampleData.slice(startIdx, endIdx);

// get the generic pagination element attributes to construct elements on your own
const genericComponentAttr = pgnHandle.createGenericCmpAttr({
    totalRecord,
    state: pgnState,
    option: pgnOption,
    callback: () => console.log('pgn state changed')
});
const {
    firstBtnAttr,
    prevBtnAttr,
    nextBtnAttr,
    lastBtnAttr,
    ltSpreadBtnsAttr,
    rtSpreadBtnsAttr,
    pageSelectAttr,
    perPageSelectAttr,
} = genericComponentAttr;

Use the Generic Attributes to create your own element/component

// JavaScript Example
const btn = document.createElement('button');
btn.type = 'button';
btn.disabled = firstBtnAttr.disabled;
btn.addEventListener('click', firstBtnAttr.onClick);
btn.textContent = firstBtnAttr.title;

// React Example
const PgnFristBtn = (firstBtnAttr) => {
    const { title, disabled, onClick } = firstBtnAttr;
    return (
        <button disabled={disabled} onClick={onClick}>
            {title}
        </button>
    );
};

Manual Navigation & Callback:

// start with page 0
const pgnStateOne = pgnHandle.getState(totalRecord, pgnOption);

// go to next page
const pgnStateTwo = pgnHandle.getState(totalRecord, {
    ...pgnOption, 
    page: pgnStateOne.next
});

API

Pagination State Object pgnState

PropertyTypeDescription
perPageintegercurrent total number displayed/allowed per page
totalPageintegertotal number of pages
currintegercurrent page index (starts from 0)
pageNointegercurrent page number (starts from 1)
startIdxintegerstart index (inclusive) for the sliced data
endIdxintegerend index (exclusive) for the sliced data
firstintegerindex for first page
previntegerindex for previous page
nextintegerindex for next page
lastintegerindex for last page
totalRecordintegertotal number of records in data
startRecordintegerstarting index for current displayed data
endRecordintegerend index (non-inclusive) for current displayed data
ltSpreadinteger or stringeither a number (if less than the maxSpread) or '...' to indicate the non-displayed pages
rtSpreadinteger or stringeither a number (if less than the maxSpread) or '...' to indicate the non-displayed pages
maxSpreadintegertotal page interval that is represented by the spread '...', i.e. not shown

Pagination Option Object pgnOption

PropertyTypeDescription
pageintegerindex for default/starting page number (starts from 0)
incrementarray of integersavailable increments typically for the dropdown, e.g. 10, 20
incrementIdxintegerthe default increment value above
maxSpreadintegermaximum number of page interval that is represented by the spread '...'

General Button Attribute Object firstBtnAttr/prevBtnAttr/nextBtnAttr/lastBtnAttr

PropertyTypeDescription
titlestringname of the button, one of the values: first prev next last
disabledbooleanwhether the button is disabled based on the current pagination state
onClickfunctionpage navigation callback when the button is clicked

Spread Button Attribute Object ltSpreadBtnsAttr/rtSpreadBtnsAttr

PropertyTypeDescription
titlestringname of the spread button, default: left-spread or right-spread
isSpreadbooleanwhether the button is the spread symbol (false if it is a number)
onClickfunctionpage navigation callback when the button is clicked

Select Attribute Object perPageSelectAttr/getPageSelectAttr

PropertyTypeDescription
titlestringname of select dropdown, default: per page select
disabledbooleanwhether select dropdown is disabled based on the current pagination
optionsarray of integerslist of total number displayed/allowed per page OR list of navigatable pages
selectedOptionValueintegervalue of current total number displayed/allowed per page OR current page number
selectedOptionIdxintegercorresponding index of selectedOptionValue property in options property
onSelectfunctionpage navigation callback when the select option is changed

Contribution/Development

Primary Tech Stack:

  • TypeScript 3.8.3
  • Node 13.12.0 | Npm 6.14.4
  • Jest 25.5.3

CLI Command

  • Build
npm run build
  • Unit Test
npm run test
  • Lint
npm run lint
  • Compile Js
npm run compile-js
  • Compile Bundled .d.ts file
npm run compile-dts
  • Clear "dist" folder
npm run clean

Folder Structure

dist/                       // output files
    cjs/                    // common js format
        index.js            
        index.js.map
        index.min.js
    esm/                    // es module js format
        index.js            
        index.js.map
        index.min.js
    umd/                    // universal module definition format
        index.js            
        index.js.map
        index.min.js
    index.d.ts              // typings
    
src/                        // source code
    index.ts                // main entry file
    index.spec.ts           // unit test
    type.ts                 // typings

rollup.config.js            // rollup (bundler/build) config
.eslintrc.js                // EsLint config
tsconfig.json               // TypeScript config 
babel.config.js             // Babel config - transform TS and new Js features into ES5/Standard JavaScript
jest.config.js              // Jest config - unit test
package.json                // dev-dependencies, dependencies for the project    
.gitignore                  // git ignored
README.md                   // readme
0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.4

4 years ago

0.0.1

4 years ago