-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
5 changed files
with
100 additions
and
115 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* @module useSearchFilters | ||
*/ | ||
|
||
import { useCallback, useRef } from 'react'; | ||
import { Query as Filter } from '/js/utils/request'; | ||
|
||
export type { Filter }; | ||
|
||
export type SearchFilters<T extends Filter[]> = { | ||
[K in keyof T]: T[K] extends Filter ? T[K] | false : T[K]; | ||
}; | ||
|
||
/** | ||
* @function useSearchFilters | ||
* @description [hook] 可缓存查询过滤条件 | ||
* @param initialValue 初始化查询过滤条件 | ||
*/ | ||
export default function useSearchFilters<T extends Filter[]>( | ||
initialValue: SearchFilters<T> | ||
): [serialize: (value?: Partial<SearchFilters<T>>) => Filter, raw: () => SearchFilters<T>] { | ||
const searchFiltersRef = useRef(initialValue); | ||
|
||
const serialize = useCallback((value?: Partial<SearchFilters<T>>) => { | ||
if (value && searchFiltersRef.current !== value) { | ||
searchFiltersRef.current = searchFiltersRef.current.map((search, index) => { | ||
return value[index] ?? search; | ||
}) as SearchFilters<T>; | ||
} | ||
|
||
return searchFiltersRef.current.reduce<Filter>((values, value) => { | ||
return value ? { ...values, ...value } : values; | ||
}, {}); | ||
}, []); | ||
|
||
const raw = useCallback(() => { | ||
return searchFiltersRef.current; | ||
}, []); | ||
|
||
return [serialize, raw]; | ||
} |
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