Skip to content

Commit 5b97491

Browse files
committed
chore: initial import
0 parents  commit 5b97491

File tree

14 files changed

+272
-0
lines changed

14 files changed

+272
-0
lines changed

.eslintignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cjs/
2+
es/
3+
umd.js

.eslintrc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"parser": "babel-eslint",
3+
"extends": [
4+
"eslint:recommended",
5+
"plugin:react/recommended",
6+
"plugin:import/recommended",
7+
"plugin:prettier/recommended",
8+
"plugin:react-hooks/recommended"
9+
],
10+
"env": {
11+
"node": true,
12+
"es6": true
13+
}
14+
}

.github/workflows/ci.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: ci
2+
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
branches: [master]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
matrix:
15+
node-version: [14.x]
16+
17+
steps:
18+
- uses: actions/checkout@v2
19+
- name: Use Node.js ${{ matrix.node-version }}
20+
uses: actions/setup-node@v2
21+
with:
22+
node-version: ${{ matrix.node-version }}
23+
- run: npm i
24+
- run: npm run lint
25+
- run: npm run build
26+
- run: npm test

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
node_modules/
2+
cjs/
3+
es/
4+
5+
.eslintcache
6+
package-lock.json
7+
.token

.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"arrowParens": "avoid",
3+
"semi": false,
4+
"singleQuote": true,
5+
"trailingComma": "es5"
6+
}

.release-it.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"git": {
3+
"commitMessage": "chore: 🚀 release v${version}"
4+
},
5+
"github": {
6+
"release": true
7+
},
8+
"hooks": {
9+
"after:bump": ["npm run toc"]
10+
}
11+
}

LICENSE

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
ISC License
2+
3+
Copyright (c) [year], [fullname]
4+
5+
Permission to use, copy, modify, and/or distribute this software for any
6+
purpose with or without fee is hereby granted, provided that the above
7+
copyright notice and this permission notice appear in all copies.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

commitlint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = { extends: ['@commitlint/config-conventional'] }

examples/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
umd.js

examples/simple.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>Simple</title>
6+
<script src="https://unpkg.com/react@17/umd/react.development.js"></script>
7+
<script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
8+
9+
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
10+
<script src="umd.js"></script>
11+
</head>
12+
<body>
13+
<div id="root"></div>
14+
<script type="text/babel" src="simple.js"></script>
15+
<script type="text/babel">
16+
ReactDOM.render(<Simple />, document.getElementById('root'))
17+
</script>
18+
</body>
19+
</html>

examples/simple.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* eslint-disable no-undef */
2+
/* eslint-disable no-unused-vars */
3+
/* eslint-disable react/react-in-jsx-scope */
4+
5+
function Simple() {
6+
const {
7+
pageNumber,
8+
currentToken,
9+
useUpdateToken,
10+
changePageNumber,
11+
changePageSize,
12+
} = useTokenPagination()
13+
14+
function previousPage() {
15+
changePageNumber(pageNumber - 1)
16+
}
17+
function nextPage() {
18+
changePageNumber(pageNumber + 1)
19+
}
20+
21+
useUpdateToken('after page ' + pageNumber)
22+
23+
return (
24+
<>
25+
<h1>hello world</h1>
26+
<pre>{JSON.stringify({ currentToken }, null, 2)}</pre>
27+
<div>
28+
<button onClick={previousPage}>&lt;&lt;</button>
29+
{' '}
30+
{pageNumber}
31+
{' '}
32+
<button onClick={nextPage}>&gt;&gt;</button>
33+
</div>
34+
</>
35+
)
36+
}

