@@ -21,18 +21,18 @@ interface Map<T> {
21
21
}
22
22
23
23
export interface ExampleObject {
24
- summary ?: string ;
25
- description ?: string ;
24
+ summary ?: string | null ;
25
+ description ?: string | null ;
26
26
value ?: any ;
27
- externalValue ?: string ;
27
+ externalValue ?: string | null ;
28
28
}
29
29
30
30
export interface Props {
31
31
className : string ;
32
32
param : {
33
33
description : string ;
34
34
example : any ;
35
- examples : Map < ExampleObject > ;
35
+ examples : Map < ExampleObject > | any [ ] ;
36
36
name : string ;
37
37
required : boolean ;
38
38
deprecated : boolean ;
@@ -57,19 +57,14 @@ ${enumDescriptions
57
57
} ;
58
58
59
59
function ParamsItem ( { param, ...rest } : Props ) {
60
- const {
61
- description,
62
- example,
63
- examples,
64
- name,
65
- required,
66
- deprecated,
67
- enumDescriptions,
68
- } = param ;
60
+ const { description, name, required, deprecated, enumDescriptions } = param ;
69
61
70
62
let schema = param . schema ;
71
63
let defaultValue : string | undefined ;
72
64
65
+ let examples = param . examples || schema ?. examples ;
66
+ let example = param . example || schema ?. example ;
67
+
73
68
if ( ! schema ) {
74
69
schema = { type : "any" } ;
75
70
}
@@ -144,32 +139,75 @@ function ParamsItem({ param, ...rest }: Props) {
144
139
const renderExample = guard ( toString ( example ) , ( example ) => (
145
140
< div >
146
141
< strong > Example: </ strong >
147
- { example }
142
+ < code > { example } </ code >
148
143
</ div >
149
144
) ) ;
150
145
146
+ // Helper function to format example value
147
+ const formatExample = ( example : any ) => {
148
+ if ( typeof example === "object" && example !== null ) {
149
+ return JSON . stringify ( example ) ;
150
+ }
151
+ return String ( example ) ;
152
+ } ;
153
+
154
+ const renderExampleTabItem = (
155
+ exampleName : string ,
156
+ exampleProperties : ExampleObject
157
+ ) => {
158
+ return (
159
+ // @ts -ignore
160
+ < TabItem value = { exampleName } label = { exampleName } >
161
+ { exampleProperties . summary && < p > { exampleProperties . summary } </ p > }
162
+ { exampleProperties . description && (
163
+ < p >
164
+ < strong > Description: </ strong >
165
+ < span > { exampleProperties . description } </ span >
166
+ </ p >
167
+ ) }
168
+ < p >
169
+ < strong > Example: </ strong >
170
+ < code > { formatExample ( exampleProperties . value ) } </ code >
171
+ </ p >
172
+ </ TabItem >
173
+ ) ;
174
+ } ;
175
+
151
176
const renderExamples = guard ( examples , ( examples ) => {
152
- const exampleEntries = Object . entries ( examples ) ;
177
+ // Handle object-based examples (existing logic)
178
+ let exampleEntries : [ string , ExampleObject ] [ ] ;
179
+ if ( Array . isArray ( examples ) ) {
180
+ exampleEntries = examples . map ( ( example , index ) => [
181
+ `Example ${ index + 1 } ` ,
182
+ { value : example , summary : null , description : null } ,
183
+ ] ) ;
184
+ } else {
185
+ exampleEntries = Object . entries ( examples ) ;
186
+ }
187
+
188
+ // If there's only one example, display it without tabs
189
+ if ( exampleEntries . length === 1 ) {
190
+ const firstExample = exampleEntries [ 0 ] [ 1 ] ;
191
+ if ( ! firstExample ) {
192
+ return undefined ;
193
+ }
194
+ return (
195
+ < div >
196
+ < strong > Example: </ strong >
197
+ < span >
198
+ < code > { formatExample ( firstExample . value ) } </ code >
199
+ </ span >
200
+ </ div >
201
+ ) ;
202
+ }
203
+
153
204
return (
154
205
< >
155
206
< strong > Examples:</ strong >
156
207
< SchemaTabs >
157
- { exampleEntries . map ( ( [ exampleName , exampleProperties ] ) => (
158
- // @ts -ignore
159
- < TabItem value = { exampleName } label = { exampleName } >
160
- { exampleProperties . summary && < p > { exampleProperties . summary } </ p > }
161
- { exampleProperties . description && (
162
- < p >
163
- < strong > Description: </ strong >
164
- < span > { exampleProperties . description } </ span >
165
- </ p >
166
- ) }
167
- < p >
168
- < strong > Example: </ strong >
169
- < code > { exampleProperties . value } </ code >
170
- </ p >
171
- </ TabItem >
172
- ) ) }
208
+ { exampleEntries . map ( ( [ exampleName , exampleProperties ] ) =>
209
+ renderExampleTabItem ( exampleName , exampleProperties )
210
+ ) }
173
211
</ SchemaTabs >
174
212
</ >
175
213
) ;
0 commit comments