refactor: Input, Textarea maintain their own value, fix #8

This commit is contained in:
2025-01-08 01:51:09 +08:00
parent d736c12ac1
commit 137186e760
3 changed files with 15 additions and 9 deletions

View File

@@ -141,7 +141,7 @@ const SelectModel = (props: { help: string }) => {
{useCustomModel ? (
<Input
value={chatStore.model}
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
onBlur={(e: React.ChangeEvent<HTMLInputElement>) => {
chatStore.model = e.target.value;
setChatStore({ ...chatStore });
}}
@@ -205,7 +205,7 @@ const LongInput = (props: {
<Textarea
className="h-24 w-full"
value={chatStore[props.field]}
onChange={async (event: any) => {
onBlur={async (event: any) => {
chatStore[props.field] = event.target.value;
await setChatStore({ ...chatStore });
autoHeight(event.target);
@@ -257,7 +257,7 @@ const InputField = (props: {
<Input
type={hideInput ? "password" : "text"}
value={chatStore[props.field]}
onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
onBlur={(event: React.ChangeEvent<HTMLInputElement>) => {
chatStore[props.field] = event.target.value;
setChatStore({ ...chatStore });
}}
@@ -351,7 +351,7 @@ const Slicer = (props: {
disabled={!enabled}
className="w-24"
value={chatStore[props.field]}
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
onBlur={(e: React.ChangeEvent<HTMLInputElement>) => {
const value = parseFloat(e.target.value);
chatStore[props.field] = value;
setChatStore({ ...chatStore });
@@ -415,7 +415,7 @@ const Number = (props: {
props.field === "maxGenTokens" && !chatStore.maxGenTokens_enabled
}
value={chatStore[props.field]}
onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
onBlur={(event: React.ChangeEvent<HTMLInputElement>) => {
let newNumber = parseFloat(event.target.value);
if (newNumber < 0) newNumber = 0;
chatStore[props.field] = newNumber;

View File

@@ -1,11 +1,14 @@
import * as React from "react";
import { useState, ComponentProps, forwardRef } from "react";
import { cn } from "@/lib/utils";
const Input = React.forwardRef<HTMLInputElement, React.ComponentProps<"input">>(
({ className, type, ...props }, ref) => {
const Input = forwardRef<HTMLInputElement, ComponentProps<"input">>(
({ className, type, value, ...props }, ref) => {
const [innerValue, setInnerValue] = useState(value);
return (
<input
value={innerValue}
onChange={(e) => setInnerValue(e.target.value)}
type={type}
className={cn(
"flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-base shadow-sm transition-colors file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 md:text-sm",

View File

@@ -5,9 +5,12 @@ import { cn } from "@/lib/utils";
const Textarea = React.forwardRef<
HTMLTextAreaElement,
React.ComponentProps<"textarea">
>(({ className, ...props }, ref) => {
>(({ className, value, onChange, ...props }, ref) => {
const [innerValue, setInnerValue] = React.useState(value);
return (
<textarea
value={innerValue}
onChange={(e) => setInnerValue(e.target.value)}
className={cn(
"flex min-h-[60px] w-full rounded-md border border-input bg-transparent px-3 py-2 text-base shadow-sm placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 md:text-sm",
className