@@ -5,17 +5,29 @@ import { l } from '../../services/Labels';
55import { OptionsContext } from '../OptionsProvider' ;
66import styled from '../../styled-components' ;
77import { RedocRawOptions } from '../../services/RedocNormalizedOptions' ;
8+ import { StyledMarkdownBlock } from '../Markdown/styled.elements' ;
9+ import { Markdown } from '../Markdown/Markdown' ;
810
911export interface EnumValuesProps {
10- values : string [ ] ;
11- isArrayType : boolean ;
12+ values ? : string [ ] | { [ name : string ] : string } ;
13+ type : string | string [ ] ;
1214}
1315
1416export interface EnumValuesState {
1517 collapsed : boolean ;
1618}
1719
20+ const DescriptionEnumsBlock = styled ( StyledMarkdownBlock ) `
21+ table {
22+ margin-bottom: 0.2em;
23+ }
24+ ` ;
25+
1826export class EnumValues extends React . PureComponent < EnumValuesProps , EnumValuesState > {
27+ constructor ( props : EnumValuesProps ) {
28+ super ( props ) ;
29+ this . toggle = this . toggle . bind ( this ) ;
30+ }
1931 state : EnumValuesState = {
2032 collapsed : true ,
2133 } ;
@@ -27,54 +39,94 @@ export class EnumValues extends React.PureComponent<EnumValuesProps, EnumValuesS
2739 }
2840
2941 render ( ) {
30- const { values, isArrayType } = this . props ;
42+ const { values, type } = this . props ;
3143 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+ } ) ) ;
3251
3352 // TODO: provide context interface in more elegant way
3453 const { enumSkipQuotes, maxDisplayedEnumValues } = this . context as RedocRawOptions ;
3554
36- if ( ! values . length ) {
55+ if ( ! enums . length ) {
3756 return null ;
3857 }
3958
4059 const displayedItems =
4160 this . state . collapsed && maxDisplayedEnumValues
42- ? values . slice ( 0 , maxDisplayedEnumValues )
43- : values ;
61+ ? enums . slice ( 0 , maxDisplayedEnumValues )
62+ : enums ;
4463
45- const showToggleButton = maxDisplayedEnumValues
46- ? values . length > maxDisplayedEnumValues
47- : false ;
64+ const showToggleButton = maxDisplayedEnumValues ? enums . length > maxDisplayedEnumValues : false ;
4865
4966 const toggleButtonText = maxDisplayedEnumValues
5067 ? collapsed
51- ? `… ${ values . length - maxDisplayedEnumValues } more`
68+ ? `… ${ enums . length - maxDisplayedEnumValues } more`
5269 : 'Hide'
5370 : '' ;
5471
5572 return (
5673 < 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+ ) }
78130 </ div >
79131 ) ;
80132 }
0 commit comments