Skip to content

Commit 42bb1df

Browse files
authored
feat(eds-core-react): onChange now receives the tab’s value (if set) instead of always its index, with index used as a fallback (#3893)
1 parent 1d7c8f2 commit 42bb1df

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

packages/eds-core-react/src/components/Tabs/TabList.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ const TabList = forwardRef<HTMLDivElement, TabListProps>(function TabsList(
120120
'aria-controls': `${tabsId}-panel-${$index + 1}`,
121121
active: isActive,
122122
$index,
123-
onClick: () => handleChange($index),
123+
onClick: () =>
124+
handleChange(
125+
controlledActive !== undefined ? controlledActive : $index,
126+
),
124127
ref: tabRef,
125128
})
126129
}) ?? []

packages/eds-core-react/src/components/Tabs/Tabs.context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Variants } from './Tabs.types'
44
type State = {
55
variant: Variants
66
scrollable: boolean
7-
handleChange: (index: number) => void
7+
handleChange: (value: number | string) => void
88
activeTab: number | string
99
tabsId: string
1010
tabsFocused: boolean

packages/eds-core-react/src/components/Tabs/Tabs.test.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,24 @@ describe('Tabs', () => {
205205
expect(screen.getByTestId(tablist)).toBeDefined()
206206
expect(screen.getByTestId(tabpanels)).toBeDefined()
207207
})
208+
it('Calls onChange with value when provided, otherwise index', () => {
209+
const handleChange = jest.fn()
210+
render(
211+
<Tabs activeTab="overview" onChange={handleChange}>
212+
<Tabs.List>
213+
<Tabs.Tab value="overview">Overview</Tabs.Tab>
214+
<Tabs.Tab value="details">Details</Tabs.Tab>
215+
<Tabs.Tab>Plain</Tabs.Tab>
216+
</Tabs.List>
217+
</Tabs>,
218+
)
219+
220+
fireEvent.click(screen.getByText('Details'))
221+
expect(handleChange).toHaveBeenLastCalledWith('details')
222+
223+
fireEvent.click(screen.getByText('Plain'))
224+
expect(handleChange).toHaveBeenLastCalledWith(2)
225+
226+
expect(handleChange).toHaveBeenCalledTimes(2)
227+
})
208228
})

packages/eds-core-react/src/components/Tabs/Tabs.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export type TabsProps = {
1717
/** The index of the active tab OR a string matching the value prop on the active tab */
1818
activeTab?: number | string
1919
/** The callback function for selecting a tab */
20-
onChange?: (index: number) => void
20+
onChange?: (value: number | string) => void
2121
/** Sets the width of the tabs. Tabs can have a maximum width of 360px */
2222
variant?: Variants
2323
/** adds scrollbar if tabs overflow on non-touch devices */

0 commit comments

Comments
 (0)