Skip to content

Commit

Permalink
fix executing callback on first render in TabsRound
Browse files Browse the repository at this point in the history
add initTab property
  • Loading branch information
kishkoigor committed Jun 19, 2024
1 parent 5b7f720 commit 29f11a5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@airdao/ui-library",
"version": "1.4.20",
"version": "1.5.0",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"types": "dist/types.d.ts",
Expand Down
25 changes: 20 additions & 5 deletions src/components/TabsRound/TabsRound.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
import React, { useEffect, useState } from 'react';
import React, { useCallback, useState } from 'react';
import { TabProps, TabsRoundProps } from './TabsRound.types';
import propTypes from 'prop-types';
import s from './tabs-round.module.css';

export function TabsRound({ tabsList, onChange, className }: TabsRoundProps) {
const [activeTab, setActiveTab] = useState(0);
export function TabsRound({
tabsList,
onChange,
className,
initialTab,
}: TabsRoundProps) {
const [activeTab, setActiveTab] = useState(() => {
if (!initialTab) return 0;
const initialTabIndex = tabsList.indexOf(initialTab);
return initialTabIndex === -1 ? 0 : initialTabIndex;
});

useEffect(() => onChange(tabsList[activeTab]), [activeTab]);
const handleTabChange = useCallback(
(index: number) => {
setActiveTab(index);
onChange(tabsList[index]);
},
[tabsList, onChange, setActiveTab],
);

return (
<div className={`${s.tabs} ${className || ''}`}>
{tabsList.map((name, index) => (
<Tab
name={name}
isActive={activeTab === index}
onClick={() => setActiveTab(index)}
onClick={() => handleTabChange(index)}
key={`${name}-${index}`}
/>
))}
Expand Down
1 change: 1 addition & 0 deletions src/components/TabsRound/TabsRound.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface TabsRoundProps {
tabsList: string[];
onChange: (value: string) => void;
className?: string;
initialTab?: string;
}

export interface TabProps {
Expand Down

0 comments on commit 29f11a5

Please sign in to comment.