Skip to content

Commit

Permalink
Merge pull request #57 from MARU-EGG/TSK-49
Browse files Browse the repository at this point in the history
[Tsk-49] ChatForm enter event issue clear
  • Loading branch information
sangmaaaaan authored Nov 3, 2024
2 parents 85fca35 + a9d6633 commit e77b6e1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
3 changes: 1 addition & 2 deletions src/hooks/use-chat-form.hooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// src/hooks/useChatForm.ts
import { useState, useCallback } from 'react';
import { useAutoComplete } from './use-auto-complete.hooks';
import useChatStore from '../store/chat-store';
Expand Down Expand Up @@ -46,7 +45,7 @@ const useChatForm = () => {
};

const handleSubmit = useCallback(
async (e: React.FormEvent<HTMLFormElement>) => {
async (e: React.FormEvent<HTMLFormElement> | React.KeyboardEvent<HTMLTextAreaElement>) => {
e.preventDefault();
try {
addMessage({ content, role: 'user' });
Expand Down
14 changes: 14 additions & 0 deletions src/hooks/use-textarea-submit.hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
interface TextAreaSubmitHooksProps {
handleSubmit: (e: React.FormEvent<HTMLFormElement> | React.KeyboardEvent<HTMLTextAreaElement>) => void;
}

export function useTextAreaSubmit({ handleSubmit }: TextAreaSubmitHooksProps) {
const handlePressEnter = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
handleSubmit(e);
}
};

return { handlePressEnter };
}
19 changes: 13 additions & 6 deletions src/ui/components/molecule/chat-form/chat-form.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
// src/ui/molecule/chat-form.tsx
import React from 'react';
import TextInput from '../../atom/text-input/text-input';
import IconButton from '../../atom/icon/icon-button';
import { ReactComponent as SendIcon } from '../../../../assets/Send.svg';
import AutoCompleteList from '../../atom/auto-complete/auto-complete';
import { cn } from '../../../../utils/style';
import useChatForm from '../../../../hooks/use-chat-form.hooks';
import { useTextAreaSubmit } from '../../../../hooks/use-textarea-submit.hooks';

const ChatForm = () => {
const { content, autoOpen, disabled, results, handleChange, handleSelect, handleSubmit } = useChatForm();

const { handlePressEnter } = useTextAreaSubmit({ handleSubmit });
return (
<>
{autoOpen && <AutoCompleteList results={results} onSelect={handleSelect} />}
<form
className={cn('flex flex-nowrap rounded-2xl border py-2 pr-1', 'bg-background-default')}
onSubmit={handleSubmit}
>
<TextInput
value={content}
onValueChange={handleChange}
placeholder="메시지를 입력해주세요."
<textarea
rows={2}
className={cn(
`w-full resize-none rounded-2xl bg-transparent px-4 py-2 text-base`,
`placeholder:text-[#72777A] focus:outline-none`,
`disabled:cursor-wait`,
)}
disabled={disabled}
value={content}
onChange={(e) => handleChange(e.target.value)}
onKeyDown={handlePressEnter}
placeholder="질문을 입력해주세요"
/>
<IconButton type="submit" disabled={disabled}>
<div className="pr-2">
Expand Down

0 comments on commit e77b6e1

Please sign in to comment.