@@ -6,33 +6,37 @@ import Typography from '@mui/material/Typography';
6
6
import Alert from '@mui/material/Alert' ;
7
7
import VerifiedRoundedIcon from '@mui/icons-material/VerifiedRounded' ;
8
8
import { alpha } from '@mui/material/styles' ;
9
- import { useTranslate , useUserLanguage } from '@mui/docs/i18n' ;
9
+ import { Translate , useTranslate , useUserLanguage } from '@mui/docs/i18n' ;
10
10
import { HighlightedCode } from '@mui/docs/HighlightedCode' ;
11
11
import { MarkdownElement } from '@mui/docs/MarkdownElement' ;
12
- import { SectionTitle } from '@mui/docs/SectionTitle' ;
12
+ import { SectionTitle , SectionTitleProps } from '@mui/docs/SectionTitle' ;
13
13
import AppLayoutDocs from 'docs/src/modules/components/AppLayoutDocs' ;
14
14
import PropertiesSection from 'docs/src/modules/components/ApiPage/sections/PropertiesSection' ;
15
- import { DEFAULT_API_LAYOUT_STORAGE_KEYS } from 'docs/src/modules/components/ApiPage/sections/ToggleDisplayOption' ;
15
+ import { PropertyDefinition } from 'docs/src/modules/components/ApiPage/definitions/properties' ;
16
+ import { LayoutStorageKeys } from 'docs/src/modules/components/ApiPage' ;
17
+ import {
18
+ DEFAULT_API_LAYOUT_STORAGE_KEYS ,
19
+ ApiDisplayOptions ,
20
+ } from 'docs/src/modules/components/ApiPage/sections/ToggleDisplayOption' ;
21
+ import {
22
+ InterfaceApiTranslation ,
23
+ InterfaceApiContent ,
24
+ } from 'docsx/scripts/api/buildInterfacesDocumentation' ;
25
+ import { TableOfContentsEntry } from '@mui/internal-markdown' ;
26
+ import kebabCase from 'lodash/kebabCase' ;
16
27
17
- export function getTranslatedHeader ( t , header ) {
28
+ type HeaderHash = 'demos' | 'import' ;
29
+
30
+ export function getTranslatedHeader ( t : Translate , header : HeaderHash ) {
18
31
const translations = {
19
32
demos : t ( 'api-docs.demos' ) ,
20
33
import : t ( 'api-docs.import' ) ,
21
34
} ;
22
35
23
- // TODO Drop runtime type-checking once we type-check this file
24
- if ( ! translations . hasOwnProperty ( header ) ) {
25
- throw new TypeError (
26
- `Unable to translate header '${ header } '. Did you mean one of '${ Object . keys (
27
- translations ,
28
- ) . join ( "', '" ) } '`,
29
- ) ;
30
- }
31
-
32
36
return translations [ header ] || header ;
33
37
}
34
38
35
- function Heading ( props ) {
39
+ function Heading ( props : Pick < SectionTitleProps < HeaderHash > , 'hash' | 'level' > ) {
36
40
const { hash, level = 'h2' } = props ;
37
41
const t = useTranslate ( ) ;
38
42
@@ -44,7 +48,62 @@ Heading.propTypes = {
44
48
level : PropTypes . string ,
45
49
} ;
46
50
47
- export default function ApiPage ( props ) {
51
+ interface ApiPageProps {
52
+ descriptions : {
53
+ [ lang : string ] : InterfaceApiTranslation & {
54
+ // Table of Content added by the mapApiPageTranslations function
55
+ componentDescriptionToc : TableOfContentsEntry [ ] ;
56
+ } ;
57
+ } ;
58
+ pageContent : InterfaceApiContent ;
59
+ defaultLayout ?: ApiDisplayOptions ;
60
+ /**
61
+ * The localStorage key used to save the user layout for each section.
62
+ * It's useful to dave different preferences on different pages.
63
+ * For example, the data grid has a different key that the core.
64
+ */
65
+ layoutStorageKey ?: LayoutStorageKeys ;
66
+ }
67
+
68
+ interface GetInterfaceApiDefinitionsParams {
69
+ interfaceName : string ;
70
+ properties : InterfaceApiContent [ 'properties' ] ;
71
+ propertiesDescriptions : InterfaceApiTranslation [ 'propertiesDescriptions' ] ;
72
+ /**
73
+ * Add indicators that the properties is optional instead of showing it is required.
74
+ */
75
+ showOptionalAbbr ?: boolean ;
76
+ }
77
+
78
+ export function getInterfaceApiDefinitions (
79
+ params : GetInterfaceApiDefinitionsParams ,
80
+ ) : PropertyDefinition [ ] {
81
+ const { properties, propertiesDescriptions, interfaceName, showOptionalAbbr = false } = params ;
82
+
83
+ return Object . entries ( properties ) . map ( ( [ propertyName , propertyData ] ) => {
84
+ const isRequired = propertyData . required && ! showOptionalAbbr ;
85
+ const isOptional = ! propertyData . required && showOptionalAbbr ;
86
+
87
+ const typeName = propertyData . type . description ;
88
+ const propDefault = propertyData . default ;
89
+ const propDescription = propertiesDescriptions [ propertyName ] ;
90
+
91
+ return {
92
+ propName : propertyName ,
93
+ hash : `${ kebabCase ( interfaceName ) } -prop-${ propertyName } ` ,
94
+ propertyName,
95
+ description : propDescription ?. description ,
96
+ isOptional,
97
+ isRequired,
98
+ typeName,
99
+ propDefault,
100
+ isProPlan : propertyData . isProPlan ,
101
+ isPremiumPlan : propertyData . isPremiumPlan ,
102
+ } ;
103
+ } ) ;
104
+ }
105
+
106
+ export default function ApiPage ( props : ApiPageProps ) {
48
107
const {
49
108
descriptions,
50
109
pageContent,
@@ -54,21 +113,17 @@ export default function ApiPage(props) {
54
113
const t = useTranslate ( ) ;
55
114
const userLanguage = useUserLanguage ( ) ;
56
115
57
- const { demos, filename = '' , properties } = pageContent ;
116
+ const { demos, properties } = pageContent ;
58
117
59
- const { componentDescription, propertiesDescriptions, interfaceDescription } =
60
- descriptions [ userLanguage ] ;
61
- const description = t ( 'api-docs.pageDescription' ) . replace ( / { { name} } / , pageContent . name ) ;
62
-
63
- // Prefer linking the .tsx or .d.ts for the "Edit this page" link.
64
- const apiSourceLocation = filename . replace ( '.js' , '.d.ts' ) ;
118
+ const { propertiesDescriptions, interfaceDescription } = descriptions [ userLanguage ] ;
119
+ const description = t ( 'api-docs.interfacePageDescription' ) . replace ( / { { name} } / , pageContent . name ) ;
65
120
66
121
return (
67
122
< AppLayoutDocs
68
123
description = { description }
69
124
disableToc = { false }
70
125
toc = { [ ] }
71
- location = { apiSourceLocation }
126
+ location = ""
72
127
title = { `${ pageContent . name } API` }
73
128
disableAd
74
129
>
@@ -79,7 +134,7 @@ export default function ApiPage(props) {
79
134
component = "p"
80
135
className = "description"
81
136
gutterBottom
82
- dangerouslySetInnerHTML = { { __html : interfaceDescription } }
137
+ dangerouslySetInnerHTML = { { __html : description } }
83
138
/>
84
139
< Heading hash = "demos" />
85
140
{ demos && (
@@ -131,26 +186,29 @@ export default function ApiPage(props) {
131
186
language = "jsx"
132
187
/>
133
188
134
- { componentDescription ? (
189
+ { interfaceDescription ? (
135
190
< React . Fragment >
136
191
< br />
137
192
< br />
138
193
< span
139
194
dangerouslySetInnerHTML = { {
140
- __html : componentDescription ,
195
+ __html : interfaceDescription ,
141
196
} }
142
197
/>
143
198
</ React . Fragment >
144
199
) : null }
200
+
145
201
< PropertiesSection
146
- properties = { properties }
147
- propertiesDescriptions = { propertiesDescriptions }
148
- componentName = { pageContent . name }
202
+ properties = { getInterfaceApiDefinitions ( {
203
+ propertiesDescriptions,
204
+ properties,
205
+ interfaceName : pageContent . name ,
206
+ showOptionalAbbr : true ,
207
+ } ) }
149
208
title = "api-docs.properties"
150
209
titleHash = "properties"
151
210
defaultLayout = { defaultLayout }
152
211
layoutStorageKey = { layoutStorageKey . props }
153
- showOptionalAbbr
154
212
/>
155
213
</ MarkdownElement >
156
214
< svg style = { { display : 'none' } } xmlns = "http://www.w3.org/2000/svg" >
@@ -162,15 +220,13 @@ export default function ApiPage(props) {
162
220
) ;
163
221
}
164
222
165
- ApiPage . propTypes = {
166
- defaultLayout : PropTypes . oneOf ( [ 'collapsed' , 'expanded' , 'table' ] ) ,
167
- descriptions : PropTypes . object . isRequired ,
168
- layoutStorageKey : PropTypes . shape ( {
169
- props : PropTypes . string ,
170
- } ) ,
171
- pageContent : PropTypes . object . isRequired ,
172
- } ;
173
-
174
223
if ( process . env . NODE_ENV !== 'production' ) {
175
- ApiPage . propTypes = exactProp ( ApiPage . propTypes ) ;
224
+ ApiPage . propTypes = exactProp ( {
225
+ defaultLayout : PropTypes . oneOf ( [ 'collapsed' , 'expanded' , 'table' ] ) ,
226
+ descriptions : PropTypes . object . isRequired ,
227
+ layoutStorageKey : PropTypes . shape ( {
228
+ props : PropTypes . string ,
229
+ } ) ,
230
+ pageContent : PropTypes . object . isRequired ,
231
+ } ) ;
176
232
}
0 commit comments