Improve chat panel keyboard navigation
- Focus input field when plus button is clicked - Close menu when 'Escape' key is pressed - Refactor menu navigation for better readability
This commit is contained in:
parent
039a6d0b22
commit
64ba8612c6
@ -50,6 +50,7 @@ const chatPanel = () => {
|
|||||||
const handlePlusClick = (event: React.MouseEvent<HTMLButtonElement>) => {
|
const handlePlusClick = (event: React.MouseEvent<HTMLButtonElement>) => {
|
||||||
setMenuType('contexts');
|
setMenuType('contexts');
|
||||||
setMenuOpend(!menuOpend);
|
setMenuOpend(!menuOpend);
|
||||||
|
inputRef.current.focus();
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -289,22 +290,27 @@ const chatPanel = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleKeyDown = (event: React.KeyboardEvent<HTMLTextAreaElement>) => {
|
const handleKeyDown = (event: React.KeyboardEvent<HTMLTextAreaElement>) => {
|
||||||
if (menuOpend && menuType === 'commands') {
|
if (menuOpend) {
|
||||||
if (event.key === 'ArrowDown') {
|
if (event.key === 'Escape') {
|
||||||
const newIndex = currentMenuIndex + 1;
|
|
||||||
setCurrentMenuIndex(newIndex < commandMenusNode.length ? newIndex : 0);
|
|
||||||
event.preventDefault();
|
|
||||||
}
|
|
||||||
if (event.key === 'ArrowUp') {
|
|
||||||
const newIndex = currentMenuIndex - 1;
|
|
||||||
setCurrentMenuIndex(newIndex < 0 ? commandMenusNode.length - 1 : newIndex);
|
|
||||||
event.preventDefault();
|
|
||||||
}
|
|
||||||
if (event.key === 'Enter' && !event.shiftKey) {
|
|
||||||
const commandNode = commandMenusNode[currentMenuIndex];
|
|
||||||
setInput(`/${commandNode.props['data-pattern']} `);
|
|
||||||
setMenuOpend(false);
|
setMenuOpend(false);
|
||||||
event.preventDefault();
|
}
|
||||||
|
if (menuType === 'commands') {
|
||||||
|
if (event.key === 'ArrowDown') {
|
||||||
|
const newIndex = currentMenuIndex + 1;
|
||||||
|
setCurrentMenuIndex(newIndex < commandMenusNode.length ? newIndex : 0);
|
||||||
|
event.preventDefault();
|
||||||
|
}
|
||||||
|
if (event.key === 'ArrowUp') {
|
||||||
|
const newIndex = currentMenuIndex - 1;
|
||||||
|
setCurrentMenuIndex(newIndex < 0 ? commandMenusNode.length - 1 : newIndex);
|
||||||
|
event.preventDefault();
|
||||||
|
}
|
||||||
|
if (event.key === 'Enter' && !event.shiftKey) {
|
||||||
|
const commandNode = commandMenusNode[currentMenuIndex];
|
||||||
|
setInput(`/${commandNode.props['data-pattern']} `);
|
||||||
|
setMenuOpend(false);
|
||||||
|
event.preventDefault();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (event.key === 'Enter' && !event.shiftKey) {
|
if (event.key === 'Enter' && !event.shiftKey) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user