Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Counter to dropdown menu in tools Page #1479

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pages/tools/components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,15 @@ export default function Sidebar({
<SearchBar transform={transform} />
{filters.map(({ label, accessorKey }) => {
const checkedValues = transform[accessorKey as keyof Transform] || [];
const selectedCount = checkedValues.length;

return (
<DropdownMenu key={accessorKey} label={label} icon={<FilterIcon />}>
<DropdownMenu
key={accessorKey}
label={label}
icon={<FilterIcon />}
selectedCount={selectedCount}
>
{filterCriteria[accessorKey as FilterCriteriaFields]
?.map(String)
.map((filterOption) => (
Expand Down
39 changes: 35 additions & 4 deletions pages/tools/components/ui/DropdownMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,52 @@ import classnames from 'classnames';
import { useRouter } from 'next/router';
import React, {
type ReactElement,
type ReactNode,
useEffect,
useState,
Children,
cloneElement,
isValidElement,
} from 'react';

interface DropdownMenuProps {
children: ReactNode;
children: React.ReactNode;
label: string;
icon: ReactElement;
selectedCount?: number;
keepOpenOnSelection?: boolean;
}

export default function DropdownMenu({
children,
label,
icon,
selectedCount = 0,
keepOpenOnSelection = true,
}: DropdownMenuProps) {
const [isDropdownOpen, setIsDropdownOpen] = useState(false);
const router = useRouter();

// Listen for changes in children's checked state
const enhancedChildren = Children.map(children, (child) => {
if (isValidElement(child) && child.type === 'Checkbox') {
return cloneElement(child, {
...child.props,
onChange: (e: React.ChangeEvent<HTMLInputElement>) => {
if (child.props.onChange) {
child.props.onChange(e);
}

// If we're not keeping the dropdown open, close it after selection
if (!keepOpenOnSelection) {
setIsDropdownOpen(false);
}
},
});
}
return child;
});

// Reset dropdown state on route change
useEffect(() => {
setIsDropdownOpen(false);
}, [router]);
Expand All @@ -39,6 +66,11 @@ export default function DropdownMenu({
<div className='text-slate-900 dark:text-slate-300 font-bold mr-auto'>
{label}
</div>
{selectedCount > 0 && (
<div className='text-slate-900 dark:text-slate-300 font-bold border-white border-2 rounded-full px-2 mr-2'>
{selectedCount > 0 && `${selectedCount}`}
</div>
)}
<svg
style={{
transform: `${isDropdownOpen ? 'rotate(180deg)' : 'rotate(0)'}`,
Expand All @@ -60,7 +92,6 @@ export default function DropdownMenu({
/>
</svg>
</div>

<div
className={classnames(
'tools-dropdown-menu',
Expand All @@ -71,7 +102,7 @@ export default function DropdownMenu({
},
)}
>
{children}
{enhancedChildren}
</div>
</div>
);
Expand Down