@@ -5,17 +5,29 @@ import { l } from '../../services/Labels';
5
5
import { OptionsContext } from '../OptionsProvider' ;
6
6
import styled from '../../styled-components' ;
7
7
import { RedocRawOptions } from '../../services/RedocNormalizedOptions' ;
8
+ import { StyledMarkdownBlock } from '../Markdown/styled.elements' ;
9
+ import { Markdown } from '../Markdown/Markdown' ;
8
10
9
11
export interface EnumValuesProps {
10
- values : string [ ] ;
11
- isArrayType : boolean ;
12
+ values ? : string [ ] | { [ name : string ] : string } ;
13
+ type : string | string [ ] ;
12
14
}
13
15
14
16
export interface EnumValuesState {
15
17
collapsed : boolean ;
16
18
}
17
19
20
+ const DescriptionEnumsBlock = styled ( StyledMarkdownBlock ) `
21
+ table {
22
+ margin-bottom: 0.2em;
23
+ }
24
+ ` ;
25
+
18
26
export class EnumValues extends React . PureComponent < EnumValuesProps , EnumValuesState > {
27
+ constructor ( props : EnumValuesProps ) {
28
+ super ( props ) ;
29
+ this . toggle = this . toggle . bind ( this ) ;
30
+ }
19
31
state : EnumValuesState = {
20
32
collapsed : true ,
21
33
} ;
@@ -27,54 +39,94 @@ export class EnumValues extends React.PureComponent<EnumValuesProps, EnumValuesS
27
39
}
28
40
29
41
render ( ) {
30
- const { values, isArrayType } = this . props ;
42
+ const { values, type } = this . props ;
31
43
const { collapsed } = this . state ;
44
+ const isDescriptionEnum = ! Array . isArray ( values ) ;
45
+ const enums =
46
+ ( Array . isArray ( values ) && values ) ||
47
+ Object . entries ( values || { } ) . map ( ( [ value , description ] ) => ( {
48
+ value,
49
+ description,
50
+ } ) ) ;
32
51
33
52
// TODO: provide context interface in more elegant way
34
53
const { enumSkipQuotes, maxDisplayedEnumValues } = this . context as RedocRawOptions ;
35
54
36
- if ( ! values . length ) {
55
+ if ( ! enums . length ) {
37
56
return null ;
38
57
}
39
58
40
59
const displayedItems =
41
60
this . state . collapsed && maxDisplayedEnumValues
42
- ? values . slice ( 0 , maxDisplayedEnumValues )
43
- : values ;
61
+ ? enums . slice ( 0 , maxDisplayedEnumValues )
62
+ : enums ;
44
63
45
- const showToggleButton = maxDisplayedEnumValues
46
- ? values . length > maxDisplayedEnumValues
47
- : false ;
64
+ const showToggleButton = maxDisplayedEnumValues ? enums . length > maxDisplayedEnumValues : false ;
48
65
49
66
const toggleButtonText = maxDisplayedEnumValues
50
67
? collapsed
51
- ? `… ${ values . length - maxDisplayedEnumValues } more`
68
+ ? `… ${ enums . length - maxDisplayedEnumValues } more`
52
69
: 'Hide'
53
70
: '' ;
54
71
55
72
return (
56
73
< div >
57
- < FieldLabel >
58
- { isArrayType ? l ( 'enumArray' ) : '' } { ' ' }
59
- { values . length === 1 ? l ( 'enumSingleValue' ) : l ( 'enum' ) } :
60
- </ FieldLabel > { ' ' }
61
- { displayedItems . map ( ( value , idx ) => {
62
- const exampleValue = enumSkipQuotes ? String ( value ) : JSON . stringify ( value ) ;
63
- return (
64
- < React . Fragment key = { idx } >
65
- < ExampleValue > { exampleValue } </ ExampleValue > { ' ' }
66
- </ React . Fragment >
67
- ) ;
68
- } ) }
69
- { showToggleButton ? (
70
- < ToggleButton
71
- onClick = { ( ) => {
72
- this . toggle ( ) ;
73
- } }
74
- >
75
- { toggleButtonText }
76
- </ ToggleButton >
77
- ) : null }
74
+ { isDescriptionEnum ? (
75
+ < >
76
+ < DescriptionEnumsBlock >
77
+ < table >
78
+ < thead >
79
+ < tr >
80
+ < th >
81
+ < FieldLabel >
82
+ { type === 'array' ? l ( 'enumArray' ) : '' } { ' ' }
83
+ { enums . length === 1 ? l ( 'enumSingleValue' ) : l ( 'enum' ) }
84
+ </ FieldLabel > { ' ' }
85
+ </ th >
86
+ < th >
87
+ < strong > Description</ strong >
88
+ </ th >
89
+ </ tr >
90
+ </ thead >
91
+ < tbody >
92
+ { ( displayedItems as { value : string ; description : string } [ ] ) . map (
93
+ ( { description, value } ) => {
94
+ return (
95
+ < tr key = { value } >
96
+ < td > { value } </ td >
97
+ < td >
98
+ < Markdown source = { description } compact inline />
99
+ </ td >
100
+ </ tr >
101
+ ) ;
102
+ } ,
103
+ ) }
104
+ </ tbody >
105
+ </ table >
106
+ </ DescriptionEnumsBlock >
107
+ { showToggleButton ? (
108
+ < ToggleButton onClick = { this . toggle } > { toggleButtonText } </ ToggleButton >
109
+ ) : null }
110
+ </ >
111
+ ) : (
112
+ < >
113
+ < FieldLabel >
114
+ { type === 'array' ? l ( 'enumArray' ) : '' } { ' ' }
115
+ { values . length === 1 ? l ( 'enumSingleValue' ) : l ( 'enum' ) } :
116
+ </ FieldLabel > { ' ' }
117
+ { displayedItems . map ( ( value , idx ) => {
118
+ const exampleValue = enumSkipQuotes ? String ( value ) : JSON . stringify ( value ) ;
119
+ return (
120
+ < React . Fragment key = { idx } >
121
+ < ExampleValue > { exampleValue } </ ExampleValue > { ' ' }
122
+ </ React . Fragment >
123
+ ) ;
124
+ } ) }
125
+ { showToggleButton ? (
126
+ < ToggleButton onClick = { this . toggle } > { toggleButtonText } </ ToggleButton >
127
+ ) : null }
128
+ </ >
129
+ ) }
78
130
</ div >
79
131
) ;
80
132
}
0 commit comments