Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tab):generic id type added to TabItem #223

Merged
merged 3 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions src/tab/Tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import classNames from "classnames";
import TabHeaderItem from "./header/item/TabHeaderItem";
import List from "../list/List";

export type TabItem = {
id: string;
export type TabItem<ID extends string | number = string | number> = {
id: ID;
content: React.ReactNode;
icon?: React.ReactNode;
isDisabled?: boolean;
};

interface UncontrolledTabProps {
items: TabItem[];
interface UncontrolledTabProps<ID extends string | number = string | number> {
items: TabItem<ID>[];
children: React.ReactNode[];
testid?: string;
initialActiveTabIndex?: number;
Expand All @@ -26,16 +26,16 @@ interface UncontrolledTabProps {
// and initialActiveTabIndex should be undefined
type ControlledTabProps =
| {
activeTabIndex: number;
onTabChange: (index: number) => void;
initialActiveTabIndex?: number;
}
activeTabIndex: number;
onTabChange: (index: number) => void;
initialActiveTabIndex?: number;
}
| {
activeTabIndex?: number;
onTabChange?: (index: number) => void;
};
activeTabIndex?: number;
onTabChange?: (index: number) => void;
};

export type TabProps = ControlledTabProps & UncontrolledTabProps;
export type TabProps = ControlledTabProps & UncontrolledTabProps<string | number>;

function Tab({
testid,
Expand Down Expand Up @@ -70,9 +70,9 @@ function Tab({
<div className={"tab__body"} data-testid={`${testid}.body`}>
{
children[
activeTabIndexFromProps === undefined
? activeTabIndex
: activeTabIndexFromProps
activeTabIndexFromProps === undefined
? activeTabIndex
: activeTabIndexFromProps
]
}
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/tab/header/item/TabHeaderItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {TabItem} from "../../Tab";
import ListItem from "../../../list/item/ListItem";

type TabHeaderItemProps = {
tab: TabItem;
tab: TabItem<string | number>;
onClick: (index: number) => void;
isActive: boolean;
index: number;
Expand Down
12 changes: 6 additions & 6 deletions stories/12-Tab.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ storiesOf("Tab", module).add("Tab", () => (

<br />
<br />
<StateProvider initialState={0}>
<StateProvider initialState={{index: 0}}>
{(state, setState) => (
<div>
<p>
Expand All @@ -45,21 +45,21 @@ storiesOf("Tab", module).add("Tab", () => (

<div style={{display: "flex"}}>
<Button
onClick={() => setState(Math.max(0, (state - 1) % 2))}
isDisabled={state === 0}>
onClick={() => setState({index: Math.max(0, (state.index - 1) % 2)})}
isDisabled={state.index === 0}>
Previous Tab
</Button>
<Button onClick={() => setState((state + 1) % 2)} isDisabled={state === 1}>
<Button onClick={() => setState({index: (state.index + 1) % 2})} isDisabled={state.index === 1}>
Next Tab
</Button>
</div>

<Tab
items={tabItems}
activeTabIndex={state}
activeTabIndex={state.index}
onTabChange={(index) => {
console.log("tab changed to index: ", index);
setState(index);
setState({index: index});
}}>
{[<div key={0}>{"Home tab"}</div>, <div key={1}>{"Following tab"}</div>]}
</Tab>
Expand Down
Loading