Skip to content

Commit fa2d22d

Browse files
support changing default values for indexType and pageSize
1 parent 8e943f3 commit fa2d22d

7 files changed

Lines changed: 75 additions & 15 deletions

src/__test__/inMemoryPagination.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { act, renderHook } from '@testing-library/react-hooks'
22
import { useSearchAndPagination } from '../inMemoryPagination'
3+
import { IndexType } from '../types'
34

45
test('should initialize pagination', () => {
56
const { result } = renderHook(() => useSearchAndPagination())
@@ -67,3 +68,12 @@ test('clear pagination should reset search and page', () => {
6768
expect(result.current.search).toBe('')
6869
expect(result.current.page).toBe(0)
6970
})
71+
72+
test('change default parameters', () => {
73+
const { result } = renderHook(() =>
74+
useSearchAndPagination({ indexType: IndexType.ONE_BASED, pageSize: 10 })
75+
)
76+
77+
expect(result.current.page).toBe(1)
78+
expect(result.current.size).toBe(10)
79+
})

src/__test__/nextRouterPagination.test.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { act, renderHook } from '@testing-library/react-hooks'
44
import router from 'next/router'
55

66
import { useSearchAndPagination } from '../nextRouterPagination'
7+
import { IndexType } from '../types'
78

89
jest.mock('next/router', () => require('next-router-mock'))
910

@@ -80,3 +81,12 @@ test('clear pagination should reset search and page', () => {
8081
expect(router.query.search).toBeUndefined()
8182
expect(router.query.page).toBeUndefined()
8283
})
84+
85+
test('change default parameters', () => {
86+
const { result } = renderHook(() =>
87+
useSearchAndPagination({ indexType: IndexType.ONE_BASED, pageSize: 10 })
88+
)
89+
90+
expect(result.current.page).toBe(1)
91+
expect(result.current.size).toBe(10)
92+
})

src/__test__/reactRouterPagination.test.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react'
22
import { act, renderHook } from '@testing-library/react-hooks'
33
import { BrowserRouter as Router } from 'react-router-dom'
44
import { useSearchAndPagination } from '../reactRouterPagination'
5+
import { IndexType } from '../types'
56

67
const wrapper: React.FC = ({ children }) => <Router>{children}</Router>
78

@@ -75,3 +76,14 @@ test('clear pagination should reset search and page', () => {
7576
expect(result.current.page).toBe(0)
7677
expect(window.location.search).toBe('')
7778
})
79+
80+
test('change default parameters', () => {
81+
const { result } = renderHook(
82+
() =>
83+
useSearchAndPagination({ indexType: IndexType.ONE_BASED, pageSize: 10 }),
84+
{ wrapper }
85+
)
86+
87+
expect(result.current.page).toBe(1)
88+
expect(result.current.size).toBe(10)
89+
})

src/inMemoryPagination.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import { useState, useCallback, useMemo } from 'react'
2-
import { UseSearchAndPagination } from './types'
1+
import { useCallback, useMemo, useState } from 'react'
2+
import { IndexType, IUseSearchAndPagination } from './types'
33

4-
const useSearchAndPagination = (): UseSearchAndPagination => {
4+
export const useSearchAndPagination: IUseSearchAndPagination = function (
5+
config
6+
) {
7+
const { indexType = IndexType.ZERO_BASED, pageSize = 15 } = config || {}
8+
const firstPage = indexType === IndexType.ZERO_BASED ? 0 : 1
59
const initialState = useMemo(
610
() => ({
7-
page: 0,
8-
size: 15,
11+
page: firstPage,
12+
size: pageSize,
913
searchQuery: '',
1014
}),
1115
[]
@@ -49,5 +53,3 @@ const useSearchAndPagination = (): UseSearchAndPagination => {
4953
size: state.size,
5054
}
5155
}
52-
53-
export { useSearchAndPagination }

src/nextRouterPagination.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useRouter } from 'next/router'
22
import { useCallback } from 'react'
33

4-
import { UseSearchAndPagination } from './types'
4+
import { IndexType, IUseSearchAndPagination } from './types'
55
import { convert } from './utils'
66

77
function getSingleParameterValue(
@@ -10,7 +10,10 @@ function getSingleParameterValue(
1010
return Array.isArray(parameter) ? parameter[0] : parameter
1111
}
1212

13-
export function useSearchAndPagination(): UseSearchAndPagination {
13+
export const useSearchAndPagination: IUseSearchAndPagination = function (
14+
config
15+
) {
16+
const { indexType = IndexType.ZERO_BASED, pageSize = 15 } = config || {}
1417
const router = useRouter()
1518

1619
const setPage = useCallback(
@@ -60,8 +63,11 @@ export function useSearchAndPagination(): UseSearchAndPagination {
6063

6164
return {
6265
search: getSingleParameterValue(router.query.search) || '',
63-
page: convert(getSingleParameterValue(router.query.page) || null, 0),
64-
size: convert(getSingleParameterValue(router.query.size) || null, 15),
66+
page: convert(
67+
getSingleParameterValue(router.query.page) || null,
68+
indexType === IndexType.ZERO_BASED ? 0 : 1
69+
),
70+
size: convert(getSingleParameterValue(router.query.size) || null, pageSize),
6571
actions: {
6672
search,
6773
clear,

src/reactRouterPagination.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { useCallback, useMemo } from 'react'
22
import { useHistory, useLocation, useRouteMatch } from 'react-router-dom'
33

4-
import { UseSearchAndPagination } from './types'
4+
import { IndexType, IUseSearchAndPagination } from './types'
55
import { convert } from './utils'
66

7-
export function useSearchAndPagination(): UseSearchAndPagination {
7+
export const useSearchAndPagination: IUseSearchAndPagination = function (
8+
config
9+
) {
10+
const { indexType = IndexType.ZERO_BASED, pageSize = 15 } = config || {}
811
const routerHistory = useHistory()
912
const { url: routerUrl } = useRouteMatch()
1013
const { search: routeQuery } = useLocation()
@@ -53,8 +56,11 @@ export function useSearchAndPagination(): UseSearchAndPagination {
5356

5457
return {
5558
search: params.get('search') || '',
56-
page: convert(params.get('page'), 0),
57-
size: convert(params.get('size'), 15),
59+
page: convert(
60+
params.get('page'),
61+
indexType === IndexType.ZERO_BASED ? 0 : 1
62+
),
63+
size: convert(params.get('size'), pageSize),
5864
actions: {
5965
search,
6066
clear,

src/types.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
export enum IndexType {
2+
ZERO_BASED = 0,
3+
ONE_BASED = 1,
4+
}
5+
6+
export type Config = {
7+
indexType?: IndexType
8+
pageSize?: number
9+
}
10+
111
export type Actions = {
212
search: (query: string) => void
313
clear: () => void
@@ -10,3 +20,7 @@ export type UseSearchAndPagination = {
1020
size: number
1121
actions: Actions
1222
}
23+
24+
export interface IUseSearchAndPagination {
25+
(config?: Config): UseSearchAndPagination
26+
}

0 commit comments

Comments
 (0)