Skip to content

Commit

Permalink
[TypeScript] Use Generics with Tabs TabType Definition (#1329)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljaltamirano authored Dec 28, 2021
1 parent bfaa56c commit ad57eee
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/shared-components/tabs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,26 @@ import React, { useState } from 'react';

import Style from './style';

interface TabType {
export interface TabType {
id: number;
text: string;
}

// Hardcoding TabType below to show the actual type in the docs
export interface TabsProps {
export interface TabsProps<TabGeneric extends TabType> {
initialActiveTabId?: number;
onClick?: (tab: TabType) => unknown;
tabItems: Array<{
id: number;
text: string;
}>;
onClick?: (tab: TabGeneric) => unknown;
tabItems: TabGeneric[];
}

export const Tabs: React.FC<TabsProps> = ({
export const Tabs = <TabGeneric extends TabType>({
initialActiveTabId = 1,
onClick = () => undefined,
tabItems,
}: TabsProps) => {
}: TabsProps<TabGeneric>) => {
const [activeTabId, setActiveTabId] = useState(initialActiveTabId);

const onTabClick = (tab: TabType) => {
const onTabClick = (tab: TabGeneric) => {
setActiveTabId(tab.id);
onClick(tab);
};
Expand Down

0 comments on commit ad57eee

Please sign in to comment.