@@ -17,11 +17,7 @@ const styles = {
1717storiesOf ( 'TableOfContents' , module )
1818 . addDecorator ( withKnobs )
1919 . add ( 'studio /w custom RowComponent' , ( ) => {
20- return (
21- < div style = { styles } >
22- < TableOfContents className = "h-full" contents = { studioContents } rowComponent = { RowComponent } />
23- </ div >
24- ) ;
20+ return < CustomComponentStory /> ;
2521 } )
2622 . add ( 'studio without rowComponent' , ( ) => {
2723 return (
@@ -52,8 +48,42 @@ const MobileStory = () => {
5248 ) ;
5349} ;
5450
55- const RowComponent : RowComponentType < ITableOfContentsLink > = props => (
56- < a href = { props . item . to } >
57- < DefaultRow { ...props } />
58- </ a >
59- ) ;
51+ const NavigationContext = React . createContext ( { path : '' , setPath : ( _ : string ) => { } } ) ;
52+
53+ const CustomComponentStory = ( ) => {
54+ const [ path , setPath ] = React . useState ( '/reference/petstore/openapi.v1.yaml/paths/~1pets~1{petId}/get' ) ;
55+
56+ const contextValue = React . useMemo ( ( ) => ( { path, setPath } ) , [ path , setPath ] ) ;
57+
58+ const contentsWithDynamicIsActive = React . useMemo ( ( ) => {
59+ return studioContents . map ( c => ( { ...c , isActive : c . to === path } ) ) ;
60+ } , [ path ] ) ;
61+
62+ return (
63+ < NavigationContext . Provider value = { contextValue } >
64+ < div style = { styles } >
65+ < TableOfContents className = "h-full" contents = { contentsWithDynamicIsActive } rowComponent = { RowComponent } />
66+ </ div >
67+ </ NavigationContext . Provider >
68+ ) ;
69+ } ;
70+
71+ const RowComponent : RowComponentType < ITableOfContentsLink > = props => {
72+ const { setPath } = React . useContext ( NavigationContext ) ;
73+
74+ const handleClick = React . useCallback (
75+ ( e : React . MouseEvent ) => {
76+ if ( props . item . to ) {
77+ setPath ( props . item . to ) ;
78+ }
79+ e . preventDefault ( ) ;
80+ } ,
81+ [ props . item . to , setPath ] ,
82+ ) ;
83+
84+ return (
85+ < a href = { props . item . to } onClick = { handleClick } >
86+ < DefaultRow { ...props } />
87+ </ a >
88+ ) ;
89+ } ;
0 commit comments