-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix TabbedForm and TabbedShowLayout with react-router v7
- Loading branch information
Showing
5 changed files
with
44 additions
and
16 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,24 @@ | ||
import { useLocation, useParams } from 'react-router-dom'; | ||
|
||
/** | ||
* Utility hook to get the base path of a splat path. | ||
* Compatible both with react-router v6 and v7. | ||
* | ||
* Example: | ||
* If a splat path is defined as `/posts/:id/show/*`, | ||
* and the current location is `/posts/12/show/3`, | ||
* this hook will return `/posts/12/show`. | ||
* | ||
* Solution inspired by | ||
* https://github.com/remix-run/react-router/issues/11052#issuecomment-1828470203 | ||
*/ | ||
export const useSplatPathBase = () => { | ||
const location = useLocation(); | ||
const params = useParams(); | ||
const splatPathRelativePart = params['*']; | ||
const splatPathBase = location.pathname.replace( | ||
new RegExp(`/${splatPathRelativePart}$`), | ||
'' | ||
); | ||
return splatPathBase; | ||
}; |
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