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oCAKY,KAAK;;;;KAgC/C,CAAC","sources":["source/source/index.tsx","source/index.tsx"],"sourcesContent":[null,"import { Constructor, parseURLData } from 'web-utility';\nimport { Location } from 'history';\nimport { Component, ComponentClass, ErrorInfo, FC } from 'react';\nimport { useLocation, useParams } from 'react-router-dom';\n\n// Types come from https://cdn.jsdelivr.net/npm/@types/react-router/index.d.ts\n\nexport interface StaticContext {\n statusCode?: number | undefined;\n}\n\nexport interface match<\n Params extends Partial<Record<keyof Params, string>> = {}\n> {\n url: string;\n path: string;\n params: Params;\n}\n\nexport interface RouteComponentProps<\n Params extends Partial<Record<keyof Params, string>> = {},\n Context extends StaticContext = StaticContext,\n Query extends Record<string, any> = {}\n> {\n location?: Location;\n match?: match<Params>;\n query?: Query;\n staticContext?: Context;\n}\n\nconst HooksWrapper: FC<{ ClassComponent: ComponentClass }> = ({\n ClassComponent\n}) => {\n const location = useLocation(),\n params = useParams();\n\n const { pathname = '/', search = '', hash = '' } = location;\n\n const path = pathname + search + hash;\n\n const match = {\n url: globalThis.location.origin + path,\n path,\n params\n },\n query = parseURLData(search);\n\n return <ClassComponent {...{ location, match, query }} />;\n};\n\n/**\n * @see https://v5.reactrouter.com/web/api/withRouter\n *\n * @example\n * ```tsx\n * import { Component } from 'react';\n * import { RouteComponentProps, withRouter } from 'react-router-class-tools';\n *\n * @withRouter\n * export class PageWithRouterDecorator extends Component<RouteComponentProps> {\n * render() {\n * const { location, match, query } = this.props;\n *\n * return (\n * <pre>{JSON.stringify({ location, match, query }, null, 4)}</pre>\n * );\n * }\n * }\n * ```\n *\n * @example\n * ```tsx\n * import { Component } from 'react';\n * import { RouteComponentProps, withRouter } from 'react-router-class-tools';\n *\n * export default withRouter(class PageWithRouterFunction extends Component<RouteComponentProps> {\n * render() {\n * const { location, match, query } = this.props;\n *\n * return (\n * <pre>{JSON.stringify({ location, match, query }, null, 4)}</pre>\n * );\n * }\n * }\n * ```\n */\nexport const withRouter = <\n P extends RouteComponentProps,\n C extends ComponentClass<P>\n>(\n Class: C,\n context?: ClassDecoratorContext<C>\n) =>\n class ComponentWithRouter extends (Class as Constructor<Component<P>> & C) {\n static WrappedComponent = Class;\n static displayName = `withRouter(${Class.displayName || Class.name})`;\n\n static getDerivedStateFromProps(\n nextProps: Readonly<P>,\n prevState: Readonly<{}>\n ) {\n return {};\n }\n\n static getDerivedStateFromError(error: Error) {}\n\n state: Readonly<{}> = {};\n\n componentDidMount() {}\n\n getSnapshotBeforeUpdate(\n prevProps: Readonly<P>,\n prevState: Readonly<{}>\n ) {}\n\n shouldComponentUpdate(\n nextProps: Readonly<P>,\n nextState: Readonly<{}>,\n nextContext: any\n ) {\n return true;\n }\n\n componentDidUpdate(\n prevProps: Readonly<P>,\n prevState: Readonly<{}>,\n snapshot?: any\n ) {}\n\n componentDidCatch(error: Error, errorInfo: ErrorInfo) {}\n\n componentWillUnmount() {}\n\n render() {\n return <HooksWrapper ClassComponent={Class} />;\n }\n };\n"],"names":[],"version":3,"file":"index.d.ts.map"}