forked from redwoodjs/redwood
-
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.
chore(router): Move useMatch to its own file (redwoodjs#9770)
- Loading branch information
Showing
6 changed files
with
169 additions
and
141 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import React from 'react' | ||
|
||
import { render } from '@testing-library/react' | ||
|
||
import { Link } from '../links' | ||
import { LocationProvider } from '../location' | ||
import { useMatch } from '../useMatch' | ||
import { flattenSearchParams } from '../util' | ||
|
||
function createDummyLocation(pathname: string, search = '') { | ||
return { | ||
pathname, | ||
hash: '', | ||
host: '', | ||
hostname: '', | ||
href: '', | ||
ancestorOrigins: null, | ||
assign: () => null, | ||
reload: () => null, | ||
replace: () => null, | ||
origin: '', | ||
port: '', | ||
protocol: '', | ||
search, | ||
} | ||
} | ||
|
||
describe('useMatch', () => { | ||
const MyLink = ({ | ||
to, | ||
...rest | ||
}: React.ComponentPropsWithoutRef<typeof Link>) => { | ||
const [pathname, queryString] = to.split('?') | ||
const matchInfo = useMatch(pathname, { | ||
searchParams: flattenSearchParams(queryString), | ||
}) | ||
|
||
return ( | ||
<Link | ||
to={to} | ||
style={{ color: matchInfo.match ? 'green' : 'red' }} | ||
{...rest} | ||
/> | ||
) | ||
} | ||
|
||
it('returns a match on the same pathname', () => { | ||
const mockLocation = createDummyLocation('/dunder-mifflin') | ||
|
||
const { getByText } = render( | ||
<LocationProvider location={mockLocation}> | ||
<MyLink to="/dunder-mifflin">Dunder Mifflin</MyLink> | ||
</LocationProvider> | ||
) | ||
|
||
expect(getByText(/Dunder Mifflin/)).toHaveStyle('color: green') | ||
}) | ||
|
||
it('returns a match on the same pathname with search parameters', () => { | ||
const mockLocation = createDummyLocation( | ||
'/search-params', | ||
'?page=1&tab=main' | ||
) | ||
|
||
const { getByText } = render( | ||
<LocationProvider location={mockLocation}> | ||
<MyLink to={`/search-params?tab=main&page=1`}>Dunder Mifflin</MyLink> | ||
</LocationProvider> | ||
) | ||
|
||
expect(getByText(/Dunder Mifflin/)).toHaveStyle('color: green') | ||
}) | ||
|
||
it('does NOT receive active class on different path', () => { | ||
const mockLocation = createDummyLocation('/staples') | ||
|
||
const { getByText } = render( | ||
<LocationProvider location={mockLocation}> | ||
<MyLink to="/dunder-mifflin">Dunder Mifflin</MyLink> | ||
</LocationProvider> | ||
) | ||
|
||
expect(getByText(/Dunder Mifflin/)).toHaveStyle('color: red') | ||
}) | ||
|
||
it('does NOT receive active class on the same pathname with different parameters', () => { | ||
const mockLocation = createDummyLocation( | ||
'/search-params', | ||
'?tab=main&page=1' | ||
) | ||
|
||
const { getByText } = render( | ||
<LocationProvider location={mockLocation}> | ||
<MyLink to={`/search-params?page=2&tab=main`}>Dunder Mifflin</MyLink> | ||
</LocationProvider> | ||
) | ||
|
||
expect(getByText(/Dunder Mifflin/)).toHaveStyle('color: red') | ||
}) | ||
}) |
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,60 @@ | ||
import { useLocation } from './location' | ||
import { matchPath } from './util' | ||
import type { FlattenSearchParams } from './util' | ||
|
||
type UseMatchOptions = { | ||
searchParams?: FlattenSearchParams | ||
matchSubPaths?: boolean | ||
} | ||
|
||
/** | ||
* Returns an object of { match: boolean; params: Record<string, unknown>; } | ||
* if the path matches the current location match will be true. | ||
* Params will be an object of the matched params, if there are any. | ||
* | ||
* Provide searchParams options to match the current location.search | ||
* | ||
* This is useful for components that need to know "active" state, e.g. | ||
* <NavLink>. | ||
* | ||
* Examples: | ||
* | ||
* Match search params key existence | ||
* const match = useMatch('/about', { searchParams: ['category', 'page'] }) | ||
* | ||
* Match search params key and value | ||
* const match = useMatch('/items', { searchParams: [{page: 2}, {category: 'book'}] }) | ||
* | ||
* Mix match | ||
* const match = useMatch('/list', { searchParams: [{page: 2}, 'gtm'] }) | ||
* | ||
* Match sub paths | ||
* const match = useMatch('/product', { matchSubPaths: true }) | ||
*/ | ||
export const useMatch = (pathname: string, options?: UseMatchOptions) => { | ||
const location = useLocation() | ||
if (!location) { | ||
return { match: false } | ||
} | ||
|
||
if (options?.searchParams) { | ||
const locationParams = new URLSearchParams(location.search) | ||
const hasUnmatched = options.searchParams.some((param) => { | ||
if (typeof param === 'string') { | ||
return !locationParams.has(param) | ||
} else { | ||
return Object.keys(param).some( | ||
(key) => param[key] != locationParams.get(key) | ||
) | ||
} | ||
}) | ||
|
||
if (hasUnmatched) { | ||
return { match: false } | ||
} | ||
} | ||
|
||
return matchPath(pathname, location.pathname, { | ||
matchSubPaths: options?.matchSubPaths, | ||
}) | ||
} |
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