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

ArgsTable: Fix union type splitting #11868

Merged
merged 4 commits into from Aug 28, 2020
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
18 changes: 18 additions & 0 deletions lib/components/src/blocks/ArgsTable/ArgRow.stories.tsx
Expand Up @@ -298,6 +298,24 @@ LongEnum.args = {
},
};

export const complexUnion =
'((a: string | SVGSVGElement) => void) | RefObject<SVGSVGElement | number> | [a|b] | {a|b}';

export const ComplexUnion = Template.bind({});
ComplexUnion.args = {
...baseArgs,
row: {
key: 'complexUnion',
name: 'Complex',
type: { required: true },
table: {
type: {
summary: complexUnion,
},
},
},
};

export const Markdown = Template.bind({});
Markdown.args = {
...baseArgs,
Expand Down
22 changes: 17 additions & 5 deletions lib/components/src/blocks/ArgsTable/ArgValue.tsx
Expand Up @@ -15,6 +15,7 @@ interface ArgValueProps {

interface ArgTextProps {
text: string;
simple?: boolean;
}

interface ArgSummaryProps {
Expand All @@ -33,19 +34,24 @@ const Summary = styled.div<{ isExpanded?: boolean }>(({ isExpanded }) => ({
minWidth: 100,
}));

const Text = styled.span<{}>(codeCommon, ({ theme }) => ({
const Text = styled.span<{ simple?: boolean }>(codeCommon, ({ theme, simple = false }) => ({
flex: '0 0 auto',
fontFamily: theme.typography.fonts.mono,
fontSize: theme.typography.size.s1,
wordBreak: 'break-word',
whiteSpace: 'normal',
maxWidth: '100%',
margin: 0,
marginRight: '4px',
marginBottom: '4px',
paddingTop: '2px',
paddingBottom: '2px',
lineHeight: '13px',
whiteSpace: 'normal',
maxWidth: '100%',
...(simple && {
background: 'transparent',
border: '0 none',
paddingLeft: 0,
}),
}));

const ExpandButton = styled.button<{}>(({ theme }) => ({
Expand Down Expand Up @@ -93,8 +99,8 @@ const EmptyArg = () => {
return <span>-</span>;
};

const ArgText: FC<ArgTextProps> = ({ text }) => {
return <Text>{text}</Text>;
const ArgText: FC<ArgTextProps> = ({ text, simple }) => {
return <Text simple={simple}>{text}</Text>;
};

const calculateDetailWidth = memoize(1000)((detail: string): string => {
Expand Down Expand Up @@ -132,6 +138,12 @@ const ArgSummary: FC<ArgSummaryProps> = ({ value, initialExpandedArgs }) => {
const summaryAsString = typeof summary.toString === 'function' ? summary.toString() : summary;

if (detail == null) {
const cannotBeSafelySplitted = /[(){}[\]<>]/.test(summaryAsString);

if (cannotBeSafelySplitted) {
return <ArgText text={summaryAsString} simple={summaryAsString.includes('|')} />;
Copy link
Member

Choose a reason for hiding this comment

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

Can you explain simple to me? Don't understand why we need it.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry I have a vacation. Simple is turning on and off styling of values (border, background...). And I used it because when you have non splitted values it looked strange with styling.

}

const summaryItems = getSummaryItems(summaryAsString);
const itemsCount = summaryItems.length;
const hasManyItems = itemsCount > ITEMS_BEFORE_EXPANSION;
Expand Down