2025-09-30 23:25:41 +08:00
|
|
|
import { Command } from 'cmdk';
|
2025-09-12 13:43:51 +08:00
|
|
|
import type React from 'react';
|
|
|
|
import { cn } from '@/lib/utils';
|
2025-09-30 21:26:09 +08:00
|
|
|
import { Popover } from '../../popover';
|
2025-09-30 03:10:04 +08:00
|
|
|
import type {
|
|
|
|
MentionInputSuggestionsDropdownItem,
|
|
|
|
MentionInputSuggestionsOnSelectParams,
|
|
|
|
} from './MentionInputSuggestions.types';
|
2025-09-12 13:43:51 +08:00
|
|
|
|
2025-09-30 02:26:19 +08:00
|
|
|
export type MentionInputSuggestionsItemProps = {
|
|
|
|
onSelect: (d: MentionInputSuggestionsOnSelectParams) => void;
|
|
|
|
} & MentionInputSuggestionsDropdownItem & {
|
2025-09-12 13:43:51 +08:00
|
|
|
className?: string;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
};
|
|
|
|
|
2025-09-30 02:26:19 +08:00
|
|
|
export const MentionInputSuggestionsItem = ({
|
2025-09-12 13:43:51 +08:00
|
|
|
value,
|
|
|
|
inputValue,
|
|
|
|
label,
|
|
|
|
icon,
|
|
|
|
onClick,
|
|
|
|
disabled,
|
|
|
|
loading,
|
|
|
|
closeOnSelect,
|
|
|
|
type,
|
|
|
|
addValueToInput,
|
|
|
|
onSelect,
|
2025-09-30 05:48:53 +08:00
|
|
|
className,
|
|
|
|
style,
|
2025-09-30 21:26:09 +08:00
|
|
|
popoverContent,
|
2025-09-30 02:26:19 +08:00
|
|
|
}: MentionInputSuggestionsItemProps) => {
|
2025-10-02 03:52:19 +08:00
|
|
|
const onSelectItem = () => {
|
|
|
|
onSelect({
|
|
|
|
value,
|
|
|
|
inputValue,
|
|
|
|
label,
|
|
|
|
onClick,
|
|
|
|
addValueToInput,
|
|
|
|
closeOnSelect,
|
|
|
|
disabled,
|
|
|
|
loading,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2025-09-12 13:43:51 +08:00
|
|
|
return (
|
2025-09-30 21:26:09 +08:00
|
|
|
<PopoverContentWrapper popoverContent={popoverContent}>
|
|
|
|
<Command.Item
|
|
|
|
className={cn(
|
2025-10-01 12:03:41 +08:00
|
|
|
'data-[selected=true]:bg-item-hover data-[selected=true]:text-foreground relative flex cursor-default items-center gap-2 rounded-sm px-2 py-1.5 text-base outline-none select-none data-[disabled=true]:pointer-events-none data-[disabled=true]:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0',
|
2025-09-30 21:26:09 +08:00
|
|
|
!disabled ? 'cursor-pointer' : 'cursor-not-allowed',
|
2025-10-01 01:42:03 +08:00
|
|
|
'text-secondary group min-h-9',
|
2025-09-30 21:26:09 +08:00
|
|
|
className
|
|
|
|
)}
|
|
|
|
value={value}
|
|
|
|
data-testid={`type-${type}-value-${value}`}
|
|
|
|
style={style}
|
2025-10-02 03:52:19 +08:00
|
|
|
onMouseDown={() => {
|
|
|
|
onSelectItem();
|
|
|
|
}}
|
2025-09-30 21:26:09 +08:00
|
|
|
onSelect={() => {
|
2025-10-02 03:52:19 +08:00
|
|
|
onSelectItem();
|
2025-09-30 21:26:09 +08:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{icon && (
|
2025-10-01 12:03:41 +08:00
|
|
|
<span className="text-icon-color text-center group-hover:text-foreground text-icon-size-sm size-3">
|
2025-09-30 21:26:09 +08:00
|
|
|
{icon}
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
{label}
|
|
|
|
</Command.Item>
|
|
|
|
</PopoverContentWrapper>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const PopoverContentWrapper = ({
|
|
|
|
children,
|
|
|
|
popoverContent,
|
|
|
|
}: {
|
|
|
|
children: React.ReactNode;
|
|
|
|
popoverContent?: MentionInputSuggestionsItemProps['popoverContent'];
|
|
|
|
}) => {
|
|
|
|
if (!popoverContent) return children;
|
|
|
|
|
|
|
|
return (
|
2025-10-01 00:25:03 +08:00
|
|
|
<Popover
|
|
|
|
trigger="hover"
|
|
|
|
side="top"
|
|
|
|
sideOffset={10}
|
|
|
|
align="start"
|
|
|
|
className="p-0"
|
|
|
|
content={popoverContent}
|
2025-10-01 00:59:18 +08:00
|
|
|
delayDuration={600}
|
2025-10-01 00:25:03 +08:00
|
|
|
>
|
2025-09-30 21:26:09 +08:00
|
|
|
{children}
|
|
|
|
</Popover>
|
2025-09-12 13:43:51 +08:00
|
|
|
);
|
|
|
|
};
|