Skip to content

Commit

Permalink
Merge pull request #11868 from ajkl2533/11840
Browse files Browse the repository at this point in the history
ArgsTable: Fix union type splitting
  • Loading branch information
shilman committed Aug 28, 2020
2 parents 8f8b36f + f61dc8d commit 08a90e3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
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('|')} />;
}

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

0 comments on commit 08a90e3

Please sign in to comment.