-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathSection.tsx
More file actions
165 lines (152 loc) · 4.91 KB
/
Copy pathSection.tsx
File metadata and controls
165 lines (152 loc) · 4.91 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
import * as React from 'react';
import styled, { css } from 'styled-components';
import { Collapse } from '@mantine/core';
import useDisclosure from 'util/hooks/useDisclosure';
import sizeForMantine from 'theme/utils/sizeForMantine';
import type { BsSize } from 'components/bootstrap/types';
import IconButton from './IconButton';
const Container = styled.div<{ $collapsible: boolean; $opened: boolean; $bsSize: BsSize }>(
({ $collapsible, $opened, $bsSize, theme }) => css`
background-color: ${theme.colors.section.filled.background};
border: 1px solid ${theme.colors.section.filled.border};
border-radius: 10px;
padding: ${$collapsible && !$opened ? 0 : theme.spacings[sizeForMantine($bsSize)]};
margin-bottom: ${theme.spacings.xxs};
`,
);
const Header = styled.div<{ $collapsible: boolean; $opened: boolean; $bsSize: BsSize }>(
({ $collapsible, $opened, $bsSize, theme }) => css`
display: flex;
justify-content: space-between;
gap: ${theme.spacings.xs};
align-items: center;
border-radius: 10px;
padding: ${$collapsible && !$opened ? theme.spacings[sizeForMantine($bsSize)] : 0};
flex-wrap: wrap;
&:hover {
background-color: ${$collapsible && !$opened ? theme.colors.table.row.backgroundHover : 'initial'};
}
`,
);
const FlexWrapper = styled.div(
({ theme }) => css`
display: flex;
justify-content: flex-start;
gap: ${theme.spacings.sm};
align-items: center;
`,
);
type Props = React.PropsWithChildren<{
title: string;
titleAs?: 'h2' | 'h3' | 'h4' | 'h5';
header?: React.ReactNode;
actions?: React.ReactNode;
preHeaderSection?: React.ReactNode;
headerLeftSection?: React.ReactNode;
collapsible?: boolean;
onCollapse?: (opened?: boolean) => void;
defaultClosed?: boolean;
disableCollapseButton?: boolean;
collapseButtonPosition?: 'left' | 'right';
className?: string;
bsSize?: BsSize;
}>;
/**
* Simple section component. Currently only a "filled" version exists.
*/
const Section = ({
title,
titleAs: TitleAs = 'h2',
header = null,
actions = null,
preHeaderSection = null,
headerLeftSection = null,
collapsible = false,
defaultClosed = false,
onCollapse = () => {},
disableCollapseButton = false,
collapseButtonPosition = 'left',
children = null,
className = undefined,
bsSize = 'md',
}: Props) => {
const [opened, { toggle }] = useDisclosure(!defaultClosed);
const onToggle = () => {
toggle();
onCollapse(opened);
};
const onHeaderClick = () => !disableCollapseButton && onToggle();
const collapseButton = collapsible && (
<IconButton
data-testid="collapseButton"
title={`Toggle ${title.toLowerCase()} section`}
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
if (!disableCollapseButton) {
onToggle();
}
}}
disabled={disableCollapseButton}
name={opened ? 'keyboard_arrow_up' : 'keyboard_arrow_down'}
size="lg"
/>
);
return (
<Container $opened={opened} $collapsible={collapsible} className={className} $bsSize={bsSize}>
<Header $opened={opened} $collapsible={collapsible} onClick={onHeaderClick} $bsSize={bsSize}>
<FlexWrapper>
{collapseButtonPosition === 'left' && collapseButton}
{preHeaderSection && (
<FlexWrapper
onClick={(e) => {
e.stopPropagation();
}}>
{preHeaderSection}
</FlexWrapper>
)}
<TitleAs>{header ?? title}</TitleAs>
{headerLeftSection && (
<FlexWrapper
onClick={(e) => {
e.stopPropagation();
}}>
{headerLeftSection}
</FlexWrapper>
)}
</FlexWrapper>
<FlexWrapper>
{actions && (
// eslint-disable-next-line jsx-a11y/click-events-have-key-events,jsx-a11y/no-static-element-interactions
<div
onClick={(e) => {
e.stopPropagation();
}}>
{actions}
</div>
)}
{collapseButtonPosition === 'right' && collapseButton}
</FlexWrapper>
</Header>
{!collapsible && children}
{collapsible && <Collapse in={opened}>{children}</Collapse>}
</Container>
);
};
export default Section;