@@ -2,6 +2,12 @@ import _ from 'lodash'
2
2
import React , { Component , PropTypes } from 'react'
3
3
4
4
import { Label , Table } from 'src'
5
+ import { SUI } from 'src/lib'
6
+
7
+ const descriptionExtraStyle = {
8
+ fontSize : '0.95em' ,
9
+ color : '#777' ,
10
+ }
5
11
6
12
/**
7
13
* Displays a table of a Component's PropTypes.
@@ -20,6 +26,19 @@ export default class ComponentProps extends Component {
20
26
meta : PropTypes . object ,
21
27
}
22
28
29
+ state = {
30
+ showEnumsFor : { } ,
31
+ }
32
+
33
+ toggleEnumsFor = ( prop ) => ( ) => {
34
+ this . setState ( {
35
+ showEnumsFor : {
36
+ ...this . state . showEnumsFor ,
37
+ [ prop ] : ! this . state . showEnumsFor [ prop ] ,
38
+ } ,
39
+ } )
40
+ }
41
+
23
42
renderName = item => < code > { item . name } </ code >
24
43
25
44
requiredRenderer = item => item . required && < Label size = 'mini' color = 'red' circular > required</ Label >
@@ -65,6 +84,61 @@ export default class ComponentProps extends Component {
65
84
)
66
85
}
67
86
87
+ expandEnums = ( value ) => {
88
+ const parts = value . split ( '.' )
89
+ if ( parts [ 0 ] === 'SUI' ) {
90
+ return SUI [ parts [ 1 ] ]
91
+ }
92
+ return value
93
+ }
94
+
95
+ renderEnums = ( item ) => {
96
+ if ( item . type !== '{enum}' ) return
97
+
98
+ const { showEnumsFor } = this . state
99
+ const truncateAt = 30
100
+
101
+ if ( ! item . value ) return null
102
+
103
+ const values = [ ] . concat ( item . value ) . reduce ( ( accumulator , v ) =>
104
+ accumulator . concat ( this . expandEnums ( _ . trim ( v . value || v , '.\'' ) ) ) ,
105
+ [ ] )
106
+
107
+ // show all if there are few
108
+ if ( values . length < truncateAt ) {
109
+ return (
110
+ < p style = { descriptionExtraStyle } >
111
+ < strong > Enums: </ strong >
112
+ { values . join ( ', ' ) }
113
+ </ p >
114
+ )
115
+ }
116
+
117
+ // add button to show more when there are many values and it is not toggled
118
+ if ( ! showEnumsFor [ item . name ] ) {
119
+ return (
120
+ < p style = { descriptionExtraStyle } >
121
+ < strong > Enums: </ strong >
122
+ < a style = { { cursor : 'pointer' } } onClick = { this . toggleEnumsFor ( item . name ) } >
123
+ Show all { values . length }
124
+ </ a >
125
+ < div > { values . slice ( 0 , truncateAt - 1 ) . join ( ', ' ) } ...</ div >
126
+ </ p >
127
+ )
128
+ }
129
+
130
+ // add "show more" button when there are many
131
+ return (
132
+ < p style = { descriptionExtraStyle } >
133
+ < strong > Enums: </ strong >
134
+ < a style = { { cursor : 'pointer' } } onClick = { this . toggleEnumsFor ( item . name ) } >
135
+ Show less
136
+ </ a >
137
+ < div > { values . join ( ', ' ) } </ div >
138
+ </ p >
139
+ )
140
+ }
141
+
68
142
render ( ) {
69
143
const { props : propsDefinition } = this . props
70
144
const content = _ . sortBy ( _ . map ( propsDefinition , ( config , name ) => {
@@ -109,6 +183,7 @@ export default class ComponentProps extends Component {
109
183
< Table . Cell >
110
184
{ item . description && < p > { item . description } </ p > }
111
185
{ this . renderFunctionSignature ( item ) }
186
+ { this . renderEnums ( item ) }
112
187
</ Table . Cell >
113
188
</ Table . Row >
114
189
) ) }
0 commit comments