Skip to content

feat(docs): restore enum expansion on docs page #1266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 2, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions docs/app/Components/ComponentDoc/ComponentProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import _ from 'lodash'
import React, { Component, PropTypes } from 'react'

import { Label, Table } from 'src'
import { SUI } from 'src/lib'

const descriptionExtraStyle = {
fontSize: '0.95em',
color: '#777',
}

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

state = {
showEnumsFor: {},
}

toggleEnumsFor = (prop) => () => {
this.setState({
showEnumsFor: {
...this.state.showEnumsFor,
[prop]: !this.state.showEnumsFor[prop],
},
})
}

renderName = item => <code>{item.name}</code>

requiredRenderer = item => item.required && <Label size='mini' color='red' circular>required</Label>
Expand Down Expand Up @@ -65,6 +84,61 @@ export default class ComponentProps extends Component {
)
}

expandEnums = (value) => {
const parts = value.split('.')
if (parts[0] === 'SUI') {
return SUI[parts[1]]
}
return value
}

renderEnums = (item) => {
if (item.type !== '{enum}') return

const { showEnumsFor } = this.state
const truncateAt = 30

if (!item.value) return null

const values = [].concat(item.value).reduce((accumulator, v) =>
accumulator.concat(this.expandEnums(_.trim(v.value || v, '.\''))),
[])

// show all if there are few
if (values.length < truncateAt) {
return (
<p style={descriptionExtraStyle}>
<strong>Enums: </strong>
{values.join(', ')}
</p>
)
}

// add button to show more when there are many values and it is not toggled
if (!showEnumsFor[item.name]) {
return (
<p style={descriptionExtraStyle}>
<strong>Enums: </strong>
<a style={{ cursor: 'pointer' }} onClick={this.toggleEnumsFor(item.name)}>
Show all {values.length}
</a>
<div>{values.slice(0, truncateAt - 1).join(', ')}...</div>
</p>
)
}

// add "show more" button when there are many
return (
<p style={descriptionExtraStyle}>
<strong>Enums: </strong>
<a style={{ cursor: 'pointer' }} onClick={this.toggleEnumsFor(item.name)}>
Show less
</a>
<div>{values.join(', ')}</div>
</p>
)
}

render() {
const { props: propsDefinition } = this.props
const content = _.sortBy(_.map(propsDefinition, (config, name) => {
Expand Down Expand Up @@ -109,6 +183,7 @@ export default class ComponentProps extends Component {
<Table.Cell>
{item.description && <p>{item.description}</p>}
{this.renderFunctionSignature(item)}
{this.renderEnums(item)}
</Table.Cell>
</Table.Row>
))}
Expand Down