Skip to content

Commit 7280642

Browse files
author
Rankin Zheng
committed
Add useRef hook to InputMessage component
1 parent f51236e commit 7280642

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

src/views/components/InputMessage/index.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
IconTextPlus,
2424
IconRobot,
2525
} from "@tabler/icons-react";
26-
import React, { useState, useEffect } from "react";
26+
import React, { useState, useEffect, useRef } from "react";
2727
import {
2828
IconGitBranchChecked,
2929
IconShellCommand,
@@ -72,6 +72,7 @@ const InputMessage = observer((props: any) => {
7272
} = input;
7373
const { generating } = chat;
7474
const showTopic = process.env.platform === "idea";
75+
const viewport = useRef<HTMLDivElement>(null);
7576

7677
const [drawerOpened, { open: openDrawer, close: closeDrawer }] =
7778
useDisclosure(false);
@@ -130,18 +131,24 @@ const InputMessage = observer((props: any) => {
130131
}
131132
if (menuType === "commands") {
132133
if (event.key === "ArrowDown") {
134+
event.preventDefault();
133135
const newIndex = currentMenuIndex + 1;
134136
input.setCurrentMenuIndex(
135137
newIndex < commandMenusNode.length ? newIndex : 0
136138
);
137-
event.preventDefault();
139+
viewport.current
140+
?.querySelectorAll('[data-list-item]')
141+
?.[newIndex]?.scrollIntoView({ block: 'nearest' });
138142
}
139143
if (event.key === "ArrowUp") {
144+
event.preventDefault();
140145
const newIndex = currentMenuIndex - 1;
141146
input.setCurrentMenuIndex(
142147
newIndex < 0 ? commandMenusNode.length - 1 : newIndex
143148
);
144-
event.preventDefault();
149+
viewport.current
150+
?.querySelectorAll('[data-list-item]')
151+
?.[newIndex]?.scrollIntoView({ block: 'nearest' });
145152
}
146153
if ((event.key === "Enter" || event.key === "Tab") && !event.shiftKey) {
147154
const commandNode = commandMenusNode[currentMenuIndex];
@@ -270,6 +277,7 @@ const InputMessage = observer((props: any) => {
270277
return (
271278
<Flex
272279
key={`command-menus-${index}`}
280+
data-list-item
273281
mih={40}
274282
gap="md"
275283
justify="flex-start"
@@ -595,7 +603,7 @@ const InputMessage = observer((props: any) => {
595603
}}
596604
>
597605
<Text sx={{ padding: "5px 5px 5px 10px" }}>DevChat Workflows</Text>
598-
<ScrollArea.Autosize mah={240} type="always" placeholder="">
606+
<ScrollArea.Autosize mah={240} type="always" placeholder="" viewportRef={viewport}>
599607
{commandMenusNode}
600608
</ScrollArea.Autosize>
601609
</Popover.Dropdown>

0 commit comments

Comments
 (0)