1- import React , { FC } from 'react'
1+ import React , { FC , ReactNode , useMemo } from 'react'
22import { Ads , Banner , Seo , Toc } from '../components'
3- import { CContainer , CNav , CNavItem , CNavLink } from '@coreui/react/src/'
3+ import { CContainer , CNav , CNavItem , CNavLink } from '@coreui/react'
4+ // @ts -expect-error json file
5+ import jsonData from './../data/other_frameworks.json'
6+
7+ import type { TocItem } from '../components/Toc'
48
59interface DocsLayoutProps {
6- children : any // eslint-disable-line @typescript-eslint/no-explicit-any
7- data : any // eslint-disable-line @typescript-eslint/no-explicit-any
8- location : any // eslint-disable-line @typescript-eslint/no-explicit-any
9- pageContext : any // eslint-disable-line @typescript-eslint/no-explicit-any
10+ children : ReactNode
11+ data : DocsData
12+ location : Location
13+ pageContext : PageContext
1014}
1115
12- // @ts -expect-error json file
13- import jsonData from './../data/other_frameworks.json'
16+ interface DocsData {
17+ allMdx : {
18+ edges : Array < {
19+ node : {
20+ fields : {
21+ slug : string
22+ }
23+ }
24+ } >
25+ }
26+ mdx ?: {
27+ tableOfContents : {
28+ items : TocItem [ ]
29+ }
30+ }
31+ }
32+
33+ interface PageContext {
34+ frontmatter ?: Frontmatter
35+ }
1436
15- const humanize = ( text : string ) => {
16- const string = text
37+ interface Frontmatter {
38+ title ?: string
39+ description ?: string
40+ name ?: string
41+ other_frameworks ?: string
42+ pro_component ?: boolean
43+ route ?: string
44+ }
45+
46+ interface OtherFrameworks {
47+ [ key : string ] : {
48+ [ key : string ] : string
49+ }
50+ }
51+
52+ const humanize = ( text : string ) : string => {
53+ return text
1754 . split ( '-' )
18- . reduce (
19- ( accumulator , currentValue ) =>
20- accumulator + ' ' + currentValue [ 0 ] . toUpperCase ( ) + currentValue . slice ( 1 ) ,
21- )
22- return string [ 0 ] . toUpperCase ( ) + string . slice ( 1 )
55+ . map ( ( word ) => word . charAt ( 0 ) . toUpperCase ( ) + word . slice ( 1 ) )
56+ . join ( ' ' )
2357}
2458
59+ const DocsNav : FC < {
60+ route : string
61+ locationPathname : string
62+ hasNavAPI : boolean
63+ hasNavStyling : boolean
64+ hasNavAccessibility : boolean
65+ } > = ( { route, locationPathname, hasNavAPI, hasNavStyling, hasNavAccessibility } ) => (
66+ < CNav className = "ms-lg-4 docs-nav bg-body" variant = "underline-border" >
67+ < CNavItem >
68+ < CNavLink href = { route } active = { route === locationPathname } >
69+ Features
70+ </ CNavLink >
71+ </ CNavItem >
72+ { hasNavAPI && (
73+ < CNavItem >
74+ < CNavLink href = { `${ route } api/` } active = { `${ route } api/` === locationPathname } >
75+ API
76+ </ CNavLink >
77+ </ CNavItem >
78+ ) }
79+ { hasNavStyling && (
80+ < CNavItem >
81+ < CNavLink href = { `${ route } styling/` } active = { `${ route } styling/` === locationPathname } >
82+ Styling
83+ </ CNavLink >
84+ </ CNavItem >
85+ ) }
86+ { hasNavAccessibility && (
87+ < CNavItem >
88+ < CNavLink
89+ href = { `${ route } accessibility/` }
90+ active = { `${ route } accessibility/` === locationPathname }
91+ >
92+ Accessibility
93+ </ CNavLink >
94+ </ CNavItem >
95+ ) }
96+ </ CNav >
97+ )
98+
2599const DocsLayout : FC < DocsLayoutProps > = ( { children, data, location, pageContext } ) => {
26- const title = pageContext . frontmatter ? pageContext . frontmatter . title : ''
27- const description = pageContext . frontmatter ? pageContext . frontmatter . description : ''
28- const name = pageContext . frontmatter ? pageContext . frontmatter . name : ''
29- const other_frameworks = pageContext . frontmatter ? pageContext . frontmatter . other_frameworks : ''
30- const pro_component = pageContext . frontmatter ? pageContext . frontmatter . pro_component : ''
31- const route = pageContext . frontmatter ? pageContext . frontmatter . route : ''
32- const frameworks = other_frameworks ? other_frameworks . split ( ', ' ) : false
33- const otherFrameworks = JSON . parse ( JSON . stringify ( jsonData ) )
100+ const frontmatter = pageContext . frontmatter || { }
34101
35- const hasNav = data ?. allMdx ?. edges . length > 1
36- const hasNavAccesibility =
37- hasNav && data . allMdx . edges . some ( ( edge : any ) => edge . node . fields . slug . includes ( 'accesibility' ) )
38- const hasNavAPI =
39- hasNav && data . allMdx . edges . some ( ( edge : any ) => edge . node . fields . slug . includes ( 'api' ) )
40- const hasNavCustomizing =
41- hasNav && data . allMdx . edges . some ( ( edge : any ) => edge . node . fields . slug . includes ( 'customizing' ) )
102+ const {
103+ title = '' ,
104+ description = '' ,
105+ name = '' ,
106+ other_frameworks : otherFrameworksStr = '' ,
107+ pro_component : proComponent = false ,
108+ route = '' ,
109+ } = frontmatter
110+ const frameworks = useMemo (
111+ ( ) => otherFrameworksStr . split ( ', ' ) . filter ( Boolean ) ,
112+ [ otherFrameworksStr ] ,
113+ )
114+ const otherFrameworks : OtherFrameworks = useMemo ( ( ) => ( { ...jsonData } ) , [ ] )
115+ const hasNav = useMemo ( ( ) => data ?. allMdx ?. edges . length > 1 , [ data ] )
116+ const hasNavAccessibility = useMemo (
117+ ( ) =>
118+ hasNav && data . allMdx . edges . some ( ( edge ) => edge . node . fields . slug . includes ( 'accessibility' ) ) ,
119+ [ hasNav , data ] ,
120+ )
121+ const hasNavAPI = useMemo (
122+ ( ) => hasNav && data . allMdx . edges . some ( ( edge ) => edge . node . fields . slug . includes ( 'api' ) ) ,
123+ [ hasNav , data ] ,
124+ )
125+ const hasNavStyling = useMemo (
126+ ( ) => hasNav && data . allMdx . edges . some ( ( edge ) => edge . node . fields . slug . includes ( 'styling' ) ) ,
127+ [ hasNav , data ] ,
128+ )
42129
43130 return (
44131 < >
45132 < Seo title = { title } description = { description } name = { name } />
46133 < CContainer lg className = "my-md-4 flex-grow-1" >
47134 < main className = "docs-main order-1" >
48135 { hasNav && (
49- < CNav className = "ms-lg-4 docs-nav bg-body" variant = "underline-border" >
50- < CNavItem >
51- < CNavLink href = { `${ route } ` } active = { route === location . pathname } >
52- Features
53- </ CNavLink >
54- </ CNavItem >
55- { hasNavAPI && (
56- < CNavItem >
57- < CNavLink href = { `${ route } api/` } active = { `${ route } api/` === location . pathname } >
58- API
59- </ CNavLink >
60- </ CNavItem >
61- ) }
62- { hasNavCustomizing && (
63- < CNavItem >
64- < CNavLink
65- href = { `${ route } customizing/` }
66- active = { `${ route } customizing/` === location . pathname }
67- >
68- Customizing
69- </ CNavLink >
70- </ CNavItem >
71- ) }
72- { hasNavAccesibility && (
73- < CNavItem >
74- < CNavLink
75- href = { `${ route } accesibility/` }
76- active = { `${ route } accesibility/` === location . pathname }
77- >
78- Accesibility
79- </ CNavLink >
80- </ CNavItem >
81- ) }
82- </ CNav >
136+ < DocsNav
137+ route = { route }
138+ locationPathname = { location . pathname }
139+ hasNavAPI = { hasNavAPI }
140+ hasNavStyling = { hasNavStyling }
141+ hasNavAccessibility = { hasNavAccessibility }
142+ />
83143 ) }
84144 < div className = "docs-intro ps-lg-4" >
85145 { name && name !== title ? (
@@ -94,38 +154,36 @@ const DocsLayout: FC<DocsLayoutProps> = ({ children, data, location, pageContext
94154 { title }
95155 </ h1 >
96156 ) }
97- < Banner pro = { pro_component } />
157+ < Banner pro = { proComponent } />
98158 < p className = "docs-lead" > { description } </ p >
99159 < Ads code = "CEAICKJY" location = { route } placement = "coreuiio" />
100- { frameworks && (
160+ { frameworks . length > 0 && (
101161 < >
102- < h2 > Other frameworks </ h2 >
162+ < h2 > Other Frameworks </ h2 >
103163 < p >
104164 CoreUI components are available as native Angular, Bootstrap (Vanilla JS), and Vue
105165 components. To learn more please visit the following pages.
106166 </ p >
107167 < ul >
108- { frameworks . map ( ( item : string , index : number ) => (
109- < React . Fragment key = { index } >
110- { Object . keys ( otherFrameworks [ item ] ) . map (
111- ( el , index ) =>
112- el !== 'react' && (
113- < li key = { index } >
114- < a href = { otherFrameworks [ item ] [ el ] } >
115- < >
116- { el [ 0 ] . toUpperCase ( ) + el . slice ( 1 ) } { humanize ( item ) }
117- </ >
118- </ a >
119- </ li >
120- ) ,
121- ) }
168+ { frameworks . map ( ( item ) => (
169+ < React . Fragment key = { item } >
170+ { Object . entries ( otherFrameworks [ item ] || { } )
171+ . filter ( ( [ key ] ) => key !== 'react' )
172+ . map ( ( [ framework , url ] ) => (
173+ < li key = { framework } >
174+ < a href = { url } >
175+ { framework . charAt ( 0 ) . toUpperCase ( ) + framework . slice ( 1 ) } { ' ' }
176+ { humanize ( item ) }
177+ </ a >
178+ </ li >
179+ ) ) }
122180 </ React . Fragment >
123181 ) ) }
124182 </ ul >
125183 </ >
126184 ) }
127185 </ div >
128- { data && data . mdx && < Toc items = { data . mdx . tableOfContents . items } /> }
186+ { data . mdx && < Toc items = { data . mdx . tableOfContents . items } /> }
129187 < div className = "docs-content ps-lg-4" > { children } </ div >
130188 </ main >
131189 </ CContainer >
0 commit comments