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 crashes when DefaultProp isn't a string #1510

Merged
merged 2 commits into from
Jan 6, 2020
Merged
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
7 changes: 4 additions & 3 deletions src/client/rsg-components/Props/renderDefault.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ export default function renderDefault(prop: PropDescriptorWithFlow): React.React
// Workaround for issue https://github.com/reactjs/react-docgen/issues/221
// If prop has defaultValue it can not be required
if (prop.defaultValue) {
const defaultValueString = typeof prop.defaultValue.value === 'string' ? showSpaces(unquote(prop.defaultValue.value)) : showSpaces(String(prop.defaultValue.value));
Copy link
Member

Choose a reason for hiding this comment

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

I guess we don't really need a condition here?

Suggested change
const defaultValueString = typeof prop.defaultValue.value === 'string' ? showSpaces(unquote(prop.defaultValue.value)) : showSpaces(String(prop.defaultValue.value));
const defaultValue = showSpaces(unquote(String(prop.defaultValue.value)));

Copy link
Contributor Author

@Lazyuki Lazyuki Jan 3, 2020

Choose a reason for hiding this comment

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

Yeah that works too. Although I just realized if the defaultValue isn't a primitive type like an object it would probably show [object Object] as the default prop?

EDIT: nvm if it's an object it gets evaluated correctly I guess

if (prop.type || prop.flowType) {
const propName = prop.type ? prop.type.name : prop.flowType && prop.flowType.type;

if (defaultValueBlacklist.indexOf(prop.defaultValue.value) > -1) {
return <Code>{showSpaces(unquote(prop.defaultValue.value))}</Code>;
return <Code>{defaultValueString}</Code>;
} else if (propName === 'func' || propName === 'function') {
return (
<Text
size="small"
color="light"
underlined
title={showSpaces(unquote(prop.defaultValue.value))}
title={defaultValueString}
>
Function
</Text>
Expand Down Expand Up @@ -50,7 +51,7 @@ export default function renderDefault(prop: PropDescriptorWithFlow): React.React
}
}

return <Code>{showSpaces(unquote(prop.defaultValue.value))}</Code>;
return <Code>{defaultValueString}</Code>;
} else if (prop.required) {
return (
<Text size="small" color="light">
Expand Down