mirror of
https://github.com/hanzonet/explore.git
synced 2026-10-30 18:16:57 +00:00
* first version of the pack * single entry point * add custom components to the package * move form components to toolkit * add more utils and hooks to the package * minor fixes * add CI workflow * update readme and package.json * fix ts * fix build of env-validator * fix docker build * fix icons size * test fixes * more test fixes
64 lines
1.8 KiB
TypeScript
64 lines
1.8 KiB
TypeScript
import { pickBy } from 'es-toolkit';
|
|
import { useRouter } from 'next/router';
|
|
import React from 'react';
|
|
|
|
import type { Props as AdaptiveTabsProps } from '../AdaptiveTabs/AdaptiveTabs';
|
|
import AdaptiveTabs from '../AdaptiveTabs/AdaptiveTabs';
|
|
import { getTabValue } from '../AdaptiveTabs/utils';
|
|
import useActiveTabFromQuery from './useActiveTabFromQuery';
|
|
|
|
interface Props extends AdaptiveTabsProps {}
|
|
|
|
const RoutedTabs = (props: Props) => {
|
|
const { tabs, onValueChange, ...rest } = props;
|
|
|
|
const router = useRouter();
|
|
const activeTab = useActiveTabFromQuery(props.tabs);
|
|
const tabsRef = React.useRef<HTMLDivElement>(null);
|
|
|
|
const handleValueChange = React.useCallback(({ value }: { value: string }) => {
|
|
const nextTab = tabs.find((tab) => getTabValue(tab) === value);
|
|
|
|
if (!nextTab) {
|
|
return;
|
|
}
|
|
|
|
const queryForPathname = pickBy(router.query, (_, key) => router.pathname.includes(`[${ key }]`));
|
|
router.push(
|
|
{ pathname: router.pathname, query: { ...queryForPathname, tab: value } },
|
|
undefined,
|
|
{ shallow: true },
|
|
);
|
|
|
|
onValueChange?.({ value });
|
|
}, [ tabs, router, onValueChange ]);
|
|
|
|
React.useEffect(() => {
|
|
if (router.query.scroll_to_tabs) {
|
|
tabsRef?.current?.scrollIntoView(true);
|
|
delete router.query.scroll_to_tabs;
|
|
router.push(
|
|
{
|
|
pathname: router.pathname,
|
|
query: router.query,
|
|
},
|
|
undefined,
|
|
{ shallow: true },
|
|
);
|
|
}
|
|
// replicate componentDidMount
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, []);
|
|
|
|
return (
|
|
<AdaptiveTabs
|
|
{ ...rest }
|
|
tabs={ tabs }
|
|
onValueChange={ handleValueChange }
|
|
defaultValue={ activeTab ? getTabValue(activeTab) : getTabValue(tabs[ 0 ]) }
|
|
/>
|
|
);
|
|
};
|
|
|
|
export default React.memo(RoutedTabs);
|