-
Notifications
You must be signed in to change notification settings - Fork 13
usePersistentReactDataTableState added #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
neotob
merged 6 commits into
neolution-ch:main
from
neotob:feature/usePersistentReactDataTableState
Jan 20, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
343d1ad
usePersistentReactDataTableState added
neotob f9d5f19
Merge branch 'main' into feature/usePersistentReactDataTableState
neotob 18c0c9a
update
neotob ded750d
fix
neotob 8dca9df
update
neotob e65f728
update changelog
neotob File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
126 changes: 126 additions & 0 deletions
126
src/lib/useReactDataTableState/usePersistentReactDataTableState.ts
This file contains hidden or 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,126 @@ | ||
| import { getLocalStorageItem, setLocalStorageItem } from "@neolution-ch/javascript-utils"; | ||
| import { OptionalNullable } from "../types/NullableTypes"; | ||
| import { useReactDataTableStateProps } from "./useReactDataTableStateProps"; | ||
| import { useReactDataTableStateResult } from "./useReactDataTableStateResult"; | ||
| import { useReactDataTableState } from "./useReactDataTableState"; | ||
| import { FilterModel } from "../types/TableState"; | ||
| import { useCallback } from "react"; | ||
|
|
||
| /** | ||
| * A custom hook that will initialize all the state needed for the react data table and will persist it to local storage | ||
| * @param props The properties to configure the initial state | ||
| * @returns the state and the setters | ||
| */ | ||
| const usePersistentReactDataTableState = <TData, TFilter extends FilterModel = Record<string, never>>( | ||
| props: OptionalNullable<useReactDataTableStateProps<TData, TFilter>> & { localStorageKey: string }, | ||
| ): useReactDataTableStateResult<TData, TFilter> => { | ||
| const { | ||
| initialColumnFilters, | ||
| initialSorting, | ||
| initialPagination, | ||
| initialRowSelection, | ||
| initialExpanded, | ||
| initialColumnPinning, | ||
| initialAfterSearchFilter, | ||
| localStorageKey, | ||
| } = props as useReactDataTableStateProps<TData, TFilter> & { localStorageKey: string }; | ||
|
|
||
| const { | ||
| pagination, | ||
| setPagination, | ||
| columnFilters, | ||
| columnPinning, | ||
| expanded, | ||
| rowSelection, | ||
| sorting, | ||
| setColumnFilters, | ||
| afterSearchFilter, | ||
| setAfterSearchFilter, | ||
| setColumnPinning, | ||
| setExpanded, | ||
| setRowSelection, | ||
| setSorting, | ||
| } = useReactDataTableState<TData, TFilter>({ | ||
| initialColumnPinning: | ||
| getLocalStorageItem<useReactDataTableStateProps<TData, TFilter>["initialColumnPinning"]>(`${localStorageKey}_columnPinning`) ?? | ||
| initialColumnPinning, | ||
| initialExpanded: | ||
| getLocalStorageItem<useReactDataTableStateProps<TData, TFilter>["initialExpanded"]>(`${localStorageKey}_expanded`) ?? initialExpanded, | ||
| initialPagination: | ||
| getLocalStorageItem<useReactDataTableStateProps<TData, TFilter>["initialPagination"]>(`${localStorageKey}_pagination`) ?? | ||
| initialPagination, | ||
| initialRowSelection: | ||
| getLocalStorageItem<useReactDataTableStateProps<TData, TFilter>["initialRowSelection"]>(`${localStorageKey}_rowSelection`) ?? | ||
| initialRowSelection, | ||
| initialSorting: | ||
| getLocalStorageItem<useReactDataTableStateProps<TData, TFilter>["initialSorting"]>(`${localStorageKey}_sorting`) ?? initialSorting, | ||
| initialColumnFilters: | ||
| getLocalStorageItem<useReactDataTableStateProps<TData, TFilter>["initialColumnFilters"]>(`${localStorageKey}_columnFilters`) ?? | ||
| initialColumnFilters, | ||
| initialAfterSearchFilter: | ||
| getLocalStorageItem<useReactDataTableStateProps<TData, TFilter>["initialAfterSearchFilter"]>( | ||
| `${localStorageKey}_afterSearchFilter`, | ||
| ) ?? initialAfterSearchFilter, | ||
| } as useReactDataTableStateProps<TData, TFilter> as OptionalNullable<useReactDataTableStateProps<TData, TFilter>>); | ||
|
|
||
| return { | ||
| pagination, | ||
| setPagination: useCallback( | ||
| (newPagination) => { | ||
| setPagination(newPagination); | ||
| setLocalStorageItem(`${props.localStorageKey}_pagination`, newPagination); | ||
| }, | ||
| [props.localStorageKey, setPagination], | ||
| ), | ||
| columnFilters, | ||
| setColumnFilters: useCallback( | ||
| (newColumnFilters) => { | ||
| setColumnFilters(newColumnFilters); | ||
| setLocalStorageItem(`${props.localStorageKey}_columnFilters`, newColumnFilters); | ||
| }, | ||
| [props.localStorageKey, setColumnFilters], | ||
| ), | ||
| afterSearchFilter, | ||
| setAfterSearchFilter: useCallback( | ||
| (newAfterSearchFilter) => { | ||
| setAfterSearchFilter(newAfterSearchFilter); | ||
| setLocalStorageItem(`${props.localStorageKey}_afterSearchFilter`, newAfterSearchFilter); | ||
| }, | ||
| [props.localStorageKey, setAfterSearchFilter], | ||
| ), | ||
| columnPinning, | ||
| setColumnPinning: useCallback( | ||
| (newColumnPinning) => { | ||
| setColumnPinning(newColumnPinning); | ||
| setLocalStorageItem(`${props.localStorageKey}_columnPinning`, newColumnPinning); | ||
| }, | ||
| [props.localStorageKey, setColumnPinning], | ||
| ), | ||
| expanded, | ||
| setExpanded: useCallback( | ||
| (newExpanded) => { | ||
| setExpanded(newExpanded); | ||
| setLocalStorageItem(`${props.localStorageKey}_expanded`, newExpanded); | ||
| }, | ||
| [props.localStorageKey, setExpanded], | ||
| ), | ||
| rowSelection, | ||
| setRowSelection: useCallback( | ||
| (newRowSelection) => { | ||
| setRowSelection(newRowSelection); | ||
| setLocalStorageItem(`${props.localStorageKey}_rowSelection`, newRowSelection); | ||
| }, | ||
| [props.localStorageKey, setRowSelection], | ||
| ), | ||
| sorting, | ||
| setSorting: useCallback( | ||
| (newSorting) => { | ||
| setSorting(newSorting); | ||
| setLocalStorageItem(`${props.localStorageKey}_sorting`, newSorting); | ||
| }, | ||
| [props.localStorageKey, setSorting], | ||
| ), | ||
| }; | ||
| }; | ||
|
|
||
| export { usePersistentReactDataTableState }; |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.