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

fix: add stop propagation when popover scrolls inside a dialog #55

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
200 changes: 79 additions & 121 deletions src/components/combobox/combobox.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"use client";
'use client';

import * as React from "react";
import { CheckIcon, CaretDownIcon, CaretUpIcon } from "@stash-ui/regular-icons";
import * as React from 'react';
import { CheckIcon, CaretDownIcon, CaretUpIcon } from '@stash-ui/regular-icons';

import { cn } from "@/lib/utils";
import { Button } from "@/components/button";
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem } from "@/components/command";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/popover";
import { cn } from '@/lib/utils';
import { Button } from '@/components/button';
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem } from '@/components/command';
import { Popover, PopoverContent, PopoverTrigger } from '@/components/popover';

interface Item {
value: string;
Expand All @@ -31,7 +31,7 @@ interface ComboboxProps {
placeholder?: string;
searchPlaceholder?: string;
emptySearchPlaceholder?: string;
variant?: "default" | "detailed" | "icon-compact" | "image-detailed";
variant?: 'default' | 'detailed' | 'icon-compact' | 'image-detailed';
defaultValue?: string;
searchValue?: string;
onSelect?: (item: Item) => void;
Expand All @@ -47,32 +47,32 @@ export function Combobox({
helperText,
options,
shouldFilter = true,
variant = "default",
placeholder = "Select an item...",
searchPlaceholder = "Search...",
emptySearchPlaceholder = "Nothing found.",
variant = 'default',
placeholder = 'Select an item...',
searchPlaceholder = 'Search...',
emptySearchPlaceholder = 'Nothing found.',
defaultValue,
searchValue,
onChangeSearchValue,
onSelect,
onEndReached,
className,
shouldFilterFalseEmptyContent,
shouldFilterFalseEmptyContent
}: ComboboxProps) {
const [open, setOpen] = React.useState(false);
const [value, setValue] = React.useState("");
const [value, setValue] = React.useState('');

const isEmpty = options.every((option) => option.items.length === 0);

const lastItemRef = React.useRef<HTMLDivElement>(null);

React.useEffect(() => {
setValue(defaultValue || "");
setValue(defaultValue || '');
}, [defaultValue]);

React.useEffect(() => {
if (!open) {
if (onChangeSearchValue) onChangeSearchValue("");
if (onChangeSearchValue) onChangeSearchValue('');
}
}, [open]);

Expand Down Expand Up @@ -100,55 +100,25 @@ export function Combobox({
};
}, [lastItemRef.current, options, open]);

const DefaultVariant = ({
item,
selected,
isButtonLabel,
}: {
item: Item;
selected: boolean;
isButtonLabel?: boolean;
}) => (
<div
className={cn(
"flex items-center h-full w-full",
selected && "justify-between",
isButtonLabel && "w-[calc(100%-30px)]"
)}
>
<span
className={`line-clamp-1 ${isButtonLabel ? "w-full h-full flex items-center" : ""} ${
selected ? "text-visible" : ""
}`}
>
const DefaultVariant = ({ item, selected, isButtonLabel }: { item: Item; selected: boolean; isButtonLabel?: boolean }) => (
<div className={cn('flex items-center h-full w-full', selected && 'justify-between', isButtonLabel && 'w-[calc(100%-30px)]')}>
<span className={`line-clamp-1 ${isButtonLabel ? 'w-full h-full flex items-center' : ''} ${selected ? 'text-visible' : ''}`}>
{item.label}
</span>
{selected && <CheckIcon />}
</div>
);

const DetailedVariant = ({
item,
selected,
isButtonLabel,
}: {
item: Item;
selected: boolean;
isButtonLabel?: boolean;
}) => (
<div className={cn("flex items-center w-full", selected && "justify-between")}>
<div className='flex items-center'>
const DetailedVariant = ({ item, selected, isButtonLabel }: { item: Item; selected: boolean; isButtonLabel?: boolean }) => (
<div className={cn('flex items-center w-full', selected && 'justify-between')}>
<div className="flex items-center">
{item.leadingElement && !isButtonLabel ? <>{item.leadingElement}</> : null}

<div className='flex flex-col items-start'>
<div
className={`line-clamp-1 text-sm font-medium${isButtonLabel ? " max-w-[160px]" : ""} ${
selected ? "text-visible" : ""
}`}
>
<div className="flex flex-col items-start">
<div className={`line-clamp-1 text-sm font-medium${isButtonLabel ? ' max-w-[160px]' : ''} ${selected ? 'text-visible' : ''}`}>
{item.label}
</div>
{!isButtonLabel ? <div className='text-xs text-gray-500'>{item.description}</div> : null}
{!isButtonLabel ? <div className="text-xs text-gray-500">{item.description}</div> : null}
</div>
</div>

Expand All @@ -157,41 +127,29 @@ export function Combobox({
);

const IconCompactVariant = ({ item, selected }: { item: Item; selected: boolean }) => (
<div className={cn("w-full h-full flex items-center", selected && "justify-between")}>
<div className='flex items-center gap-2 h-full'>
<div className='flex items-center justify-center w-6 h-6 rounded-md'>{item.icon || null}</div>
<div className='text-sm font-medium line-clamp-1'>{item.label}</div>
<div className={cn('w-full h-full flex items-center', selected && 'justify-between')}>
<div className="flex items-center gap-2 h-full">
<div className="flex items-center justify-center w-6 h-6 rounded-md">{item.icon || null}</div>
<div className="text-sm font-medium line-clamp-1">{item.label}</div>
</div>

{selected && <CheckIcon height={20} width={20} />}
</div>
);

const ImageDetailedVariant = ({
item,
selected,
isButtonLabel,
}: {
item: Item;
selected: boolean;
isButtonLabel?: boolean;
}) => (
<div className={cn("flex items-center w-full h-full", selected && "justify-between")}>
<div className='flex items-center gap-4 h-full'>
const ImageDetailedVariant = ({ item, selected, isButtonLabel }: { item: Item; selected: boolean; isButtonLabel?: boolean }) => (
<div className={cn('flex items-center w-full h-full', selected && 'justify-between')}>
<div className="flex items-center gap-4 h-full">
{item.imageUrl ? (
<img src={item.imageUrl} alt={item.label} className='w-[64px] h-[48px] rounded-md object-cover' />
<img src={item.imageUrl} alt={item.label} className="w-[64px] h-[48px] rounded-md object-cover" />
) : (
<div className='w-[64px] h-[48px] rounded-md bg-gray-200' />
<div className="w-[64px] h-[48px] rounded-md bg-gray-200" />
)}
<div className='flex flex-col gap-1'>
<div
className={`line-clamp-2 text-sm${
isButtonLabel ? " max-w-[151px] w-full truncate h-full flex items-center" : ""
}`}
>
<div className="flex flex-col gap-1">
<div className={`line-clamp-2 text-sm${isButtonLabel ? ' max-w-[151px] w-full truncate h-full flex items-center' : ''}`}>
{item.label}
</div>
<div className='text-xs text-gray-500'>{item.description}</div>
<div className="text-xs text-gray-500">{item.description}</div>
</div>
</div>

Expand All @@ -201,11 +159,11 @@ export function Combobox({

const getVariant = () => {
switch (variant) {
case "detailed":
case 'detailed':
return DetailedVariant;
case "icon-compact":
case 'icon-compact':
return IconCompactVariant;
case "image-detailed":
case 'image-detailed':
return ImageDetailedVariant;
default:
return DefaultVariant;
Expand All @@ -220,122 +178,122 @@ export function Combobox({
if (value) {
const selectedItem = options.flatMap((option) => option.items).find((item) => item.value === value);

if (selectedItem && variant === "image-detailed") {
if (selectedItem && variant === 'image-detailed') {
return React.createElement(getVariant(), {
item: selectedItem,
selected: false,
isButtonLabel: true,
isButtonLabel: true
});
}

if (selectedItem && variant === "icon-compact") {
if (selectedItem && variant === 'icon-compact') {
return React.createElement(getVariant(), {
item: selectedItem,
selected: false,
selected: false
});
}

if (selectedItem) {
return React.createElement(getVariant(), {
item: selectedItem,
selected: false,
isButtonLabel: true,
isButtonLabel: true
});
}

return null;
}

return <span className='text-tertiary-foreground text-sm opacity-60 font-normal'>{placeholder}</span>;
return <span className="text-tertiary-foreground text-sm opacity-60 font-normal">{placeholder}</span>;
};

return (
<Popover open={open} onOpenChange={setOpen}>
<div className='flex flex-col items-start gap-1 w-full'>
{label ? <label className='text-xs font-semibold text-tertiary-foreground'>{label}</label> : null}
<div className="flex flex-col items-start gap-1 w-full">
{label ? <label className="text-xs font-semibold text-tertiary-foreground">{label}</label> : null}

<PopoverTrigger asChild>
<Button
variant='outline'
size='combobox'
role='combobox'
variant="outline"
size="combobox"
role="combobox"
aria-expanded={open}
className='w-full justify-between bg-background-accent hover:bg-background-accent'
className="w-full justify-between bg-background-accent hover:bg-background-accent"
>
{renderButtonContent()}

{open ? (
<CaretUpIcon className='ml-2 h-4 w-4 shrink-0 opacity-50' />
<CaretUpIcon className="ml-2 h-4 w-4 shrink-0 opacity-50" />
) : (
<CaretDownIcon className='ml-2 h-4 w-4 shrink-0 opacity-50' />
<CaretDownIcon className="ml-2 h-4 w-4 shrink-0 opacity-50" />
)}
</Button>
</PopoverTrigger>

{helperText ? <span className='text-xs font-normal text-tertiary-foreground mt-1'>{helperText}</span> : null}
{helperText ? <span className="text-xs font-normal text-tertiary-foreground mt-1">{helperText}</span> : null}
</div>

<PopoverContent className='w-full p-0 bg-background-accent' data-testid='comboxbox-popover-content'>
<PopoverContent
className="w-full p-0 bg-background-accent"
onWheel={(e) => e.stopPropagation()}
onTouchMove={(e) => e.stopPropagation()}
data-testid="comboxbox-popover-content"
>
<Command shouldFilter={shouldFilter} className={className}>
<div className='w-full p-4 flex items-center justify-center border-b border-divider'>
<CommandInput
placeholder={searchPlaceholder}
className='h-9 w-full'
defaultValue={searchValue}
onInput={handleSearchInput}
/>
<div className="w-full p-4 flex items-center justify-center border-b border-divider">
<CommandInput placeholder={searchPlaceholder} className="h-9 w-full" defaultValue={searchValue} onInput={handleSearchInput} />
</div>

<CommandEmpty>{emptySearchPlaceholder}</CommandEmpty>

<div className='max-h-[272px] overflow-y-auto scrollbar-style'>
<div className="max-h-[272px] overflow-y-auto scrollbar-style">
{options.map((option, index) => (
<>
<CommandGroup key={index} className='py-2' heading={option.heading}>
<CommandGroup key={index} className="py-2" heading={option.heading}>
{option.items.map((item) => (
<CommandItem
key={item.value}
value={item.value}
onSelect={(currentValue: string) => {
onSelect?.(item);
setValue(currentValue === value ? "" : currentValue);
setValue(currentValue === value ? '' : currentValue);
setOpen(false);
}}
>
{React.createElement(getVariant(), {
item,
selected: value === item.value,
selected: value === item.value
})}
</CommandItem>
))}
</CommandGroup>

{index < options.length - 1 && <div className='border-b border-divider' />}
{index < options.length - 1 && <div className="border-b border-divider" />}
</>
))}

<div
ref={lastItemRef}
style={{ display: "flex", alignItems: "center", justifyContent: "center", minHeight: 4, width: "100%" }}
style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', minHeight: 4, width: '100%' }}
/>
</div>

{isLoading ? (
<div className='flex items-center justify-center mb-4 h-10'>
<div className="flex items-center justify-center mb-4 h-10">
<svg
aria-hidden='true'
className='w-8 h-8 text-gray-200 animate-spin fill-purple-500'
viewBox='0 0 100 101'
fill='none'
xmlns='http://www.w3.org/2000/svg'
aria-hidden="true"
className="w-8 h-8 text-gray-200 animate-spin fill-purple-500"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d='M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z'
fill='currentColor'
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d='M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z'
fill='currentFill'
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
</div>
Expand Down
Loading