Skip to content

Commit a289b01

Browse files
author
Brendan Abbott
committed
Clean up Response/Example components
1 parent de89655 commit a289b01

File tree

15 files changed

+194
-119
lines changed

15 files changed

+194
-119
lines changed

jest.config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ module.exports = {
77
'node_modules'
88
],
99
moduleNameMapper: {
10+
'balloon-css': '<rootDir>/src/__mocks__/styleMock.js',
1011
'\\.(css|scss)$': '<rootDir>/src/__mocks__/styleMock.js',
11-
'\\.(jpg|jpeg|png|gif|svg)$': '<rootDir>/src/__mocks__/styleMock.js'
12+
'\\.(jpg|jpeg|png|gif|svg)$': '<rootDir>/src/__mocks__/fileMock.js'
1213
},
1314
moduleFileExtensions: [
1415
'js',
@@ -17,4 +18,4 @@ module.exports = {
1718
],
1819
testEnvironment: 'node',
1920
verbose: true
20-
};
21+
}

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@
2424
"prop-types": "^15.5.8",
2525
"react": "^15.5.4",
2626
"react-addons-create-fragment": "^15.5.4",
27-
"react-addons-css-transition-group": "^15.5.2",
2827
"react-document-title": "^2.0.2",
2928
"react-dom": "^15.5.4",
30-
"react-json-view": "^1.7.5",
29+
"react-json-view": "^1.8.2",
3130
"react-redux": "^5.0.3",
3231
"react-router": "3.0.0",
3332
"react-router-redux": "^4.0.8",

src/components/BodyContent/BodyContent.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ export default class BodyContent extends Component {
1313

1414
this.renderTabs = this.renderTabs.bind(this)
1515
this.renderSchema = this.renderSchema.bind(this)
16-
this.renderExample = this.renderExample.bind(this)
16+
this.renderExamples = this.renderExamples.bind(this)
1717

1818
this.state = {
1919
tab: 'schema'
2020
}
2121
}
2222

2323
render () {
24-
const { schema, example, examples } = this.props
24+
const { schema, examples } = this.props
2525

2626
const { tab } = this.state
2727
return (
2828
<div className='body-content'>
29-
{schema && this.renderTabs(schema, example || examples)}
29+
{schema && this.renderTabs(schema, examples)}
3030
{tab === 'schema' && this.renderSchema(schema)}
31-
{tab === 'example' && this.renderExample(example, examples)}
31+
{tab === 'example' && this.renderExamples(examples)}
3232
</div>
3333
)
3434
}
@@ -38,7 +38,7 @@ export default class BodyContent extends Component {
3838
return (
3939
<div className='body-content-tabs'>
4040
{includeSchema && this.renderSchemaTab(currentTab)}
41-
{includeExample && this.renderExampleTab(currentTab)}
41+
{includeExample && this.renderExamplesTab(currentTab)}
4242
</div>
4343
)
4444
}
@@ -57,7 +57,7 @@ export default class BodyContent extends Component {
5757
)
5858
}
5959

