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

print shape prop values in docs #50

Merged
merged 2 commits into from
Dec 17, 2018
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
39 changes: 17 additions & 22 deletions packages/styleguide/src/components/ComponentDocs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ function printEnums(values) {
`;
}

function printShape(values) {
const keyValues = Object.keys(values)
.map(name => name + ': ' + getType(values[name]))
.join(', \r\n');

return codeBlock`
\`\`\`js
{
${keyValues.replace(/```js\n/g, '').replace(/\s+}\n```/g, '\n}')}
}
\`\`\`
`;
}

/* eslint-disable no-use-before-define */
function printTypeOf(value, of) {
return codeBlock`
Expand All @@ -81,6 +95,8 @@ function printType(type) {
switch (type.name) {
case 'enum':
return printEnums(type.value);
case 'shape':
return printShape(type.value);
case 'instanceOf':
return printTypeOf(type.value, 'instance');
case 'arrayOf':
Expand All @@ -103,24 +119,6 @@ function getType(type) {
}
}

function getShapePropsData(name, props = {}) {
return Object.keys(props).reduce(
(acc, prop) => ({
...acc,
[`${name}.${prop}`]: {
type: {
name: props[prop].name,
value: props[prop].value
},
defaultValue: props[prop].defaultValue,
description: props[prop].description,
required: props[prop].required
}
}),
{}
);
}

function getPropsData(props = {}) {
return Object.keys(props).reduce(
(allProps, prop) => [
Expand All @@ -130,10 +128,7 @@ function getPropsData(props = {}) {
type: getType(props[prop].type),
default: props[prop].defaultValue && props[prop].defaultValue.value,
description: props[prop].description
},
...(props[prop].type.name === 'shape'
? getPropsData(getShapePropsData(prop, props[prop].type.value))
: [])
}
],
[]
);
Expand Down