Skip to content

Commit

Permalink
Force CompositeRoot to propagate events
Browse files Browse the repository at this point in the history
  • Loading branch information
mj12albert committed Nov 19, 2024
1 parent 68b5598 commit 83d372c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
3 changes: 3 additions & 0 deletions packages/mui-base/src/Composite/Root/CompositeRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const CompositeRoot = React.forwardRef(function CompositeRoot(
cols,
enableHomeAndEndKeys,
onMapChange,
alwaysPropagateEvents,
...otherProps
} = props;

Expand All @@ -39,6 +40,7 @@ const CompositeRoot = React.forwardRef(function CompositeRoot(
activeIndex: activeIndexProp,
onActiveIndexChange: onActiveIndexChangeProp,
rootRef: forwardedRef,
alwaysPropagateEvents,
enableHomeAndEndKeys,
});

Expand Down Expand Up @@ -79,6 +81,7 @@ namespace CompositeRoot {
enableHomeAndEndKeys?: boolean;
// TODO: can't pass a generic <CustomMetadata> into forwardRef?
onMapChange?: (newMap: Map<Node, any>) => void;
alwaysPropagateEvents?: boolean;
}
}

Expand Down
7 changes: 6 additions & 1 deletion packages/mui-base/src/Composite/Root/useCompositeRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export interface UseCompositeRootParameters {
* @default false
*/
enableHomeAndEndKeys?: boolean;
alwaysPropagateEvents?: boolean;
}

// Advanced options of Composite, to be implemented later if needed.
Expand All @@ -64,6 +65,7 @@ export function useCompositeRoot(params: UseCompositeRootParameters) {
onActiveIndexChange: externalSetActiveIndex,
rootRef: externalRef,
enableHomeAndEndKeys = false,
alwaysPropagateEvents = false,
} = params;

const [internalActiveIndex, internalSetActiveIndex] = React.useState(0);
Expand Down Expand Up @@ -219,7 +221,9 @@ export function useCompositeRoot(params: UseCompositeRootParameters) {
}

if (nextIndex !== activeIndex && !isIndexOutOfBounds(elementsRef, nextIndex)) {
event.stopPropagation();
if (!alwaysPropagateEvents) {
event.stopPropagation();
}

if (preventedKeys.includes(event.key)) {
event.preventDefault();
Expand All @@ -236,6 +240,7 @@ export function useCompositeRoot(params: UseCompositeRootParameters) {
}),
[
activeIndex,
alwaysPropagateEvents,
cols,
dense,
elementsRef,
Expand Down
11 changes: 7 additions & 4 deletions packages/mui-base/src/Tabs/Root/useTabsRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,13 @@ function useTabsRoot(parameters: useTabsRoot.Parameters): useTabsRoot.ReturnValu
// used as a param for `useActivationDirectionDetector`
// put this into TabsRootContext
// use it in useTabsList
const getTabElementBySelectedValue = React.useCallback((selectedValue: any | undefined) => {
console.log('getTabElementBySelectedValue', selectedValue);
return null;
}, []);
const getTabElementBySelectedValue = React.useCallback(
(/* selectedValue: any | undefined */) => {
// console.log('getTabElementBySelectedValue', selectedValue);
return null;
},
[],
);

// TODO: no need to put this in a ref anymore?
const registerGetTabIdByPanelValueOrIndexFn = React.useCallback(
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-base/src/Tabs/Tab/useTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function useTab(parameters: useTab.Parameters): useTab.ReturnValue {
// FIXME: something is wrong with this
useEnhancedEffect(() => {
if (activateOnFocus && selected && index > -1 && highlightedTabIndex !== index) {
console.log('useEnhancedEffect update index', index);
// console.log('useEnhancedEffect update index', index);
setHighlightedTabIndex(index);
}
}, [activateOnFocus, highlightedTabIndex, index, selected, setHighlightedTabIndex]);
Expand Down
1 change: 1 addition & 0 deletions packages/mui-base/src/Tabs/TabsList/TabsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ const TabsList = React.forwardRef(function TabsList(
*/}
<CompositeRoot
activeIndex={highlightedTabIndex}
alwaysPropagateEvents
loop={loop}
onActiveIndexChange={setHighlightedTabIndex}
onMapChange={setTabMap}
Expand Down

0 comments on commit 83d372c

Please sign in to comment.