Skip to content

Commit 5781193

Browse files
committed
feat(docs) restore enum expansion on docs page
1 parent 672feac commit 5781193

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

docs/app/Components/ComponentDoc/ComponentProps.js

+78
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import _ from 'lodash'
22
import React, { Component, PropTypes } from 'react'
33

44
import { Label, Table } from 'src'
5+
import { SUI } from 'src/lib'
6+
7+
const descriptionExtraStyle = {
8+
fontSize: '0.95em',
9+
color: '#777',
10+
}
511

612
/**
713
* Displays a table of a Component's PropTypes.
@@ -20,6 +26,19 @@ export default class ComponentProps extends Component {
2026
meta: PropTypes.object,
2127
}
2228

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+
2342
renderName = item => <code>{item.name}</code>
2443

2544
requiredRenderer = item => item.required && <Label size='mini' color='red' circular>required</Label>
@@ -65,6 +84,64 @@ export default class ComponentProps extends Component {
6584
)
6685
}
6786

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+
68145
render() {
69146
const { props: propsDefinition } = this.props
70147
const content = _.sortBy(_.map(propsDefinition, (config, name) => {
@@ -109,6 +186,7 @@ export default class ComponentProps extends Component {
109186
<Table.Cell>
110187
{item.description && <p>{item.description}</p>}
111188
{this.renderFunctionSignature(item)}
189+
{this.renderEnums(item)}
112190
</Table.Cell>
113191
</Table.Row>
114192
))}

0 commit comments

Comments
 (0)