Skip to content
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

Fix semi broken __docgenInfo integration in addon info #1030

Merged
merged 16 commits into from
May 19, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions addons/info/src/components/PropTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ export default class PropTable extends React.Component {
? type.__docgenInfo.props[property].description
: null;
if (propType === 'other') {
if (type.__docgenInfo && type.__docgenInfo.props && type.__docgenInfo.props[property]) {
propType = type.__docgenInfo.props[property].type;
if (type.__docgenInfo && type.__docgenInfo.props && type.__docgenInfo.props[property] && type.__docgenInfo.props[property].type) {
propType = type.__docgenInfo.props[property].type.name;
}
}
props[property] = { property, propType, required, description };
Expand Down
96 changes: 48 additions & 48 deletions addons/info/src/components/PropVal.js
Original file line number Diff line number Diff line change
@@ -1,119 +1,119 @@
import React from 'react';
import createFragment from 'react-addons-create-fragment';
import React from 'react'
import createFragment from 'react-addons-create-fragment'

const valueStyles = {
func: {
color: '#170',
color: '#170'
},

attr: {
color: '#666',
color: '#666'
},

object: {
color: '#666',
color: '#666'
},

array: {
color: '#666',
color: '#666'
},

number: {
color: '#a11',
color: '#a11'
},

string: {
color: '#22a',
wordBreak: 'break-word',
wordBreak: 'break-word'
},

bool: {
color: '#a11',
color: '#a11'
},

empty: {
color: '#777',
},
};
color: '#777'
}
}

function previewArray(val) {
const items = {};
function previewArray (val) {
const items = {}
val.slice(0, 3).forEach((item, i) => {
items[`n${i}`] = <PropVal val={item} />;
items[`c${i}`] = ', ';
});
items[`n${i}`] = <PropVal val={item} />
items[`c${i}`] = ', '
})
if (val.length > 3) {
items.last = '…';
items.last = '…'
} else {
delete items[`c${val.length - 1}`];
delete items[`c${val.length - 1}`]
}
return (
<span style={valueStyles.array}>
[{createFragment(items)}]
</span>
);
)
}

function previewObject(val) {
const names = Object.keys(val);
const items = {};
function previewObject (val) {
const names = Object.keys(val)
const items = {}
names.slice(0, 3).forEach((name, i) => {
items[`k${i}`] = <span style={valueStyles.attr}>{name}</span>;
items[`c${i}`] = ': ';
items[`v${i}`] = <PropVal val={val[name]} />;
items[`m${i}`] = ', ';
});
items[`k${i}`] = <span style={valueStyles.attr}>{name}</span>
items[`c${i}`] = ': '
items[`v${i}`] = <PropVal val={val[name]} />
items[`m${i}`] = ', '
})
if (names.length > 3) {
items.rest = '…';
items.rest = '…'
} else {
delete items[`m${names.length - 1}`];
delete items[`m${names.length - 1}`]
}
return (
<span style={valueStyles.object}>
{'{'}{createFragment(items)}{'}'}
</span>
);
)
}

function previewProp(val) {
let braceWrap = true;
let content = null;
function previewProp (val) {
let braceWrap = true
let content = null
if (typeof val === 'number') {
content = <span style={valueStyles.number}>{val}</span>;
content = <span style={valueStyles.number}>{val}</span>
} else if (typeof val === 'string') {
if (val.length > 50) {
val = `${val.slice(0, 50)}…`;
val = `${val.slice(0, 50)}…`
}
content = <span style={valueStyles.string}>"{val}"</span>;
braceWrap = false;
content = <span style={valueStyles.string}>"{val}"</span>
braceWrap = false
} else if (typeof val === 'boolean') {
content = <span style={valueStyles.bool}>{`${val}`}</span>;
content = <span style={valueStyles.bool}>{`${val}`}</span>
} else if (Array.isArray(val)) {
content = previewArray(val);
content = previewArray(val)
} else if (typeof val === 'function') {
content = <span style={valueStyles.func}>{val.name ? `${val.name}()` : 'anonymous()'}</span>;
content = <span style={valueStyles.func}>{val.name ? `${val.name}()` : 'anonymous()'}</span>
} else if (!val) {
content = <span style={valueStyles.empty}>{`${val}`}</span>;
content = <span style={valueStyles.empty}>{`${val}`}</span>
} else if (typeof val !== 'object') {
content = <span>…</span>;
content = <span>…</span>
} else if (React.isValidElement(val)) {
content = (
<span style={valueStyles.object}>
{`<${val.type.displayName || val.type.name || val.type} />`}
</span>
);
)
} else {
content = previewObject(val);
content = previewObject(val)
}

if (!braceWrap) return content;
return <span>{content}</span>;
}

export default class PropVal extends React.Component {
render() {
return previewProp(this.props.val);
render () {
return previewProp(this.props.val)
}
}

module.exports = PropVal;
module.exports = PropVal
20 changes: 20 additions & 0 deletions addons/info/src/components/Story.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export default class Story extends React.Component {
<div style={this.state.stylesheet.infoPage}>
<div style={this.state.stylesheet.infoBody}>
{this._getInfoContent()}
{this._getComponentDescription()}
{this._getSourceCode()}
{this._getPropTables()}
</div>
Expand Down Expand Up @@ -162,6 +163,7 @@ export default class Story extends React.Component {
<div style={this.state.stylesheet.infoBody}>
{this._getInfoHeader()}
{this._getInfoContent()}
{this._getComponentDescription()}
{this._getSourceCode()}
{this._getPropTables()}
</div>
Expand Down Expand Up @@ -214,6 +216,24 @@ export default class Story extends React.Component {
);
}

_getComponentDescription() {
let retDiv = null;

if (Object.keys(window.STORYBOOK_REACT_CLASSES).length) {
Object.keys(window.STORYBOOK_REACT_CLASSES).forEach((key, index) => {
if (window.STORYBOOK_REACT_CLASSES[key].name === this.props.context.kind) {
retDiv = (
<div>
{window.STORYBOOK_REACT_CLASSES[key].docgenInfo.description}
</div>
);
}
});
}

return retDiv;
}

_getSourceCode() {
if (!this.props.showSource) {
return null;
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
},
"scripts": {
"bootstrap": "lerna bootstrap",
"postinstall": "lerna bootstrap --hoist",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah never mind then

"lint": "eslint .",
"test": "jest",
"test:watch": "npm test -- --watch",
Expand Down