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

Component/interact #102

Merged
merged 23 commits into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion packages/styleguide/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
},
"lint-staged": {
"src/**/*.{js,jsx,json,css}": [
"prettier --single-quote --write",
"prettier --single-quote --trailing-comma --write",
"git add"
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ Convey meaning through color. ColorPalette is intended to display one color hue

## Props

<ComponentDocs title="<ColorPallete />" component={ColorPallete} />
<ComponentDocs title="<ColorPallete />" component={ColorPallete} />
1 change: 0 additions & 1 deletion packages/styleguide/src/components/ComponentDocs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ function getType(type) {
}

function getPropsData(props = {}) {
console.log('props:', props);
return Object.keys(props).reduce(
(allProps, prop) => [
...allProps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ const propTypes = {
/** Type label */
label: string.isRequired,
/** Array of values which will be displayed as InfoBadges */
values: array
values: array,
})
).isRequired,
/** Default map of available colors to types. Color must be defined in theme. */
typeToColorMap: BadgePropTypes.typeToColorMap
typeToColorMap: BadgePropTypes.typeToColorMap,
};

const ComponentInfo = ({ infoTypes, typeToColorMap, ...other }) => {
Expand All @@ -33,7 +33,7 @@ const ComponentInfo = ({ infoTypes, typeToColorMap, ...other }) => {
typeToColorMap={typeToColorMap}
{...other}
/>
</BarItem>
</BarItem>,
];
}

Expand All @@ -58,6 +58,6 @@ const StyledComponentInfo = styled.div`
`;

ComponentInfo.propTypes = propTypes;
ComponentInfo.defaultName = 'ComponentInfo';
ComponentInfo.displayName = 'ComponentInfo';

export default ComponentInfo;
53 changes: 52 additions & 1 deletion packages/styleguide/src/components/Preview/CodeExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,65 @@ import { renderToStaticMarkup } from 'react-dom/server';
import Code from '../Code/';
import Button from '../Button';

/**
* Remove props that are undefined or null
* Don't show React.Fragment in code example
*/
const cleanUpCode = markup => {
const markupProps = markup.props || {};

return Object.keys(markupProps).reduce((acc, curr) => {
let currProp = markupProps[curr];

let newProp;
// clean up child code
if (curr === 'children' && typeof currProp !== 'string') {
newProp = React.Children.map(currProp, child => {
// hide fragments if containing just strings
const isFragmentString =
child.type === React.Fragment &&
child.props &&
typeof child.props.children === 'string';

return isFragmentString
? child.props.children
: {
...child,
props: cleanUpCode(child)
};
});
}

// hide undefined or null props
const isNotDefined = [undefined, null].indexOf(currProp) !== -1;
return {
...acc,
...(isNotDefined
? {}
: {
[curr]: newProp || currProp
})
};
}, {});
};

const getJSXAsStringFromMarkup = (markup, options) => {
const { cleanProps, ...otherOptions } = options || {};

const reactElementToJSXStringOptions = {
showDefaultProps: false,
showFunctions: true,
functionValue: fn => fn.name,
...options
...otherOptions
};

if (cleanProps) {
markup = {
...markup,
props: cleanUpCode(markup)
};
}

// valid element can be passed to reactElementToJSXString directly
if (React.isValidElement(markup)) {
return reactElementToJSXString(markup, reactElementToJSXStringOptions);
Expand Down
Loading