File tree Expand file tree Collapse file tree 5 files changed +38
-35
lines changed
react-router-dom/__tests__ Expand file tree Collapse file tree 5 files changed +38
-35
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ " react-router-dom " : patch
3
+ " @remix-run/router " : patch
4
+ ---
5
+
6
+ properly support index routes with a path in useResolvedPath
Original file line number Diff line number Diff line change @@ -1737,6 +1737,29 @@ function testDomRouter(
1737
1737
await new Promise ( ( r ) => setTimeout ( r , 0 ) ) ;
1738
1738
assertLocation ( testWindow , "/form" , "?index" ) ;
1739
1739
} ) ;
1740
+
1741
+ it ( "handles index routes with a path" , async ( ) => {
1742
+ let { container } = render (
1743
+ < TestDataRouter
1744
+ window = { getWindow ( "/foo/bar?a=1#hash" ) }
1745
+ hydrationData = { { } }
1746
+ >
1747
+ < Route path = "/" >
1748
+ < Route path = "foo" >
1749
+ < Route
1750
+ index = { true }
1751
+ path = "bar"
1752
+ element = { < NoActionComponent /> }
1753
+ />
1754
+ </ Route >
1755
+ </ Route >
1756
+ </ TestDataRouter >
1757
+ ) ;
1758
+
1759
+ expect ( container . querySelector ( "form" ) ?. getAttribute ( "action" ) ) . toBe (
1760
+ "/foo/bar?index&a=1#hash"
1761
+ ) ;
1762
+ } ) ;
1740
1763
} ) ;
1741
1764
1742
1765
describe ( "dynamic routes" , ( ) => {
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ import {
19
19
parsePath ,
20
20
resolveTo ,
21
21
warning ,
22
+ UNSAFE_getPathContributingMatches as getPathContributingMatches ,
22
23
} from "@remix-run/router" ;
23
24
24
25
import type {
@@ -147,36 +148,6 @@ export interface NavigateFunction {
147
148
( delta : number ) : void ;
148
149
}
149
150
150
- /**
151
- * When processing relative navigation we want to ignore ancestor routes that
152
- * do not contribute to the path, such that index/pathless layout routes don't
153
- * interfere.
154
- *
155
- * For example, when moving a route element into an index route and/or a
156
- * pathless layout route, relative link behavior contained within should stay
157
- * the same. Both of the following examples should link back to the root:
158
- *
159
- * <Route path="/">
160
- * <Route path="accounts" element={<Link to=".."}>
161
- * </Route>
162
- *
163
- * <Route path="/">
164
- * <Route path="accounts">
165
- * <Route element={<AccountsLayout />}> // <-- Does not contribute
166
- * <Route index element={<Link to=".."} /> // <-- Does not contribute
167
- * </Route
168
- * </Route>
169
- * </Route>
170
- */
171
- function getPathContributingMatches ( matches : RouteMatch [ ] ) {
172
- return matches . filter (
173
- ( match , index ) =>
174
- index === 0 ||
175
- ( ! match . route . index &&
176
- match . pathnameBase !== matches [ index - 1 ] . pathnameBase )
177
- ) ;
178
- }
179
-
180
151
/**
181
152
* Returns an imperative method for changing the location. Used by <Link>s, but
182
153
* may also be used by other elements to change the location.
Original file line number Diff line number Diff line change 1
- import { convertRoutesToDataRoutes } from "./utils" ;
1
+ import { convertRoutesToDataRoutes , getPathContributingMatches } from "./utils" ;
2
2
3
3
export type {
4
4
ActionFunction ,
@@ -79,4 +79,7 @@ export * from "./router";
79
79
///////////////////////////////////////////////////////////////////////////////
80
80
81
81
/** @internal */
82
- export { convertRoutesToDataRoutes as UNSAFE_convertRoutesToDataRoutes } ;
82
+ export {
83
+ convertRoutesToDataRoutes as UNSAFE_convertRoutesToDataRoutes ,
84
+ getPathContributingMatches as UNSAFE_getPathContributingMatches ,
85
+ } ;
Original file line number Diff line number Diff line change @@ -857,6 +857,8 @@ function getInvalidPathError(
857
857
}
858
858
859
859
/**
860
+ * @private
861
+ *
860
862
* When processing relative navigation we want to ignore ancestor routes that
861
863
* do not contribute to the path, such that index/pathless layout routes don't
862
864
* interfere.
@@ -882,9 +884,7 @@ export function getPathContributingMatches<
882
884
> ( matches : T [ ] ) {
883
885
return matches . filter (
884
886
( match , index ) =>
885
- index === 0 ||
886
- ( ! match . route . index &&
887
- match . pathnameBase !== matches [ index - 1 ] . pathnameBase )
887
+ index === 0 || ( match . route . path && match . route . path . length > 0 )
888
888
) ;
889
889
}
890
890
You can’t perform that action at this time.
0 commit comments