Skip to content

Commit de96f14

Browse files
committed
Refactor form initialization to use defaultValues and reset
Updated LLMInstanceForm, SEInstanceForm, SNSiteForm, and StoreInstanceForm to initialize forms using the defaultValues option in useForm and to reset form state with form.reset on value changes. This simplifies the code by removing repetitive setValue calls and ensures form state stays in sync with props.
1 parent 0c886e5 commit de96f14

File tree

4 files changed

+31
-55
lines changed

4 files changed

+31
-55
lines changed

turing-react/src/components/llm/llm.instance.form.tsx

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,31 +44,16 @@ interface Props {
4444
}
4545

4646
export const LLMInstanceForm: React.FC<Props> = ({ value, isNew }) => {
47-
const form = useForm<TurLLMInstance>();
48-
const { setValue } = form;
47+
const form = useForm<TurLLMInstance>({
48+
defaultValues: value
49+
});
50+
4951
const [open, setOpen] = useState(false);
5052
const navigate = useNavigate()
51-
useEffect(() => {
52-
setValue("id", value.id)
53-
setValue("title", value.title);
54-
setValue("description", value.description);
55-
setValue("turLLMVendor", value.turLLMVendor);
56-
setValue("url", value.url);
57-
setValue("modelName", value.modelName);
58-
setValue("temperature", value.temperature);
59-
setValue("topK", value.topK);
60-
setValue("topP", value.topP);
61-
setValue("repeatPenalty", value.repeatPenalty);
62-
setValue("seed", value.seed);
63-
setValue("numPredict", value.numPredict);
64-
setValue("stop", value.stop);
65-
setValue("responseFormat", value.responseFormat);
66-
setValue("supportedCapabilities", value.supportedCapabilities);
67-
setValue("timeout", value.timeout);
68-
setValue("maxRetries", value.maxRetries);
69-
setValue("enabled", value.enabled);
70-
}, [setValue, value]);
7153

54+
useEffect(() => {
55+
form.reset(value);
56+
}, [value])
7257

7358
function onSubmit(llmInstance: TurLLMInstance) {
7459
try {

turing-react/src/components/se/se.instance.form.tsx

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,16 @@ interface Props {
4242
}
4343

4444
export const SEInstanceForm: React.FC<Props> = ({ value, isNew }) => {
45-
const form = useForm<TurSEInstance>();
46-
const { setValue } = form;
45+
const form = useForm<TurSEInstance>({
46+
defaultValues: value
47+
});
48+
const { control } = form;
4749
const [open, setOpen] = useState(false);
4850
const navigate = useNavigate()
49-
useEffect(() => {
50-
setValue("id", value.id)
51-
setValue("title", value.title);
52-
setValue("description", value.description);
53-
setValue("turSEVendor", value.turSEVendor);
54-
setValue("host", value.host);
55-
setValue("port", value.port);
56-
}, [setValue, value]);
5751

52+
useEffect(() => {
53+
form.reset(value);
54+
}, [value])
5855

5956
function onSubmit(seInstance: TurSEInstance) {
6057
try {
@@ -128,7 +125,7 @@ export const SEInstanceForm: React.FC<Props> = ({ value, isNew }) => {
128125
<Form {...form}>
129126
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8 max-w-3xl mx-auto py-10">
130127
<FormField
131-
control={form.control}
128+
control={control}
132129
name="title"
133130
render={({ field }) => (
134131
<FormItem>
@@ -147,7 +144,7 @@ export const SEInstanceForm: React.FC<Props> = ({ value, isNew }) => {
147144
/>
148145

149146
<FormField
150-
control={form.control}
147+
control={control}
151148
name="description"
152149
render={({ field }) => (
153150
<FormItem>
@@ -166,7 +163,7 @@ export const SEInstanceForm: React.FC<Props> = ({ value, isNew }) => {
166163
/>
167164

168165
<FormField
169-
control={form.control}
166+
control={control}
170167
name="turSEVendor.id"
171168
render={({ field }) => (
172169
<FormItem>
@@ -189,7 +186,7 @@ export const SEInstanceForm: React.FC<Props> = ({ value, isNew }) => {
189186
/>
190187

191188
<FormField
192-
control={form.control}
189+
control={control}
193190
name="host"
194191
render={({ field }) => (
195192
<FormItem>
@@ -206,7 +203,7 @@ export const SEInstanceForm: React.FC<Props> = ({ value, isNew }) => {
206203
)}
207204
/>
208205
<FormField
209-
control={form.control}
206+
control={control}
210207
name="port"
211208
render={({ field }) => (
212209
<FormItem>

turing-react/src/components/sn/sn.site.form.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,15 @@ interface Props {
3939
}
4040

4141
export const SNSiteForm: React.FC<Props> = ({ value, isNew }) => {
42-
const form = useForm<TurSNSite>();
43-
const { setValue } = form;
42+
const form = useForm<TurSNSite>({
43+
defaultValues: value
44+
});
4445
const urlBase = "/admin/sn/instance";
4546
const navigate = useNavigate()
46-
useEffect(() => {
47-
setValue("id", value.id)
48-
setValue("name", value.name);
49-
setValue("description", value.description);
50-
setValue("turSEInstance", value.turSEInstance);
51-
}, [setValue, value]);
5247

48+
useEffect(() => {
49+
form.reset(value);
50+
}, [value])
5351

5452
function onSubmit(snSite: TurSNSite) {
5553
try {

turing-react/src/components/store/store.instance.form.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,15 @@ interface Props {
4444
}
4545

4646
export const StoreInstanceForm: React.FC<Props> = ({ value, isNew }) => {
47-
const form = useForm<TurStoreInstance>();
48-
const { setValue } = form;
47+
const form = useForm<TurStoreInstance>({
48+
defaultValues: value
49+
});
4950
const [open, setOpen] = useState(false);
5051
const navigate = useNavigate()
51-
useEffect(() => {
52-
setValue("id", value.id)
53-
setValue("title", value.title);
54-
setValue("description", value.description);
55-
setValue("turStoreVendor", value.turStoreVendor);
56-
setValue("url", value.url);
57-
setValue("enabled", value.enabled);
58-
}, [setValue, value]);
5952

53+
useEffect(() => {
54+
form.reset(value);
55+
}, [value])
6056

6157
function onSubmit(storeInstance: TurStoreInstance) {
6258
try {

0 commit comments

Comments
 (0)