66 lines
1.2 KiB
TypeScript
Raw Normal View History

2021-12-27 23:55:38 +05:30
import { Tabs, TabsProps } from 'antd';
import history from 'lib/history';
2022-03-14 20:12:42 +05:30
import React from 'react';
2021-12-27 23:55:38 +05:30
const { TabPane } = Tabs;
function RouteTab({
2021-12-27 23:55:38 +05:30
routes,
activeKey,
onChangeHandler,
...rest
}: RouteTabProps & TabsProps): JSX.Element {
const onChange = (activeRoute: string): void => {
if (onChangeHandler) {
onChangeHandler();
}
2021-12-27 23:55:38 +05:30
const selectedRoute = routes.find((e) => e.name === activeRoute);
if (selectedRoute) {
history.push(selectedRoute.route);
}
};
return (
<Tabs
onChange={onChange}
destroyInactiveTabPane
activeKey={activeKey}
animated
// eslint-disable-next-line react/jsx-props-no-spreading
2021-12-27 23:55:38 +05:30
{...rest}
>
{routes.map(
({ Component, name, route }): JSX.Element => (
<TabPane
tabKey={route}
animated
destroyInactiveTabPane
tab={name}
key={name}
>
2021-12-27 23:55:38 +05:30
<Component />
</TabPane>
),
)}
</Tabs>
);
}
2021-12-27 23:55:38 +05:30
interface RouteTabProps {
routes: {
name: string;
route: string;
Component: () => JSX.Element;
}[];
activeKey: TabsProps['activeKey'];
onChangeHandler?: VoidFunction;
}
RouteTab.defaultProps = {
onChangeHandler: undefined,
};
2021-12-27 23:55:38 +05:30
export default RouteTab;