@@ -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,64 @@ 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
+ } else if ( parts [ 1 ] === '_meta' ) {
92
+ // these look like Sidebar._meta.props.animation
93
+ return _ . get ( this . props , `meta.props.${ parts [ 3 ] } ` )
94
+ }
95
+ return value
96
+ }
97
+
98
+ renderEnums = ( item ) => {
99
+ if ( item . type !== '{enum}' ) return
100
+
101
+ const { showEnumsFor } = this . state
102
+ const truncateAt = 30
103
+
104
+ if ( ! item . value ) return null
105
+
106
+ const values = [ ] . concat ( item . value ) . reduce ( ( accumulator , v ) =>
107
+ accumulator . concat ( this . expandEnums ( _ . trim ( v . value || v , '.\'' ) ) ) ,
108
+ [ ] )
109
+
110
+ // show all if there are few
111
+ if ( values . length < truncateAt ) {
112
+ return (
113
+ < p style = { descriptionExtraStyle } >
114
+ < strong > Enums: </ strong >
115
+ { values . join ( ', ' ) }
116
+ </ p >
117
+ )
118
+ }
119
+
120
+ // add button to show more when there are many values and it is not toggled
121
+ if ( ! showEnumsFor [ item . name ] ) {
122
+ return (
123
+ < p style = { descriptionExtraStyle } >
124
+ < strong > Enums: </ strong >
125
+ < a style = { { cursor : 'pointer' } } onClick = { this . toggleEnumsFor ( item . name ) } >
126
+ Show all { values . length }
127
+ </ a >
128
+ < div > { values . slice ( 0 , truncateAt - 1 ) . join ( ', ' ) } ...</ div >
129
+ </ p >
130
+ )
131
+ }
132
+
133
+ // add "show more" button when there are many
134
+ return (
135
+ < p style = { descriptionExtraStyle } >
136
+ < strong > Enums: </ strong >
137
+ < a style = { { cursor : 'pointer' } } onClick = { this . toggleEnumsFor ( item . name ) } >
138
+ Show less
139
+ </ a >
140
+ < div > { values . join ( ', ' ) } </ div >
141
+ </ p >
142
+ )
143
+ }
144
+
68
145
render ( ) {
69
146
const { props : propsDefinition } = this . props
70
147
const content = _ . sortBy ( _ . map ( propsDefinition , ( config , name ) => {
@@ -109,6 +186,7 @@ export default class ComponentProps extends Component {
109
186
< Table . Cell >
110
187
{ item . description && < p > { item . description } </ p > }
111
188
{ this . renderFunctionSignature ( item ) }
189
+ { this . renderEnums ( item ) }
112
190
</ Table . Cell >
113
191
</ Table . Row >
114
192
) ) }
0 commit comments