Skip to content

Commit

Permalink
✨ feat: support input area value controlled
Browse files Browse the repository at this point in the history
  • Loading branch information
ONLY-yours committed May 9, 2024
1 parent c80574c commit 73827cf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/ProChat/components/InputArea/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { SendOutlined } from '@ant-design/icons';
import { Button, ButtonProps, ConfigProvider } from 'antd';
import { createStyles, cx } from 'antd-style';
import { ReactNode, useContext, useMemo, useRef, useState } from 'react';
import { ReactNode, useContext, useMemo, useRef } 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 @@ -82,11 +83,16 @@ export const ChatInputArea = (props: ChatInputAreaProps) => {
s.stopGenerateMessage,
]);
const { getPrefixCls } = useContext(ConfigProvider.ConfigContext);
const [message, setMessage] = useState('');
const isChineseInput = useRef(false);
const { styles, theme } = useStyles();
const { localeObject } = useProChatLocale();

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

const send = async () => {
if (onSend) {
const success = await onSend(message);
Expand Down
13 changes: 12 additions & 1 deletion src/ProChat/demos/request.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,24 @@
*/
import { ProChat } from '@ant-design/pro-chat';
import { useTheme } from 'antd-style';
import { useState } from 'react';

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

const [value, setValue] = useState();

return (
<div style={{ background: theme.colorBgLayout }}>
<ProChat locale="en-US" />
<ProChat
locale="en-US"
inputAreaProps={{
value: value,
onChange: (e) => {
setValue(e);
},
}}
/>
</div>
);
};
6 changes: 5 additions & 1 deletion src/ProChat/store/initialState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ export interface ChatPropsState<T extends Record<string, any> = Record<string, a
/**
* 输入框的 props,优先级最高
*/
inputAreaProps?: TextAreaProps & { autoCompleteProps?: AutoCompleteProps };
inputAreaProps?: TextAreaProps & {
autoCompleteProps?: AutoCompleteProps;
value?: string;
onChange?: (value: string) => void;
};

/**
* 信息框额外渲染
Expand Down

0 comments on commit 73827cf

Please sign in to comment.