|
1 | | -import { useState, useEffect } from 'react'; |
| 1 | +import { useEffect, useState } from 'react'; |
2 | 2 | import { useLocation, useNavigate } from 'react-router-dom'; |
3 | 3 |
|
4 | 4 | // Assisted by watsonx Code Assistant |
5 | 5 | // Code generated by WCA@IBM in this programming language is not approved for use in IBM product development. |
6 | 6 |
|
7 | | -export const useTabHook = (validTabIndicies=[], defaultTab) => { |
| 7 | +export const useTabHook = (validTabIndicies=[], defaultTab='summary', skipHash=false) => { |
8 | 8 |
|
9 | 9 | const location = useLocation(); |
10 | 10 | const navigate = useNavigate(); |
11 | | - const currentHash = location.hash.slice(1); |
12 | | - const activeTab = validTabIndicies.includes(currentHash) ? currentHash : defaultTab; |
| 11 | + const [activeTab, setActiveTab] = useState(defaultTab); |
| 12 | + |
| 13 | + useEffect(() => { |
| 14 | + if (!skipHash) { |
| 15 | + const currentHash = location.hash.slice(1); |
| 16 | + setActiveTab(validTabIndicies.includes(currentHash) ? currentHash : defaultTab); |
| 17 | + } |
| 18 | + }, [defaultTab, location.hash, skipHash, validTabIndicies]); |
13 | 19 |
|
14 | 20 | useEffect(() => { |
15 | 21 | // catch a bad tab index and use the default |
16 | | - if (!validTabIndicies.includes(location.hash.slice(1))) {navigate(`#${defaultTab}`);} |
17 | | - }, [defaultTab, location.hash, navigate, validTabIndicies]); |
| 22 | + if (!skipHash && !validTabIndicies.includes(location.hash.slice(1))) {navigate(`#${defaultTab}`);} |
| 23 | + }, [defaultTab, location.hash, navigate, skipHash, validTabIndicies]); |
18 | 24 |
|
19 | 25 | const onTabSelect = (_, tabIndex) => { |
20 | | - navigate(`#${tabIndex}`); |
| 26 | + if (!skipHash) {navigate(`#${tabIndex}`);}; |
| 27 | + setActiveTab(tabIndex); |
21 | 28 | }; |
22 | 29 |
|
23 | 30 | return {activeTab, onTabSelect}; |
|
0 commit comments