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

Feature/tabs on change #1676

Merged
merged 2 commits into from
Feb 4, 2025
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
5 changes: 5 additions & 0 deletions .changeset/wet-snails-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@gemeente-denhaag/tab": patch
---

Add onChange prop to Tabs component
7 changes: 4 additions & 3 deletions components/Tab/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React, { KeyboardEvent, RefObject, createRef, useEffect, useState } from
import './index.scss';
import { TabsContainer } from './TabsContainer';
import { TabIndicator } from './TabIndicator';
import { HTMLAttributes } from 'react';
import { TabList } from './TabList';
import Tab from './Tab';
import TabPanel from './TabPanel';
Expand All @@ -16,8 +15,9 @@ export * from './TabPanel';
export * from './TabsContainer';
export * from './TabText';

export interface TabsProps extends HTMLAttributes<HTMLDivElement> {
export interface TabsProps {
tabData: Array<{ label: string; panelContent: React.ReactNode }>;
onChange?: (index: number) => void;
}

type TabRef = RefObject<HTMLDivElement | null>;
Expand All @@ -34,7 +34,7 @@ interface CustomCSSProperties extends React.CSSProperties {
* @param props The properties of a Tabs component.
* @constructor Constructs an instance of Tabs.
*/
export const Tabs: React.FC<TabsProps> = ({ tabData }: TabsProps) => {
export const Tabs = ({ tabData, onChange }: TabsProps) => {
const [tabs, setTabs] = useState(
tabData.map((tab) => {
const tabRef = createRef<HTMLDivElement>();
Expand Down Expand Up @@ -83,6 +83,7 @@ export const Tabs: React.FC<TabsProps> = ({ tabData }: TabsProps) => {
const handleTabChange = (tabRef: TabRef) => {
setSelectedTab(tabRef);
setFocussedTab(tabRef);
onChange?.(tabs.findIndex(({ tabRef: ref }) => ref === tabRef));
};

const handleKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
},
"engines": {
"node": "^20",
"pnpm": "^9"
"pnpm": "^10"
},
"packageManager": "pnpm@9.1.1+sha256.9551e803dcb7a1839fdf5416153a844060c7bce013218ce823410532504ac10b"
"packageManager": "pnpm@10.2.0+sha256.72f1630812107c8723eb5be4517725f3e13f09cfd8f71571cf29f23ce318bdb7"
}