-
Notifications
You must be signed in to change notification settings - Fork 299
Expand file tree
/
Copy pathSuggestionListItem.tsx
More file actions
68 lines (61 loc) · 1.98 KB
/
SuggestionListItem.tsx
File metadata and controls
68 lines (61 loc) · 1.98 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
import clsx from 'clsx';
import type { Ref } from 'react';
import React, { useCallback, useLayoutEffect, useRef } from 'react';
import { useMessageComposer } from '../../MessageInput';
import type { TextComposerSuggestion } from 'stream-chat';
import type { UserItemProps } from './UserItem';
import type { CommandItemProps } from './CommandItem';
import type { EmoticonItemProps } from './EmoticonItem';
export type DefaultSuggestionListItemEntity =
| UserItemProps['entity']
| CommandItemProps['entity']
| EmoticonItemProps['entity'];
export type SuggestionListItemComponentProps = {
entity: DefaultSuggestionListItemEntity | unknown;
focused: boolean;
};
export type SuggestionItemProps = {
component: React.ComponentType<SuggestionListItemComponentProps>;
item: TextComposerSuggestion;
focused: boolean;
className?: string;
onMouseEnter?: () => void;
};
export const SuggestionListItem = React.forwardRef<
HTMLButtonElement,
SuggestionItemProps
>(function SuggestionListItem(
{ className, component: Component, focused, item, onMouseEnter }: SuggestionItemProps,
innerRef: Ref<HTMLButtonElement>,
) {
const { textComposer } = useMessageComposer();
const containerRef = useRef<HTMLLIElement>(null);
const handleSelect = useCallback(() => {
textComposer.handleSelect(item);
}, [item, textComposer]);
useLayoutEffect(() => {
if (!focused) return;
containerRef.current?.scrollIntoView({ behavior: 'instant', block: 'nearest' });
}, [focused, containerRef]);
return (
<li
className={clsx('str-chat__suggestion-list-item', className, {
'str-chat__suggestion-item--selected': focused,
})}
onMouseEnter={onMouseEnter}
ref={containerRef}
>
<button
onClick={handleSelect}
onKeyDown={(event) => {
if (event.key === 'Enter') {
handleSelect();
}
}}
ref={innerRef}
>
<Component entity={item} focused={focused} />
</button>
</li>
);
});