Skip to content

Commit 5ec61e5

Browse files
committed
fix(Navigation): implement item provider to detect parents
1 parent 3c5e30c commit 5ec61e5

File tree

3 files changed

+32
-29
lines changed

3 files changed

+32
-29
lines changed

packages/plus/src/components/Navigation/components/Item.tsx

+17-28
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ import type {
2929
} from 'react'
3030
import {
3131
Children,
32-
cloneElement,
3332
isValidElement,
3433
memo,
3534
useCallback,
35+
useContext,
3636
useEffect,
3737
useMemo,
3838
useReducer,
@@ -41,6 +41,7 @@ import type { PascalToCamelCaseWithoutSuffix } from '../../../types'
4141
import { useNavigation } from '../NavigationProvider'
4242
import { ANIMATION_DURATION, shrinkHeight } from '../constants'
4343
import type { PinUnPinType } from '../types'
44+
import { ItemContext, ItemProvider } from './ItemProvider'
4445

4546
const RelativeDiv = styled.div`
4647
position: relative;
@@ -325,10 +326,6 @@ type ItemProps = {
325326
* You don't need to use this prop it's used internally to control the type of the item
326327
*/
327328
type?: ItemType
328-
/**
329-
* You don't need to use this prop it's used internally to control if the item has a parent
330-
*/
331-
hasParents?: boolean
332329
/**
333330
* You don't need to use this prop it's used internally for pinned item to be reorganized with drag and drop
334331
*/
@@ -372,7 +369,6 @@ export const Item = memo(
372369
active,
373370
noPinButton,
374371
type = 'default',
375-
hasParents,
376372
as,
377373
disabled,
378374
noExpand = false,
@@ -387,6 +383,9 @@ export const Item = memo(
387383
)
388384
}
389385

386+
const itemProvider = useContext(ItemContext)
387+
const hasParents = !!itemProvider
388+
390389
const {
391390
expanded,
392391
locales,
@@ -528,14 +527,6 @@ export const Item = memo(
528527

529528
// This content is when the navigation is expanded
530529
if (expanded || (!expanded && animation === 'expand')) {
531-
const renderChildren = Children.map(children, child =>
532-
isValidElement<ItemProps>(child)
533-
? cloneElement(child, {
534-
hasParents: true,
535-
})
536-
: child,
537-
)
538-
539530
return (
540531
<>
541532
<Container
@@ -708,14 +699,18 @@ export const Item = memo(
708699
{children ? (
709700
<>
710701
{!noExpand ? (
711-
<Expandable
712-
opened={internalExpanded}
713-
animationDuration={expandableAnimationDuration}
714-
>
715-
<PaddedStack>{renderChildren}</PaddedStack>
716-
</Expandable>
702+
<ItemProvider>
703+
<Expandable
704+
opened={internalExpanded}
705+
animationDuration={expandableAnimationDuration}
706+
>
707+
<PaddedStack>{children}</PaddedStack>
708+
</Expandable>
709+
</ItemProvider>
717710
) : (
718-
<PaddedStack>{renderChildren}</PaddedStack>
711+
<ItemProvider>
712+
<PaddedStack>{children}</PaddedStack>
713+
</ItemProvider>
719714
)}
720715
</>
721716
) : null}
@@ -755,13 +750,7 @@ export const Item = memo(
755750
}
756751
placement="right"
757752
>
758-
{Children.map(children, child =>
759-
isValidElement<ItemProps>(child)
760-
? cloneElement(child, {
761-
hasParents: true,
762-
})
763-
: child,
764-
)}
753+
<ItemProvider>{children}</ItemProvider>
765754
</StyledMenu>
766755
) : (
767756
<Tooltip text={label} placement="right" tabIndex={-1}>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { ReactNode } from 'react'
2+
import { createContext, useMemo } from 'react'
3+
4+
// Create the context with a default value
5+
export const ItemContext = createContext(false)
6+
7+
type ItemProviderProps = {
8+
children: ReactNode
9+
}
10+
11+
export const ItemProvider = ({ children }: ItemProviderProps) => {
12+
const value = useMemo(() => true, [])
13+
14+
return <ItemContext.Provider value={value}>{children}</ItemContext.Provider>
15+
}

packages/plus/src/components/Navigation/components/PinnedItems.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ export const PinnedItems = ({
150150
onClickPinUnpin={
151151
items[itemId]?.onClickPinUnpin ?? undefined
152152
}
153-
hasParents
154153
/>
155154
</RelativeDiv>
156155
) : null,

0 commit comments

Comments
 (0)