fix: auto hegiht for textarea

This commit is contained in:
2024-01-14 14:33:05 +08:00
parent 324e374884
commit 67a8140a01
3 changed files with 14 additions and 8 deletions

View File

@@ -30,6 +30,7 @@ import getDefaultParams from "./getDefaultParam";
import { AddImage } from "./addImage"; import { AddImage } from "./addImage";
import { ListAPIs } from "./listAPIs"; import { ListAPIs } from "./listAPIs";
import { ListToolsTempaltes } from "./listToolsTemplates"; import { ListToolsTempaltes } from "./listToolsTemplates";
import { autoHeight } from "./textarea";
export interface TemplateChatStore extends ChatStore { export interface TemplateChatStore extends ChatStore {
name: string; name: string;
@@ -832,8 +833,7 @@ export default function ChatBOX(props: {
value={inputMsg} value={inputMsg}
onChange={(event: any) => { onChange={(event: any) => {
setInputMsg(event.target.value); setInputMsg(event.target.value);
event.target.style.height = "auto"; autoHeight(event);
event.target.style.height = `${event.target.scrollHeight+1}px`;
}} }}
onKeyPress={(event: any) => { onKeyPress={(event: any) => {
console.log(event); console.log(event);
@@ -842,9 +842,8 @@ export default function ChatBOX(props: {
setInputMsg(""); setInputMsg("");
return; return;
} }
autoHeight(event);
setInputMsg(event.target.value); setInputMsg(event.target.value);
event.target.style.height = "auto";
event.target.style.height = `${event.target.scrollHeight+1}px`;
}} }}
className="rounded grow m-1 p-1 border-2 border-gray-400 w-0" className="rounded grow m-1 p-1 border-2 border-gray-400 w-0"
placeholder="Type here..." placeholder="Type here..."

View File

@@ -13,6 +13,7 @@ import { tr, Tr, langCodeContext, LANG_OPTIONS } from "./translate";
import p from "preact-markdown"; import p from "preact-markdown";
import { isVailedJSON } from "./message"; import { isVailedJSON } from "./message";
import { SetAPIsTemplate } from "./setAPIsTemplate"; import { SetAPIsTemplate } from "./setAPIsTemplate";
import { autoHeight } from "./textarea";
const TTS_VOICES: string[] = [ const TTS_VOICES: string[] = [
"alloy", "alloy",
@@ -80,12 +81,10 @@ const LongInput = (props: {
onChange={(event: any) => { onChange={(event: any) => {
props.chatStore[props.field] = event.target.value; props.chatStore[props.field] = event.target.value;
props.setChatStore({ ...props.chatStore }); props.setChatStore({ ...props.chatStore });
event.target.style.height = "auto"; autoHeight(event);
event.target.style.height = `${event.target.scrollHeight + 1}px`;
}} }}
onKeyPress={(event: any) => { onKeyPress={(event: any) => {
event.target.style.height = "auto"; autoHeight(event);
event.target.style.height = `${event.target.scrollHeight + 1}px`;
}} }}
></textarea> ></textarea>
</Help> </Help>

8
src/textarea.tsx Normal file
View File

@@ -0,0 +1,8 @@
export const autoHeight = (event: any) => {
event.target.style.height = "auto";
// max 70% of screen height
event.target.style.height = `${Math.min(
event.target.scrollHeight,
window.innerHeight * 0.7
)}px`;
};