60-
renderExampleTab (currentTab) {
60+
renderExamplesTab (currentTab) {
6161
return (
6262
<div
6363
role='button'
@@ -72,23 +72,23 @@ export default class BodyContent extends Component {
7272
}
7373

7474
renderSchema (schema) {
75-
if (schema) {
76-
return (
77-
<BodySchema properties={schema} styleVariation='odd' />
78-
)
75+
if (!schema) {
76+
return null
7977
}
80-
return null
78+
79+
return (
80+
<BodySchema properties={schema} styleVariation='odd' />
81+
)
8182
}
8283

83-
renderExample (example, examples) {
84+
renderExamples (examples) {
8485
return (
85-
<Example example={example} examples={examples} />
86+
<Example examples={examples} />
8687
)
8788
}
8889
}
8990

9091
BodyContent.propTypes = {
9192
schema: PropTypes.array,
92-
example: PropTypes.string,
9393
examples: PropTypes.array
9494
}

src/components/Example/Example.js

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,79 +13,57 @@ export default class Example extends Component {
1313
super(props)
1414

1515
this.onCopyClick = this.onCopyClick.bind(this)
16-
this.updateState = this.updateState.bind(this)
17-
this.renderString = this.renderString.bind(this)
18-
this.renderJson = this.renderJson.bind(this)
19-
20-
const { example, examples } = this.props
21-
let exampleToShow = example
22-
23-
if (!exampleToShow && examples && examples.length > 0) {
24-
exampleToShow = examples[0]
25-
}
2616

2717
this.state = {
2818
hovered: false,
29-
collapseAll: false,
30-
example: exampleToShow
19+
collapseAll: false
3120
}
3221
}
3322

3423
render () {
35-
const example = this.state.example
24+
const { examples } = this.props
25+
let example
3626

37-
if (example) {
38-
if (typeof example === 'string') {
39-
return this.renderString(example)
40-
} else {
41-
return this.renderJson(example)
42-
}
27+
// TODO: Handle multiple examples
28+
if (examples && examples.length) {
29+
example = examples[0]
4330
}
4431

45-
return null
46-
}
47-
48-
renderString (example) {
49-
return (
50-
<div className='body-content-example'>{example}</div>
51-
)
52-
}
32+
if (!example) {
33+
return null
34+
}
5335

54-
renderJson (example) {
5536
return (
5637
<div className='body-content-example'
57-
onMouseEnter={() => this.updateState({ hovered: true })}
58-
onMouseLeave={() => this.updateState({ hovered: false })}
38+
onMouseEnter={() => this.setState({ hovered: true })}
39+
onMouseLeave={() => this.setState({ hovered: false })}
5940
>
6041
<div className={classNames('action-buttons', {
6142
hovered: this.state.hovered
6243
})}>
6344
<CopyButton onCopyClick={this.onCopyClick} tooltip='Copy to Clipboard' />
64-
<span onClick={() => this.updateState({ collapseAll: false })}>Expand All</span>
65-
<span onClick={() => this.updateState({ collapseAll: true })}>Collapse All</span>
45+
<span onClick={() => this.setState({ collapseAll: false })}>Expand All</span>
46+
<span onClick={() => this.setState({ collapseAll: true })}>Collapse All</span>
6647
</div>
67-
<ReactJson src={example} theme='chalk' displayDataTypes={false} displayObjectSize={false}
68-
collapsed={this.state.collapseAll}
69-
enableClipboard={false} />
48+
{typeof example === 'string'
49+
? {example}
50+
: <ReactJson
51+
src={example}
52+
theme='chalk'
53+
displayDataTypes={false}
54+
displayObjectSize={false}
55+
collapsed={this.state.collapseAll}
56+
enableClipboard={false}
57+
/>}
7058
</div>
7159
)
7260
}
7361

74-
/**
75-
* Update internal state
76-
*
77-
* @param {Object} changedState
78-
*/
79-
updateState (changedState) {
80-
this.setState(Object.assign({}, this.state, changedState))
81-
}
82-
8362
onCopyClick () {
84-
copy(JSON.stringify(this.state.example, null, 2))
63+
copy(JSON.stringify(this.props.examples[0], null, 2))
8564
}
8665
}
8766

8867
Example.propTypes = {
89-
example: PropTypes.string,
9068
examples: PropTypes.array
9169
}

src/components/Method/Method.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,18 @@ export default class Method extends Component {
5757
}
5858

5959
renderRequest (request) {
60-
const { schema, example, examples } = request
60+
const { schema, examples } = request
6161

62-
if (request.schema) {
63-
return (
64-
<div className='method-request'>
65-
<h4>Request Body</h4>
66-
<BodyContent schema={schema} example={example} examples={examples} />
67-
</div>
68-
)
62+
if (!schema) {
63+
return null
6964
}
7065

71-
return ''
66+
return (
67+
<div className='method-request'>
68+
<h4>Request Body</h4>
69+
<BodyContent schema={schema} examples={examples} />
70+
</div>
71+
)
7272
}
7373

7474
renderResponses (responses) {

src/components/NavigationMethod/NavigationMethod.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export default class NavigationMethod extends Component {
1313
className={classNames('nav-method', {
1414
active: isActive
1515
})}
16-
key={method.link}
1716
href={`#${method.link}`}
1817
>
1918
<span className='method-type'>{method.type.toUpperCase()}</span>

src/components/NavigationTag/NavigationTag.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export default class NavigationTag extends Component {
3535
const isExpanded = (status === 'right')
3636

3737
return (
38-
<div key={title}>
38+
<div>
3939
<a
4040
className='nav-tag'
4141
href={`#${title}`}

src/components/Property/Property.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ $cell-padding: 10px;
124124

125125
.enum,
126126
.default {
127+
display: inline-block;
127128
padding: 0 5px;
128129
margin: 2px 3px;
129130
border: 1px solid grey;

src/components/PropertyConstraints/PropertyConstraints.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export default class PropertyConstraints extends PureComponent {
6161

6262
PropertyConstraints.propTypes = {
6363
type: PropTypes.arrayOf(PropTypes.string).isRequired,
64-
isRequired: PropTypes.bool.isRequired,
64+
isRequired: PropTypes.bool,
6565
constraints: PropTypes.shape({
6666
format: PropTypes.string,
6767
exclusiveMinimum: PropTypes.number,

src/components/Response/Response.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React, { Component } from 'react'
22
import classNames from 'classnames'
33
import PropTypes from 'prop-types'
4-
import ReactCSSTransitionGroup from 'react-addons-css-transition-group'
54

65
import BodyContent from '../BodyContent/BodyContent'
76
import Description from '../Description/Description'
@@ -13,42 +12,37 @@ export default class Response extends Component {
1312
constructor (props) {
1413
super(props)
1514

15+
this.onClick = this.onClick.bind(this)
16+
1617
this.state = {
1718
isOpen: false
1819
}
1920
}
2021

2122
render () {
2223
const { response } = this.props
23-
const { code, description, schema, example, examples } = response
24+
const { code, description, schema, examples } = response
2425
const { isOpen } = this.state
2526
let status
2627
if (isOpen) {
2728
status = 'open'
2829
}
2930

30-
const success = this.isSuccessCode(code)
31+
const successCode = this.isSuccessCode(code)
32+
const hasDetails = schema || examples
3133

3234
return (
3335
<div className='response'>
3436
<div className={classNames('response-info', {
35-
success: success,
36-
error: !success
37-
})} onClick={this.onClick.bind(this)}>
38-
<Indicator className='property-indicator' status={status} />
37+
success: successCode,
38+
error: !successCode
39+
})} onClick={hasDetails ? this.onClick : undefined}>
40+
{hasDetails && <Indicator status={status} />}
3941
<span className='response-code'>{code}</span>
4042
{description && <Description isInline description={description} />}
4143
</div>
42-
{isOpen &&
43-
<ReactCSSTransitionGroup
44-
transitionName='response-slide-toggle'
45-
transitionEnterTimeout={500}
46-
transitionLeaveTimeout={500}
47-
transitionAppear
48-
transitionAppearTimeout={500}
49-
>
50-
<BodyContent schema={schema} example={example} examples={examples} />
51-
</ReactCSSTransitionGroup>
44+
{hasDetails && isOpen &&
45+
<BodyContent schema={schema} examples={examples} />
5246
}
5347
</div>
5448
)
@@ -68,5 +62,10 @@ export default class Response extends Component {
6862
}
6963

7064
Response.propTypes = {
71-
response: PropTypes.object
65+
response: PropTypes.shape({
66+
code: PropTypes.string,
67+
description: PropTypes.string,
68+
schema: PropTypes.array,
69+
examples: PropTypes.array
70+
})
7271
}

0 commit comments

Comments
 (0)