From 84659635128021ad07efab5ba66113a5de18fd68 Mon Sep 17 00:00:00 2001 From: Albert Yu Date: Tue, 22 Oct 2024 15:58:27 +0800 Subject: [PATCH] TabsList --- .../mui-base/src/Tabs/TabsList/TabsList.tsx | 26 ++++++++++---- .../src/Tabs/TabsList/useTabsList.test.tsx | 24 +++++++++++-- .../mui-base/src/Tabs/TabsList/useTabsList.ts | 36 ++++++++----------- 3 files changed, 56 insertions(+), 30 deletions(-) diff --git a/packages/mui-base/src/Tabs/TabsList/TabsList.tsx b/packages/mui-base/src/Tabs/TabsList/TabsList.tsx index 140f0a8d9d..c955ff264e 100644 --- a/packages/mui-base/src/Tabs/TabsList/TabsList.tsx +++ b/packages/mui-base/src/Tabs/TabsList/TabsList.tsx @@ -4,6 +4,7 @@ import PropTypes from 'prop-types'; import { useTabsList } from './useTabsList'; import { TabsListProvider } from './TabsListProvider'; import { tabsStyleHookMapping } from '../Root/styleHooks'; +import { useTabsRootContext } from '../Root/TabsRootContext'; import { useComponentRenderer } from '../../utils/useComponentRenderer'; import { TabsRoot } from '../Root/TabsRoot'; import { BaseUIComponentProps } from '../../utils/types'; @@ -24,12 +25,25 @@ const TabsList = React.forwardRef(function TabsList( ) { const { activateOnFocus = true, className, loop = true, render, ...other } = props; - const { direction, orientation, getRootProps, contextValue, tabActivationDirection } = - useTabsList({ - rootRef: forwardedRef, - loop, - activateOnFocus, - }); + const { + direction = 'ltr', + onSelected, + orientation = 'horizontal', + value, + registerTabIdLookup, + tabActivationDirection, + } = useTabsRootContext(); + + const { getRootProps, contextValue } = useTabsList({ + activateOnFocus, + direction, + loop, + onSelected, + orientation, + registerTabIdLookup, + rootRef: forwardedRef, + value, + }); const ownerState: TabsList.OwnerState = React.useMemo( () => ({ diff --git a/packages/mui-base/src/Tabs/TabsList/useTabsList.test.tsx b/packages/mui-base/src/Tabs/TabsList/useTabsList.test.tsx index 7a89c7d6df..1784250f9f 100644 --- a/packages/mui-base/src/Tabs/TabsList/useTabsList.test.tsx +++ b/packages/mui-base/src/Tabs/TabsList/useTabsList.test.tsx @@ -5,6 +5,8 @@ import { createRenderer, screen, fireEvent } from '@mui/internal-test-utils'; import { Tabs } from '@base_ui/react/Tabs'; import { useTabsList } from './useTabsList'; +const NOOP = () => {}; + describe('useTabsList', () => { const { render } = createRenderer(); @@ -12,7 +14,16 @@ describe('useTabsList', () => { it('returns props for root slot', () => { function TestTabsList() { const rootRef = React.createRef(); - const { getRootProps } = useTabsList({ rootRef, activateOnFocus: true, loop: true }); + const { getRootProps } = useTabsList({ + activateOnFocus: true, + direction: 'ltr', + loop: true, + onSelected: NOOP, + orientation: 'horizontal', + registerTabIdLookup: NOOP, + rootRef, + value: 0, + }); return
; } @@ -35,7 +46,16 @@ describe('useTabsList', () => { function TestTabsList() { const rootRef = React.createRef(); - const { getRootProps } = useTabsList({ rootRef, activateOnFocus: true, loop: true }); + const { getRootProps } = useTabsList({ + activateOnFocus: true, + direction: 'ltr', + loop: true, + onSelected: NOOP, + orientation: 'horizontal', + registerTabIdLookup: NOOP, + rootRef, + value: 0, + }); return (
{ /** * If `true`, the tab will be activated whenever it is focused. * Otherwise, it has to be activated by clicking or pressing the Enter or Space key. @@ -298,20 +299,11 @@ namespace useTabsList { * The value of the currently highlighted tab. */ highlightedValue: any | null; - /** - * If `true`, it will indicate that the text's direction in right-to-left. - */ - direction: TabsDirection; - /** - * The component orientation (layout flow direction). - */ - orientation: TabsOrientation; rootRef: React.RefCallback | null; /** * The value of the currently selected tab. */ selectedValue: any | null; - tabActivationDirection: TabActivationDirection; } }