-
Notifications
You must be signed in to change notification settings - Fork 686
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into jimbo/minicart-height
- Loading branch information
Showing
15 changed files
with
347 additions
and
264 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,95 @@ | ||
import { useMemo, useState } from 'react'; | ||
import { useCallback, useMemo, useState } from 'react'; | ||
import { getSearchParam } from './useSearchParam'; | ||
|
||
export const usePagination = () => { | ||
const [currentPage, setCurrentPage] = useState(0); | ||
const [totalPages, setTotalPages] = useState(null); | ||
/** | ||
* Sets a query parameter in history. Attempt to use React Router if provided | ||
* otherwise fallback to builtins. | ||
*/ | ||
const setQueryParam = ({ location, history, parameter, value }) => { | ||
const { search } = location; | ||
const queryParams = new URLSearchParams(search); | ||
queryParams.set(parameter, value); | ||
|
||
if (history.push) { | ||
history.push({ search: queryParams.toString() }); | ||
} else { | ||
// Use the native pushState. See https://developer.mozilla.org/en-US/docs/Web/API/History_API#The_pushState()_method | ||
history.pushState({ search: queryParams.toString() }, ''); | ||
} | ||
}; | ||
|
||
const defaultInitialPage = 1; | ||
|
||
/** | ||
* `usePagination` provides a pagination state with `currentPage` and | ||
* `totalPages` as well as an API for interacting with the state. | ||
* | ||
* @param {Object} location the location object, like window.location, or from react router | ||
* @param {Object} history the history object, like window.history, or from react router | ||
* @param {String} namespace the namespace to apply to the pagination query | ||
* @param {String} parameter the name of the query parameter to use for page | ||
* @param {Number} initialPage the initial current page value | ||
* @param {Number} intialTotalPages the total pages expected to be usable by this hook | ||
* | ||
* TODO update with defaults | ||
* | ||
* @returns {[PaginationState, PaginationApi]} | ||
*/ | ||
export const usePagination = ({ | ||
location = window.location, | ||
history = window.history, | ||
namespace = '', | ||
parameter = 'page', | ||
initialPage, | ||
initialTotalPages = 1 | ||
} = {}) => { | ||
const searchParam = namespace ? `${namespace}_${parameter}` : parameter; | ||
if (!initialPage) { | ||
// We need to synchronously fetch the initial page value from the query | ||
// param otherwise we would initialize this value twice. | ||
initialPage = parseInt( | ||
getSearchParam(searchParam, location) || defaultInitialPage | ||
); | ||
} | ||
|
||
const [currentPage, setCurrentPage] = useState(initialPage); | ||
const [totalPages, setTotalPages] = useState(initialTotalPages); | ||
|
||
const setPage = useCallback( | ||
page => { | ||
// Update the query parameter. | ||
setQueryParam({ | ||
location, | ||
history, | ||
parameter: searchParam, | ||
value: page | ||
}); | ||
|
||
// Update the state object. | ||
setCurrentPage(page); | ||
}, | ||
[history, location, searchParam] | ||
); | ||
|
||
/** | ||
* @typedef PaginationState | ||
* @property {Number} currentPage the current page | ||
* @property {Number} totalPages the total pages | ||
*/ | ||
const paginationState = { currentPage, totalPages }; | ||
const api = useMemo(() => ({ setCurrentPage, setTotalPages }), [ | ||
setCurrentPage, | ||
setTotalPages | ||
]); | ||
|
||
/** | ||
* @typedef PaginationApi | ||
* @property {Function} setCurrentPage | ||
* @property {Function} setTotalPages | ||
*/ | ||
const api = useMemo( | ||
() => ({ | ||
setCurrentPage: setPage, | ||
setTotalPages | ||
}), | ||
[setPage, setTotalPages] | ||
); | ||
|
||
return [paginationState, api]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...s/venia-concept/src/components/Pagination/__tests__/__snapshots__/pagination.spec.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`renders nothing when there is only 1 page 1`] = `null`; | ||
|
||
exports[`renders when there is more than 1 page 1`] = ` | ||
<div | ||
className="root" | ||
> | ||
<button | ||
onClick={[Function]} | ||
> | ||
<div /> | ||
1 | ||
</button> | ||
<button | ||
onClick={[Function]} | ||
> | ||
2 | ||
</button> | ||
<button | ||
onClick={[Function]} | ||
> | ||
3 | ||
</button> | ||
</div> | ||
`; |
Oops, something went wrong.