Skip to content

Commit a4c6b9a

Browse files
fracmakharel
authored andcommitted
feat(docs): restore enum expansion on docs page (Semantic-Org#1266)
1 parent bb6485a commit a4c6b9a

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

docs/app/Components/ComponentDoc/ComponentProps.js

+75
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,61 @@ 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+
}
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+
68142
render() {
69143
const { props: propsDefinition } = this.props
70144
const content = _.sortBy(_.map(propsDefinition, (config, name) => {
@@ -109,6 +183,7 @@ export default class ComponentProps extends Component {
109183
<Table.Cell>
110184
{item.description && <p>{item.description}</p>}
111185
{this.renderFunctionSignature(item)}
186+
{this.renderEnums(item)}
112187
</Table.Cell>
113188
</Table.Row>
114189
))}

0 commit comments

Comments
 (0)