Skip to content

Commit

Permalink
Add dropdown component for visualising oneOf
Browse files Browse the repository at this point in the history
  • Loading branch information
Brendan Abbott committed Jun 6, 2017
1 parent 7568dcc commit 6823996
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
27 changes: 21 additions & 6 deletions src/components/BodyContent/BodyContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import classNames from 'classnames'
import PropTypes from 'prop-types'

import BodySchema from '../BodySchema/BodySchema'
import BodySchemaSwitcher from '../BodySchemaSwitcher/BodySchemaSwitcher'
import Example from '../Example/Example'

import './BodyContent.scss'
Expand All @@ -11,19 +12,22 @@ export default class BodyContent extends Component {
constructor (props) {
super(props)

this.setBodySchemaIndex = this.setBodySchemaIndex.bind(this)

this.state = {
tab: 'schema'
tab: 'schema',
index: 0
}
}

render () {
const { schema, examples } = this.props
const { tab, index } = this.state

const { tab } = this.state
return (
<div className='body-content'>
{schema && this.renderTabs(schema, examples)}
{tab === 'schema' && this.renderSchema(schema)}
{tab === 'schema' && this.renderSchema(schema, index)}
{tab === 'example' && this.renderExamples(examples)}
</div>
)
Expand Down Expand Up @@ -67,16 +71,19 @@ export default class BodyContent extends Component {
)
}

renderSchema (schema) {
renderSchema (schema, index) {
if (!schema) {
return null
}

// Peek at first item of `schema` to see if it's an array of possible
// schemas (eg. oneOf).
if (Array.isArray(schema[0])) {
return schema.map(
(schemaVariation, i) => <BodySchema key={i} properties={schemaVariation} styleVariation='odd' />
return (
<div className='body-content-switcher'>
<BodySchemaSwitcher options={schema} onChange={this.setBodySchemaIndex} />
<BodySchema properties={schema[index]} styleVariation='odd' />
</div>
)
}

Expand All @@ -90,6 +97,14 @@ export default class BodyContent extends Component {
<Example examples={examples} />
)
}

setBodySchemaIndex (bodySchemaIndex) {
const { index } = this.state

if (bodySchemaIndex !== index) {
this.setState({ index: bodySchemaIndex })
}
}
}

BodyContent.propTypes = {
Expand Down
5 changes: 0 additions & 5 deletions src/components/BodySchema/BodySchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React, { Component } from 'react'
import createFragment from 'react-addons-create-fragment'
import classNames from 'classnames'
import PropTypes from 'prop-types'
import isEqual from 'lodash/isEqual'

import Property from '../Property/Property'

Expand All @@ -19,10 +18,6 @@ export default class BodySchema extends Component {
}
}

shouldComponentUpdate (nextProps, nextState) {
return !isEqual(nextState.expandedProp, this.state.expandedProp)
}

render () {
const { properties, styleVariation } = this.props

Expand Down
38 changes: 38 additions & 0 deletions src/components/BodySchemaSwitcher/BodySchemaSwitcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React, { Component } from 'react'
import PropTypes from 'prop-types'

import './BodySchemaSwitcher.scss'

export default class BodySchemaSwitcher extends Component {
constructor (props) {
super(props)

this.handleChange = this.handleChange.bind(this)
}

handleChange (event) {
if (this.props.onChange) {
this.props.onChange(event.target.value)
}
}

render () {
const { options } = this.props

return (
<form className='body-schema-switcher-form'>
<label>This schema can be fulfilled by multiple options: </label>
<select onChange={this.handleChange}>
{options.map(
(option, i) => <option key={i} value={i}>{`Option ${i + 1}`}</option>
)}
</select>
</form>
)
}
}

BodySchemaSwitcher.propTypes = {
options: PropTypes.array.isRequired,
onChange: PropTypes.func
}
Empty file.

0 comments on commit 6823996

Please sign in to comment.