import * as React from 'react'; import { QueryParamConfigMap, DecodedValueMap } from 'serialize-query-params'; import useQueryParams from './useQueryParams'; import { SetQuery } from './types'; type Omit = Pick>; type Diff = Omit; export interface InjectedQueryProps { query: DecodedValueMap; setQuery: SetQuery; } /** * HOC to provide query parameters via props `query` and `setQuery` * NOTE: I couldn't get type to automatically infer generic when * using the format withQueryParams(config)(component), so I switched * to withQueryParams(config, component). * See: https://github.com/microsoft/TypeScript/issues/30134 */ export function withQueryParams< QPCMap extends QueryParamConfigMap, P extends InjectedQueryProps >(paramConfigMap: QPCMap, WrappedComponent: React.ComponentType

) { // return a FC that takes props excluding query and setQuery const Component: React.FC>> = (props) => { const [query, setQuery] = useQueryParams(paramConfigMap); // see https://github.com/microsoft/TypeScript/issues/28938#issuecomment-450636046 for why `...props as P` return ( ); }; Component.displayName = `withQueryParams(${ WrappedComponent.displayName || WrappedComponent.name || 'Component' })`; return Component; } export default withQueryParams; /** * HOC to provide query parameters via props mapToProps (similar to * react-redux connect style mapStateToProps) * NOTE: I couldn't get type to automatically infer generic when * using the format withQueryParams(config)(component), so I switched * to withQueryParams(config, component). * See: https://github.com/microsoft/TypeScript/issues/30134 */ export function withQueryParamsMapped< QPCMap extends QueryParamConfigMap, MappedProps extends object, P extends MappedProps >( paramConfigMap: QPCMap, mapToProps: ( query: DecodedValueMap, setQuery: SetQuery, props: Diff ) => MappedProps, WrappedComponent: React.ComponentType

) { // return a FC that takes props excluding query and setQuery const Component: React.FC> = (props) => { const [query, setQuery] = useQueryParams(paramConfigMap); const propsToAdd = mapToProps(query, setQuery, props); // see https://github.com/microsoft/TypeScript/issues/28938#issuecomment-450636046 for why `...props as P` return ; }; Component.displayName = `withQueryParams(${ WrappedComponent.displayName || WrappedComponent.name || 'Component' })`; return Component; }