From 6aa3ee15473c448a283d22544b65e113e253ae2e Mon Sep 17 00:00:00 2001 From: Lucas Constantino Silva Date: Tue, 21 May 2019 21:26:35 -0300 Subject: [PATCH] feat: ensure parents are unfolded when defaultSelectedPath is a child --- src/NavGroup.tsx | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/NavGroup.tsx b/src/NavGroup.tsx index 0067436..9f4218e 100644 --- a/src/NavGroup.tsx +++ b/src/NavGroup.tsx @@ -1,6 +1,6 @@ import * as React from 'react' import { Nav } from './Nav'; -import { ViewMode, SideNavContext } from './'; +import { ViewMode, SideNavContext, NavContext } from './'; export enum NavGroupState { expanded = 'expanded', @@ -118,7 +118,10 @@ const ToggleIndicator: React.FC<{collapsed: NavGroupState}> = (props) => { export const NavGroup: React.FC = (props) => { const { children, onClick, ...others } = props - const [state, setState] = React.useState(NavGroupState.collapsed) + const { selectedPath } = React.useContext(SideNavContext) + const { pathId } = React.useContext(NavContext) + const childSelected = pathId && selectedPath.indexOf(pathId) === 0 + const [state, setState] = React.useState(childSelected ? NavGroupState.expanded : NavGroupState.collapsed) const rootRef = React.useRef(null) const onHandleClick = (e?: React.MouseEvent ) => {