generated from habx/lib-ts-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jean Dessane
committed
May 11, 2021
1 parent
260651e
commit fb5bd65
Showing
12 changed files
with
148 additions
and
252 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 @@ | ||
export const isClientSide: boolean = typeof document === 'object' |
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,45 @@ | ||
import { isEmpty, mapValues, pickBy, isNil } from 'lodash' | ||
|
||
export const parseUrlString = (filter: any): any => { | ||
if (filter instanceof Date) { | ||
return filter.toISOString() | ||
} | ||
if (Array.isArray(filter)) { | ||
if (filter.length === 0) { | ||
return undefined | ||
} | ||
return filter.map(parseUrlString) | ||
} | ||
if (typeof filter === 'object') { | ||
if (isEmpty(filter)) { | ||
return undefined | ||
} | ||
if (filter['0']) { | ||
return Object.values(filter) | ||
} | ||
return parseQueryString(filter) | ||
} | ||
if (filter === 'true') { | ||
return true | ||
} | ||
if (filter === 'false') { | ||
return false | ||
} | ||
try { | ||
const numberInput = Number(filter.trim()) | ||
if (!isNaN(numberInput)) { | ||
return Math.round(numberInput) | ||
} | ||
} catch (e) { | ||
// | ||
} | ||
return filter | ||
} | ||
|
||
export const parseQueryString = <FiltersType>(filters: any) => { | ||
const state = pickBy(mapValues(filters, parseUrlString), (el) => !isNil(el)) | ||
if (isEmpty(state)) { | ||
return undefined | ||
} | ||
return state | ||
} |
This file was deleted.
Oops, something went wrong.
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,4 +1,2 @@ | ||
export { default as useQueryState } from './useQueryState' | ||
export { default as cleanQueryState } from './cleanQueryState' | ||
export { default as QueryStateProvider } from './QueryStateProvider' | ||
export { UseQueryStateOptions, UrlOptions } from './types' | ||
export { useQueryState } from './useQueryState' | ||
export { UseQueryStateOptions } from './types' |
This file was deleted.
Oops, something went wrong.
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,15 +1,21 @@ | ||
export type UrlOptions = { | ||
shouldUseGetRoot?: boolean | ||
persist?: boolean | ||
persistInitial?: boolean | ||
} | ||
|
||
export type UseQueryStateOptions<FiltersType> = { | ||
export type UseQueryStateOptions<FiltersType extends object> = { | ||
/** | ||
* Default value used in state | ||
* @default {} | ||
*/ | ||
defaultValue?: FiltersType | ||
customClean?: (filter: any, key?: string | number) => any | void | ||
url?: UrlOptions | ||
value?: FiltersType | ||
onChange?: (groupName: string, value: FiltersType) => void | ||
persistValue?: (groupName: string, value: FiltersType) => void | ||
groupName?: string | ||
/** | ||
* Persist state in query url | ||
* @default true | ||
*/ | ||
persist?: boolean | ||
/** | ||
* If provided, state is persisted in session storage with the key provided | ||
*/ | ||
cacheKey?: string | ||
/** | ||
* Overwrites default parse url function | ||
* @param url | ||
*/ | ||
parseSearch?: (search: string) => object | undefined | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.