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

Refactor Tabs with Context and types support #336

Merged
merged 6 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 src/components/ui/Tabs/context/TabsContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {createContext} from 'react';

const TabRootContext = createContext(null);

export default TabRootContext;
6 changes: 3 additions & 3 deletions src/components/ui/Tabs/segments/TabContent.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use client';
import React from 'react';
import {customClassSwitcher} from '~/core';
import {Tab} from '../types';
import {TabProps} from '../types';
const COMPONENT_NAME = 'TabContent';


export type TabContentProps ={
tabs?: Tab[]
activeTab: Tab
tabs?: TabProps[]
activeTab: TabProps
className?: string;
customRootClass?: string;
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/ui/Tabs/segments/TabList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import React from 'react';
import {customClassSwitcher} from '~/core';
import TabTrigger from './TabTrigger';
import {Tab} from '../types';
import {TabProps} from '../types';

const COMPONENT_NAME = 'TabList';

Expand All @@ -12,7 +12,7 @@ export type TabListProps = {
className?: string;
customRootClass?: string;
setActiveTab: React.Dispatch<Tab>;
activeTab: Tab;
activeTab: TabProps;
}

const TabList = ({tabs = [], className='', customRootClass='', setActiveTab, activeTab}: TabListProps) => {
Expand Down
22 changes: 9 additions & 13 deletions src/components/ui/Tabs/segments/TabRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,21 @@
import React from 'react';
import {customClassSwitcher} from '~/core';

const COMPONENT_NAME = 'TabRoot';
import TabsContext from '../context/TabsContext';

import {TabRootProps} from '../types';

const COMPONENT_NAME = 'Tabs';

export type TabRootProps = {
children: React.ReactNode;
customRootClass?: string;
className?: string;
color?: string;
props?: Record<string, any>[];
}

const TabRoot = ({children, customRootClass, className, color, ...props}: TabRootProps) => {
const rootClass = customClassSwitcher(customRootClass, COMPONENT_NAME);


return (
<div className={`${rootClass} ${className}`} data-accent-color={color} {...props} >
{children}
</div>
<TabsContext.Provider value={null}>
<div className={`${rootClass} ${className}`} data-accent-color={color} {...props} >
{children}
</div>
</TabsContext.Provider>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TabsContext.Provider is given a null value. Consider providing a meaningful default value or handling this scenario appropriately in the consumer components.

);
};

Expand Down
11 changes: 7 additions & 4 deletions src/components/ui/Tabs/segments/TabTrigger.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use client';
import React from 'react';
import {customClassSwitcher} from '~/core';
import {Tab} from '../types';
import {TabProps} from '../types';

const COMPONENT_NAME = 'TabTrigger';

export type TabTriggerProps = {
tab: Tab;
tab: TabProps;
setActiveTab: React.Dispatch<Tab>;
activeTab: Tab;
activeTab: TabProps;
className?: string;
customRootClass?: string;
index: number;
Expand All @@ -25,7 +25,10 @@ const TabTrigger = ({tab, setActiveTab, activeTab, className, customRootClass, i
};

return (
<button role="tab" key={index} className={`${rootClass} ${isActive?'active':''} ${className}`} {...props} onClick={() => handleClick(tab)}>
<button
role="tab" key={index} className={`${rootClass} ${isActive?'active':''} ${className}`} {...props} onKeyDown={(e) => {
console.log(e.key);
}} onClick={() => handleClick(tab)}>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider removing the console log from the onKeyDown event handler unless it is intended for debugging purposes, in which case it should be conditional on the environment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The handleClick function uses the old Tab type instead of TabProps. Update this to ensure type consistency.

- const handleClick = (tab: Tab) => {
+ const handleClick = (tab: TabProps) => {

Committable suggestion was skipped due to low confidence.

<span className={`${rootClass}-inner`}>
{tab.label}
</span>
Expand Down
5 changes: 0 additions & 5 deletions src/components/ui/Tabs/types.ts

This file was deleted.

9 changes: 9 additions & 0 deletions src/components/ui/Tabs/types/TabProps.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';

type TabProps ={
label: string;
value: string;
content: React.ReactNode;
}

export default TabProps;
11 changes: 11 additions & 0 deletions src/components/ui/Tabs/types/TabRootProps.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type TabRootProps = {
children: React.ReactNode;
customRootClass?: string;
className?: string;
color?: string;
props?: Record<string, any>[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider refining the type of the props property to a single Record<string, any> unless there's a specific need for an array of such objects.

tabs: [];
activeTab: any
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type of tabs should be more specific than an empty array, and activeTab should not use any. Consider using a more precise type.

}

export default TabRootProps;
5 changes: 5 additions & 0 deletions src/components/ui/Tabs/types/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import TabRootProps from './TabRootProps';
import TabProps from './TabProps';


export type {TabRootProps, TabProps};
2 changes: 1 addition & 1 deletion styles/jsTokens/colors.tokens.js

Large diffs are not rendered by default.

Loading