Skip to content

Commit

Permalink
🐛 fix: slove when input area controlled mode but chinese chats not wo…
Browse files Browse the repository at this point in the history
…rk (#223)
  • Loading branch information
ONLY-yours committed May 23, 2024
1 parent 7cec024 commit 9dcf240
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
24 changes: 17 additions & 7 deletions src/ProChat/components/InputArea/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { SendOutlined } from '@ant-design/icons';
import { Button, ButtonProps, ConfigProvider } from 'antd';
import { createStyles, cx } from 'antd-style';
import { ReactNode, useContext, useMemo, useRef } from 'react';
import { ReactNode, useContext, useEffect, useMemo, useRef, useState } from 'react';
import { Flexbox } from 'react-layout-kit';

import { useStore } from '../../store';

import useProChatLocale from '@/ProChat/hooks/useProChatLocale';
import { TextAreaProps } from 'antd/es/input';
import { useMergedState } from 'rc-util';
import ActionBar from './ActionBar';
import { AutoCompleteTextArea } from './AutoCompleteTextArea';
import StopLoadingIcon from './StopLoading';
Expand Down Expand Up @@ -88,10 +87,20 @@ export const ChatInputArea = (props: ChatInputAreaProps) => {
const { localeObject } = useProChatLocale();

const { value, onChange } = inputAreaProps || {};
const [message, setMessage] = useMergedState('', {
onChange: onChange,
value: value,
});
const [message, setMessage] = useState('');

// 兼容中文的受控输入逻辑
useEffect(() => {
if (!isChineseInput.current && onChange) {
onChange(message);
}
}, [message]);

useEffect(() => {
if (value) {
setMessage(value);
}
}, [value]);

const send = async () => {
if (onSend) {
Expand Down Expand Up @@ -136,8 +145,9 @@ export const ChatInputArea = (props: ChatInputAreaProps) => {
onCompositionStart: () => {
isChineseInput.current = true;
},
onCompositionEnd: () => {
onCompositionEnd: (e) => {
isChineseInput.current = false;
setMessage(e.target.value);
},
onPressEnter: (e) => {
if (!isLoading && !e.shiftKey && !isChineseInput.current) {
Expand Down
2 changes: 0 additions & 2 deletions src/ProChat/demos/request.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import { useState } from 'react';

export default () => {
const theme = useTheme();

const [value, setValue] = useState();

return (
<div style={{ background: theme.colorBgLayout }}>
<ProChat
Expand Down

0 comments on commit 9dcf240

Please sign in to comment.