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

Addon info prop options #3428

Merged
merged 7 commits into from
Apr 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
625 changes: 375 additions & 250 deletions addons/info/src/__snapshots__/index.test.js.snap

Large diffs are not rendered by default.

164 changes: 151 additions & 13 deletions addons/info/src/components/PropVal.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,45 +38,157 @@ const getValueStyles = (codeColors = {}) => ({
},
});

function previewArray(val, maxPropArrayLength, valueStyles) {
function indent(breakIntoNewLines, level, isBlock) {
return (
breakIntoNewLines && (
<span>
<br />
{`${Array(level).join(' ')} `}
{!isBlock && ' '}
</span>
)
);
}

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

return (
<span style={valueStyles.array}>
[{createFragment(items)}
{indent(breakIntoNewLines, level, true)}]
</span>
);
}

function previewObject(val, maxPropObjectKeys, valueStyles) {
PreviewArray.propTypes = {
val: PropTypes.any, // eslint-disable-line
maxPropArrayLength: PropTypes.number.isRequired,
maxPropStringLength: PropTypes.number.isRequired,
maxPropsIntoLine: PropTypes.number.isRequired,
level: PropTypes.number.isRequired,
valueStyles: PropTypes.shape({
func: PropTypes.object,
attr: PropTypes.object,
object: PropTypes.object,
array: PropTypes.object,
number: PropTypes.object,
string: PropTypes.object,
bool: PropTypes.object,
empty: PropTypes.object,
}).isRequired,
};

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

PreviewObject.propTypes = {
val: PropTypes.any, // eslint-disable-line
maxPropObjectKeys: PropTypes.number.isRequired,
maxPropStringLength: PropTypes.number.isRequired,
maxPropsIntoLine: PropTypes.number.isRequired,
level: PropTypes.number.isRequired,
valueStyles: PropTypes.shape({
func: PropTypes.object,
attr: PropTypes.object,
object: PropTypes.object,
array: PropTypes.object,
number: PropTypes.object,
string: PropTypes.object,
bool: PropTypes.object,
empty: PropTypes.object,
}).isRequired,
};

function PropVal(props) {
const { maxPropObjectKeys, maxPropArrayLength, maxPropStringLength, theme } = props;
const {
level,
maxPropObjectKeys,
maxPropArrayLength,
maxPropStringLength,
maxPropsIntoLine,
theme,
} = props;
let { val } = props;
const { codeColors } = theme || {};
let braceWrap = true;
Expand All @@ -94,7 +206,18 @@ function PropVal(props) {
} else if (typeof val === 'boolean') {
content = <span style={valueStyles.bool}>{`${val}`}</span>;
} else if (Array.isArray(val)) {
content = previewArray(val, maxPropArrayLength, valueStyles);
content = (
<PreviewArray
{...{
val,
level,
maxPropArrayLength,
maxPropStringLength,
maxPropsIntoLine,
valueStyles,
}}
/>
);
} else if (typeof val === 'function') {
content = <span style={valueStyles.func}>{val.name ? `${val.name}()` : 'anonymous()'}</span>;
} else if (!val) {
Expand All @@ -108,7 +231,18 @@ function PropVal(props) {
</span>
);
} else {
content = previewObject(val, maxPropObjectKeys, valueStyles);
content = (
<PreviewObject
{...{
val,
level,
maxPropObjectKeys,
maxPropStringLength,
maxPropsIntoLine,
valueStyles,
}}
/>
);
}

if (!braceWrap) return content;
Expand All @@ -121,6 +255,8 @@ PropVal.defaultProps = {
maxPropObjectKeys: 3,
maxPropArrayLength: 3,
maxPropStringLength: 50,
maxPropsIntoLine: 3,
level: 1,
theme: {},
valueStyles: null,
};
Expand All @@ -130,6 +266,8 @@ PropVal.propTypes = {
maxPropObjectKeys: PropTypes.number,
maxPropArrayLength: PropTypes.number,
maxPropStringLength: PropTypes.number,
maxPropsIntoLine: PropTypes.number,
level: PropTypes.number,
theme: PropTypes.shape({
codeColors: PropTypes.object,
}),
Expand Down
1 change: 1 addition & 0 deletions addons/info/src/components/Props.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export default function Props(props) {
maxPropObjectKeys={maxPropObjectKeys}
maxPropArrayLength={maxPropArrayLength}
maxPropStringLength={maxPropStringLength}
maxPropsIntoLine={maxPropsIntoLine}
/>
{typeof nodeProps[name] === 'string' && '"'}
</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,10 +530,12 @@ exports[`Storyshots Button with new info 1`] = `
style="color:#666"
>
{
<span
style="color:#666"
>
isOld
<span>
<span
style="color:#666"
>
isOld
</span>
</span>
:
<span>
Expand All @@ -545,11 +547,13 @@ exports[`Storyshots Button with new info 1`] = `
</span>
}
</span>
,
<span
style="color:#666"
>
value
,
<span>
<span
style="color:#666"
>
value
</span>
</span>
:
<span>
Expand Down Expand Up @@ -942,10 +946,12 @@ exports[`Storyshots Button with some info 1`] = `
style="color:#666"
>
{
<span
style="color:#666"
>
isOld
<span>
<span
style="color:#666"
>
isOld
</span>
</span>
:
<span>
Expand All @@ -957,11 +963,13 @@ exports[`Storyshots Button with some info 1`] = `
</span>
}
</span>
,
<span
style="color:#666"
>
value
,
<span>
<span
style="color:#666"
>
value
</span>
</span>
:
<span>
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.