Skip to content

Commit

Permalink
feat: updated tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
amars29 committed Dec 6, 2023
1 parent 2854c5a commit 7d2b4f0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
34 changes: 25 additions & 9 deletions packages/unstyled/tabs/src/Tabs.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
import React, { memo } from 'react';
import { forwardRef } from 'react';
import { TabProvider } from './TabProvider';
import { useControllableState } from '@gluestack-ui/hooks';

export const Tabs = <StyledTabs,>(
StyledTabs: React.ComponentType<StyledTabs>
) =>
memo(
forwardRef(
({ value, ...props }: StyledTabs & { value?: string }, ref?: any) => {
const DEFAULT_TAB = 'tab-0';
const [currentActiveTab, setCurrentActiveTab] = React.useState(
value ?? DEFAULT_TAB
);

const onChange = (currentValue: string) =>
setCurrentActiveTab(currentValue);
(
{
value,
onChange,
defaultValue,
...props
}: StyledTabs & {
value?: string;
defaultValue: string;
onChange: (value: string) => void;
},
ref?: any
) => {
const [currentActiveTab, setCurrentActiveTab] = useControllableState({
value,
defaultValue,
onChange: (val) => {
onChange && onChange(val);
},
});

return (
<TabProvider currentActiveTab={currentActiveTab} onChange={onChange}>
<TabProvider
currentActiveTab={currentActiveTab}
onChange={setCurrentActiveTab}
>
<StyledTabs {...(props as StyledTabs)} ref={ref} />
</TabProvider>
);
Expand Down
2 changes: 2 additions & 0 deletions packages/unstyled/tabs/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export interface ITabListProps {
}
export interface ITabsProps {
value?: string;
defaultValue: string;
onChange?: (value: string) => void;
}
export interface ITabPanelProps {
value?: string;
Expand Down

0 comments on commit 7d2b4f0

Please sign in to comment.