@@ -24,9 +24,9 @@ type MatchRouterTabResult = {
24
24
match : DataRouteMatch ;
25
25
} ;
26
26
27
- export const matchRouterTab = (
27
+ export const matchRouterTab = < UiState extends ValidUiState = ValidUiState > (
28
28
matches : DataRouteMatch [ ] ,
29
- config : TabDefinition [ ] ,
29
+ config : TabDefinition < UiState > [ ] ,
30
30
) : MatchRouterTabResult | undefined => {
31
31
for ( let i = matches . length - 1 ; i > - 1 ; i -- ) {
32
32
const match = matches [ i ] ;
@@ -48,8 +48,9 @@ export const useRouterTabs = <
48
48
config : TabDefinition < UiState > [ ] ;
49
49
onPathsChange ?: PathsChangeCallback ;
50
50
paths : RouterTabPath [ ] ;
51
+ undefinedPath : string ;
51
52
} ) => {
52
- const { onPathsChange, paths, config, router } = options ;
53
+ const { onPathsChange, paths, config, router, undefinedPath } = options ;
53
54
54
55
const isOpenFor = useCallback (
55
56
( match : DataRouteMatch ) => ( path : string ) => {
@@ -85,7 +86,7 @@ export const useRouterTabs = <
85
86
const getNextPaths = ( prevPaths : RouterTabPath [ ] ) => {
86
87
const tab = prevPaths . find ( isOpenFor ( matchResult . match ) ) ;
87
88
88
- const path = getUrl ( location ) ;
89
+ const path = getPathFromLocation ( location ) ;
89
90
90
91
if ( tab ) {
91
92
// update the tab path
@@ -126,18 +127,25 @@ export const useRouterTabs = <
126
127
. map ( toUiState )
127
128
. filter ( ( tab ) : tab is UiState => Boolean ( tab ) ) ;
128
129
129
- const activeTab = toUiState ( getUrl ( router . state . location ) ) as
130
+ const setActivePath = ( path : string | undefined ) => {
131
+ setTimeout ( ( ) => {
132
+ router . navigate ( path || undefinedPath ) ;
133
+ } ) ;
134
+ } ;
135
+
136
+ const activeTab = toUiState ( getPathFromLocation ( router . state . location ) ) as
130
137
| UiState
131
138
| undefined ;
132
139
133
140
return {
134
141
tabs,
135
142
activeTab,
143
+ setActivePath,
136
144
} ;
137
145
} ;
138
146
139
- const getUrl = ( location : Location ) => {
140
- const { pathname, search } = location ;
147
+ const getPathFromLocation = ( location : Location ) => {
148
+ const { pathname, search, hash } = location ;
141
149
142
- return normalizePathname ( pathname ) + search ;
150
+ return normalizePathname ( pathname ) + search + hash ;
143
151
} ;
0 commit comments