Skip to content

Commit

Permalink
Merge pull request #17 from deriv-com/shayan/add-onchange-to-tab
Browse files Browse the repository at this point in the history
Shayan/ fix: add onChange to Tabs component
  • Loading branch information
shayan-deriv authored Jan 24, 2024
2 parents a8e785e + b147ab0 commit 929fbec
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
6 changes: 3 additions & 3 deletions lib/components/Tabs/TabTitle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ export type TabTitleProps = {
title: string;
className?: string;
variant: 'primary' | 'secondary';
onClickHandler?: () => void;
onChange?: () => void;
};

const TabTitle = ({ icon, activeTab, isActive, setSelectedTab, title,className,variant, onClickHandler }: TabTitleProps) => {
const TabTitle = ({ icon, activeTab, isActive, setSelectedTab, title,className,variant, onChange }: TabTitleProps) => {
const handleOnClick = useCallback((title: string) => {
setSelectedTab(title);
onClickHandler?.();
onChange?.();
}, [setSelectedTab, activeTab]);

const classNameVariants: Record<TabTitleProps['variant'], string> = {
Expand Down
9 changes: 5 additions & 4 deletions lib/components/Tabs/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ type TabsProps = {
wrapperClassName?: string;
className?: string;
variant?: 'primary' | 'secondary';
onClickHandler?: () => void;
onChange?: (index: number) => void;
};

const Tabs = ({ children, activeTab, wrapperClassName, className, variant = 'primary', onClickHandler }: TabsProps): JSX.Element => {
const Tabs = ({ children, activeTab, wrapperClassName, className, variant = 'primary', onChange }: TabsProps): JSX.Element => {
const [selectedTab, setSelectedTab] = useState(activeTab || children[0].props.title);

return (
<div className={wrapperClassName}>
<div className={clsx(`derivs-${variant + '-'}tabs`, className)}>
{children.map((item) => {
{children.map((item, index) => {
return (
<TabTitle
icon={item.props.icon}
Expand All @@ -28,8 +28,9 @@ const Tabs = ({ children, activeTab, wrapperClassName, className, variant = 'pri
setSelectedTab={setSelectedTab}
title={item.props.title}
variant={variant}
onClickHandler={onClickHandler}
onChange={()=>onChange?.(index)}
className={item.props.className}

/>
)
})}
Expand Down
2 changes: 1 addition & 1 deletion src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ReactDOM.createRoot(document.getElementById('root')!).render(
<Button> button </Button>
<Text color='blue' size='lg'>this is a Text</Text>

<Tabs variant='primary' activeTab='primary tab 2' >
<Tabs variant='primary' activeTab='primary tab 2' onChange={(index)=>console.log(index)} >
<Tab title='primary tab 1'>
test data for tab 1
</Tab>
Expand Down

0 comments on commit 929fbec

Please sign in to comment.