package.json

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"name": "token-pagination-hooks",
3+
"version": "1.0.0",
4+
"description": "React Hook for token based pagination",
5+
"author": "Simone Busoli <[email protected]>",
6+
"repository": "simoneb/token-pagination-hooks",
7+
"main": "cjs/index.js",
8+
"module": "es/index.js",
9+
"files": [
10+
"cjs/",
11+
"es/",
12+
"src/"
13+
],
14+
"scripts": {
15+
"dev": "npm run build:watch",
16+
"clean": "rimraf cjs es",
17+
"prepare": "npm run clean && npm run build",
18+
"build": "rollup -c",
19+
"build:watch": "rollup -c -w",
20+
"lint": "eslint .",
21+
"lint:fix": "eslint . --fix",
22+
"release": "dotenv -e .token release-it --",
23+
"toc": "markdown-toc README.md -i",
24+
"test": "echo test"
25+
},
26+
"keywords": [],
27+
"license": "ISC",
28+
"devDependencies": {
29+
"@commitlint/cli": "^11.0.0",
30+
"@commitlint/config-conventional": "^11.0.0",
31+
"babel-eslint": "^10.1.0",
32+
"dotenv-cli": "^4.0.0",
33+
"eslint": "^7.16.0",
34+
"eslint-config-prettier": "^7.1.0",
35+
"eslint-plugin-import": "^2.22.1",
36+
"eslint-plugin-prettier": "^3.3.0",
37+
"eslint-plugin-react": "^7.22.0",
38+
"eslint-plugin-react-hooks": "^4.2.0",
39+
"husky": "^4.3.6",
40+
"jest": "^26.6.3",
41+
"lint-staged": "^10.5.3",
42+
"markdown-toc": "^1.2.0",
43+
"prettier": "^2.2.1",
44+
"react": "^17.0.1",
45+
"release-it": "^14.2.2",
46+
"rollup": "^2.35.1"
47+
},
48+
"peerDependencies": {
49+
"react": "^17.0.1"
50+
},
51+
"husky": {
52+
"hooks": {
53+
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
54+
"pre-commit": "lint-staged"
55+
}
56+
},
57+
"lint-staged": {
58+
"*.js": "eslint --cache --fix"
59+
}
60+
}

rollup.config.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export default {
2+
input: 'src/index.js',
3+
output: [
4+
{
5+
dir: 'cjs',
6+
format: 'cjs',
7+
exports: 'default',
8+
},
9+
{
10+
dir: 'es',
11+
format: 'es',
12+
},
13+
{
14+
file: 'examples/umd.js',
15+
format: 'umd',
16+
name: 'useTokenPagination',
17+
globals: {
18+
react: 'React',
19+
},
20+
},
21+
],
22+
external: ['react'],
23+
}

src/index.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { useCallback, useEffect, useState } from 'react'
2+
3+
export default function useTokenPagination(firstPage = 1) {
4+
const [{ pageNumber, pageToToken }, setState] = useState({
5+
pageNumber: firstPage,
6+
pageToToken: {
7+
[firstPage]: '',
8+
},
9+
})
10+
11+
const changePageNumber = useCallback(pageNumber => {
12+
setState(s => ({
13+
...s,
14+
pageNumber,
15+
}))
16+
}, [])
17+
18+
const changePageSize = useCallback(() => {
19+
setState(s => ({
20+
...s,
21+
pageNumber: firstPage,
22+
pageToToken: {
23+
[firstPage]: '',
24+
},
25+
}))
26+
}, [firstPage])
27+
28+
const useUpdateToken = useCallback(
29+
function useUpdateToken(nextToken = '') {
30+
useEffect(() => {
31+
setState(s => ({
32+
...s,
33+
pageToToken: {
34+
...s.pageToToken,
35+
[pageNumber + 1]: nextToken,
36+
},
37+
}))
38+
}, [nextToken])
39+
},
40+
[pageNumber]
41+
)
42+
43+
return {
44+
pageNumber,
45+
currentToken: pageToToken[pageNumber],
46+
useUpdateToken,
47+
changePageNumber,
48+
changePageSize,
49+
}
50+
}

0 commit comments

Comments
 (0)