Skip to content

Commit

Permalink
Use Composite for TabsRoot and TabPanel
Browse files Browse the repository at this point in the history
  • Loading branch information
mj12albert committed Nov 13, 2024
1 parent 32ed91c commit 5a7f24f
Show file tree
Hide file tree
Showing 12 changed files with 216 additions and 198 deletions.
108 changes: 0 additions & 108 deletions packages/mui-base/src/Tabs/Root/TabsProvider.tsx

This file was deleted.

59 changes: 50 additions & 9 deletions packages/mui-base/src/Tabs/Root/TabsRoot.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import { useTabsRoot } from './useTabsRoot';
import { tabsStyleHookMapping } from './styleHooks';
import { TabsProvider } from './TabsProvider';
import { useComponentRenderer } from '../../utils/useComponentRenderer';
import type { BaseUIComponentProps } from '../../utils/types';
import { CompositeList } from '../../Composite/List/CompositeList';
import { useTabsRoot } from './useTabsRoot';
import { TabsRootContext } from './TabsRootContext';
import { tabsStyleHookMapping } from './styleHooks';
import { TabPanelMetadata } from '../TabPanel/useTabPanel';

/**
*
Expand All @@ -24,22 +26,55 @@ const TabsRoot = React.forwardRef(function TabsRoot(
const {
className,
defaultValue,
direction = 'ltr',
direction: directionProp = 'ltr',
onValueChange,
orientation = 'horizontal',
render,
value,
value: valueProp,
...other
} = props;

const { contextValue, getRootProps, tabActivationDirection } = useTabsRoot({
const {
getRootProps,
direction,
getTabId,
getTabPanelIdByTabValueOrIndex,
onSelected,
registerTabIdLookup,
setTabPanelMap,
tabActivationDirection,
tabPanelRefs,
value,
} = useTabsRoot({
value: valueProp,
defaultValue,
onValueChange,
orientation,
direction,
direction: directionProp,
});

const tabsContextValue = React.useMemo(
() => ({
direction,
getTabId,
getTabPanelIdByTabValueOrIndex,
onSelected,
orientation,
registerTabIdLookup,
tabActivationDirection,
value,
}),
[
direction,
getTabId,
getTabPanelIdByTabValueOrIndex,
onSelected,
orientation,
registerTabIdLookup,
tabActivationDirection,
value,
],
);

const ownerState: TabsRoot.OwnerState = {
orientation,
direction,
Expand All @@ -56,7 +91,13 @@ const TabsRoot = React.forwardRef(function TabsRoot(
customStyleHookMapping: tabsStyleHookMapping,
});

return <TabsProvider value={contextValue}>{renderElement()}</TabsProvider>;
return (
<TabsRootContext.Provider value={tabsContextValue}>
<CompositeList<TabPanelMetadata> elementsRef={tabPanelRefs} onMapChange={setTabPanelMap}>
{renderElement()}
</CompositeList>
</TabsRootContext.Provider>
);
});

export type TabsOrientation = 'horizontal' | 'vertical';
Expand Down
4 changes: 2 additions & 2 deletions packages/mui-base/src/Tabs/Root/TabsRootContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface TabsRootContext {
/**
* The currently selected tab's value.
*/
value?: any | null;
value: any | null;
/**
* Callback for setting new value.
*/
Expand Down Expand Up @@ -36,7 +36,7 @@ export interface TabsRootContext {
* Gets the id of the tab panel with the given value.
* @param value Value to find the tab panel for.
*/
getTabPanelId: (value: any) => string | undefined;
getTabPanelIdByTabValueOrIndex: (tabValue: any, index: number) => string | undefined;
/**
* The position of the active tab relative to the previously active tab.
*/
Expand Down
Loading

0 comments on commit 5a7f24f

Please sign in to comment.