Skip to content

Commit 20b8b6d

Browse files
committed
Expandable component
1 parent 9b85188 commit 20b8b6d

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

packages/html-reporter/src/links.tsx

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,33 @@ export const ProjectLink: React.FunctionComponent<{
6969
</Link>;
7070
};
7171

72+
export const Expandable: React.FunctionComponent<{
73+
summary: React.ReactNode,
74+
children: React.ReactNode,
75+
className?: string,
76+
style?: React.CSSProperties,
77+
}> = ({ summary, children, className, style }) => {
78+
const [expanded, setExpanded] = React.useState(false);
79+
80+
const handleToggle = (e: React.SyntheticEvent<HTMLDetailsElement>) => {
81+
setExpanded(e.currentTarget.open);
82+
};
83+
84+
return (
85+
<details
86+
style={style}
87+
className={className}
88+
onToggle={handleToggle}
89+
>
90+
<summary style={{ cursor: 'pointer', listStyle: 'none', whiteSpace: 'nowrap', paddingLeft: 4 }}>
91+
{expanded ? icons.downArrow() : icons.rightArrow()}
92+
{summary}
93+
</summary>
94+
{children}
95+
</details>
96+
);
97+
};
98+
7299
export const AttachmentLink: React.FunctionComponent<{
73100
attachment: TestAttachment,
74101
result: TestResult,
@@ -77,7 +104,6 @@ export const AttachmentLink: React.FunctionComponent<{
77104
openInNewTab?: boolean,
78105
}> = ({ attachment, result, href, linkName, openInNewTab }) => {
79106
const [flash, triggerFlash] = useFlash();
80-
const [expanded, setExpanded] = React.useState(false);
81107
useAnchor('attachment-' + result.attachments.indexOf(attachment), triggerFlash);
82108

83109
const summaryContent = (
@@ -117,20 +143,16 @@ export const AttachmentLink: React.FunctionComponent<{
117143
}
118144

119145
return (
120-
<details
146+
<Expandable
121147
style={{ lineHeight: '32px' }}
122148
className={clsx(flash && 'flash')}
123-
onToggle={e => setExpanded(e.currentTarget.open)}
149+
summary={summaryContent}
124150
>
125-
<summary style={{ cursor: 'pointer', listStyle: 'none', whiteSpace: 'nowrap', paddingLeft: 4 }}>
126-
{expanded ? icons.downArrow() : icons.rightArrow()}
127-
{summaryContent}
128-
</summary>
129151
<div className='attachment-body'>
130152
<CopyToClipboard value={attachment.body!}/>
131153
{linkifyText(attachment.body!)}
132154
</div>
133-
</details>
155+
</Expandable>
134156
);
135157
};
136158

0 commit comments

Comments
 (0)