Skip to content

Commit 4fd49fc

Browse files
committed
Refactor to a popover
1 parent 754ab49 commit 4fd49fc

File tree

2 files changed

+114
-40
lines changed

2 files changed

+114
-40
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import React, { useCallback, type PropsWithChildren } from 'react';
2+
3+
import {
4+
css,
5+
Icon,
6+
IconButton,
7+
InteractivePopover,
8+
Label,
9+
Overline,
10+
spacing,
11+
Toggle,
12+
Tooltip,
13+
useId,
14+
} from '@mongodb-js/compass-components';
15+
import type { ConnectionsFilter } from './use-filtered-connections';
16+
17+
const containerStyles = css({
18+
display: 'flex',
19+
flexDirection: 'column',
20+
alignItems: 'flex-start',
21+
padding: spacing[300],
22+
minWidth: 270,
23+
});
24+
25+
const closeButtonStyles = css({
26+
display: 'none',
27+
});
28+
29+
const groupStyles = css({
30+
display: 'flex',
31+
flexDirection: 'row',
32+
gap: spacing[200],
33+
marginTop: spacing[200],
34+
});
35+
36+
type ConnectionsFilterPopoverProps = PropsWithChildren<{
37+
open: boolean;
38+
setOpen: (open: boolean) => void;
39+
filter: ConnectionsFilter;
40+
onFilterChange(
41+
updater: (filter: ConnectionsFilter) => ConnectionsFilter
42+
): void;
43+
}>;
44+
45+
export default function ConnectionsFilterPopover({
46+
open,
47+
setOpen,
48+
filter,
49+
onFilterChange,
50+
}: ConnectionsFilterPopoverProps) {
51+
const onExcludeInactiveChange = useCallback(
52+
(excludeInactive: boolean) => {
53+
onFilterChange((filter) => ({
54+
...filter,
55+
excludeInactive,
56+
}));
57+
},
58+
[onFilterChange]
59+
);
60+
61+
const excludeInactiveId = useId('Sort by');
62+
63+
return (
64+
<InteractivePopover
65+
open={open}
66+
setOpen={setOpen}
67+
closeButtonClassName={closeButtonStyles}
68+
containerClassName={containerStyles}
69+
trigger={({ onClick, children, ref }) => (
70+
<>
71+
<Tooltip
72+
align="right"
73+
enabled={!open}
74+
trigger={
75+
<IconButton
76+
onClick={onClick}
77+
active={open}
78+
aria-label="Filter connections"
79+
ref={ref as React.Ref<unknown>}
80+
>
81+
{/* TODO: Show a small blue circle when filter.excludeInactive is enabled */}
82+
<Icon glyph="Filter" />
83+
</IconButton>
84+
}
85+
>
86+
Filter connections
87+
</Tooltip>
88+
{children}
89+
</>
90+
)}
91+
>
92+
<Overline>Filter Options</Overline>
93+
<div className={groupStyles}>
94+
<Toggle
95+
aria-labelledby={excludeInactiveId}
96+
checked={filter.excludeInactive}
97+
onChange={onExcludeInactiveChange}
98+
size="small"
99+
/>
100+
<Label htmlFor={excludeInactiveId}>Show only active connections</Label>
101+
</div>
102+
</InteractivePopover>
103+
);
104+
}

packages/compass-sidebar/src/components/navigation-items-filter.tsx

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
import React, { useCallback } from 'react';
2-
import {
3-
ConnectedPlugsIcon,
4-
css,
5-
DisconnectedPlugIcon,
6-
IconButton,
7-
spacing,
8-
TextInput,
9-
Tooltip,
10-
} from '@mongodb-js/compass-components';
1+
import React, { useCallback, useState } from 'react';
2+
import { css, spacing, TextInput } from '@mongodb-js/compass-components';
113
import type { ConnectionsFilter } from './use-filtered-connections';
4+
import ConnectionsFilterPopover from './connections-filter-popover';
125

136
const filterContainerStyles = css({
147
display: 'flex',
@@ -54,12 +47,7 @@ export default function NavigationItemsFilter({
5447
[onFilterChange]
5548
);
5649

57-
const toggleExcludeInactive = useCallback(() => {
58-
onFilterChange((filter) => ({
59-
...filter,
60-
excludeInactive: !filter.excludeInactive,
61-
}));
62-
}, [onFilterChange]);
50+
const [isPopoverOpen, setPopoverOpen] = useState(false);
6351

6452
const onSubmit = useCallback((evt) => {
6553
evt.preventDefault();
@@ -78,30 +66,12 @@ export default function NavigationItemsFilter({
7866
onChange={onChange}
7967
/>
8068
</form>
81-
<Tooltip
82-
justify="middle"
83-
trigger={
84-
<IconButton
85-
onClick={toggleExcludeInactive}
86-
active={filter.excludeInactive}
87-
aria-label={
88-
filter.excludeInactive
89-
? 'Showing active connections'
90-
: 'Showing all connections'
91-
}
92-
>
93-
{filter.excludeInactive ? (
94-
<ConnectedPlugsIcon />
95-
) : (
96-
<DisconnectedPlugIcon />
97-
)}
98-
</IconButton>
99-
}
100-
>
101-
{filter.excludeInactive
102-
? 'Showing active connections'
103-
: 'Showing all connections'}
104-
</Tooltip>
69+
<ConnectionsFilterPopover
70+
open={isPopoverOpen}
71+
setOpen={setPopoverOpen}
72+
filter={filter}
73+
onFilterChange={onFilterChange}
74+
/>
10575
</div>
10676
);
10777
}

0 commit comments

Comments
 (0)