-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
ArgRow.tsx
123 lines (111 loc) · 3.41 KB
/
ArgRow.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import React, { FC } from 'react';
import Markdown from 'markdown-to-jsx';
import { transparentize } from 'polished';
import { styled } from '@storybook/theming';
import { ArgType, Args, TableAnnotation } from './types';
import { ArgJsDoc } from './ArgJsDoc';
import { ArgValue } from './ArgValue';
import { ArgControl, ArgControlProps } from './ArgControl';
import { codeCommon } from '../../typography/shared';
export interface ArgRowProps {
row: ArgType;
arg: any;
updateArgs?: (args: Args) => void;
compact?: boolean;
expandable?: boolean;
initialExpandedArgs?: boolean;
}
const Name = styled.span({ fontWeight: 'bold' });
const Required = styled.span(({ theme }) => ({
color: theme.color.negative,
fontFamily: theme.typography.fonts.mono,
cursor: 'help',
}));
const Description = styled.div(({ theme }) => ({
'&&': {
p: {
margin: '0 0 10px 0',
},
a: {
color: theme.color.secondary,
},
},
code: {
...codeCommon({ theme }),
fontSize: 12,
fontFamily: theme.typography.fonts.mono,
},
'& code': {
margin: 0,
display: 'inline-block',
},
'& pre > code': {
whiteSpace: 'pre-wrap',
},
}));
const Type = styled.div<{ hasDescription: boolean }>(({ theme, hasDescription }) => ({
color:
theme.base === 'light'
? transparentize(0.1, theme.color.defaultText)
: transparentize(0.2, theme.color.defaultText),
marginTop: hasDescription ? 4 : 0,
}));
const TypeWithJsDoc = styled.div<{ hasDescription: boolean }>(({ theme, hasDescription }) => ({
color:
theme.base === 'light'
? transparentize(0.1, theme.color.defaultText)
: transparentize(0.2, theme.color.defaultText),
marginTop: hasDescription ? 12 : 0,
marginBottom: 12,
}));
const StyledTd = styled.td<{ expandable: boolean }>(({ theme, expandable }) => ({
paddingLeft: expandable ? '40px !important' : '20px !important',
}));
export const ArgRow: FC<ArgRowProps> = (props) => {
const { row, updateArgs, compact, expandable, initialExpandedArgs } = props;
const { name, description } = row;
const table = (row.table || {}) as TableAnnotation;
const type = table.type || row.type;
const defaultValue = table.defaultValue || row.defaultValue;
const required = row.type?.required;
const hasDescription = description != null && description !== '';
return (
<tr>
<StyledTd expandable={expandable}>
<Name>{name}</Name>
{required ? <Required title="Required">*</Required> : null}
</StyledTd>
{compact ? null : (
<td>
{hasDescription && (
<Description>
<Markdown>{description}</Markdown>
</Description>
)}
{table.jsDocTags != null ? (
<>
<TypeWithJsDoc hasDescription={hasDescription}>
<ArgValue value={type} initialExpandedArgs={initialExpandedArgs} />
</TypeWithJsDoc>
<ArgJsDoc tags={table.jsDocTags} />
</>
) : (
<Type hasDescription={hasDescription}>
<ArgValue value={type} initialExpandedArgs={initialExpandedArgs} />
</Type>
)}
</td>
)}
{compact ? null : (
<td>
<ArgValue value={defaultValue} initialExpandedArgs={initialExpandedArgs} />
</td>
)}
{updateArgs ? (
<td>
<ArgControl {...(props as ArgControlProps)} />
</td>
) : null}
</tr>
);
};