Shadcn 表单示例

cccs7 Lv5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<div className="add-circle-content">
<Form
{...form}
>
<form onSubmit={form.handleSubmit(onSubmit)} className="w-full space-y-6">
<FormField
className='md:w-auto'
control={form.control}
name="circleName"
render={({field}) => (
<div className='flex items-start gap-4 '>
<FormItem>
<FormLabel>CircleName</FormLabel>
<FormControl>
<Input placeholder="circle-name" {...field} />
</FormControl>
<FormDescription>
This is your public display circle name.
</FormDescription>
<FormMessage/>
</FormItem>
</div>
)}
/>
<FormField
control={form.control}
name="category"
render={({field}) => (
<div className='flex items-start '>
<FormItem>
<FormLabel>category</FormLabel>
<FormControl>
<ComboboxArea
list={circleCategoryList}
value={field.value}
onChange={field.onChange}
{...field}
></ComboboxArea>
</FormControl>
<FormMessage/>
</FormItem>
</div>
)}
/>
</div>
</div>
<Button type="submit">Submit</Button>
</form>
</Form>

</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const FormSchema = z.object({
circleName: z.string().min(2, {
message: "circleName must be at least 2 characters.",
}),
category: z.string().nonempty({
message: "not null"
}),
intro: z.string().optional(), // 添加介绍字段
imageUrl: z.string().url({ // 添加图片URL字段
message: "Please upload a valid image"
})
})

const form = useForm<z.infer<typeof FormSchema>>({
resolver: zodResolver(FormSchema),
defaultValues: {
circleName: "",
category: ""
},
})


function onSubmit(data: z.infer<typeof FormSchema>) {

showGlobalLoader()
setTimeout(() => {
hideGlobalLoader();
}, 5000);
toast({
title: "You submitted the following values:",
description: (
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
<code className="text-white">{JSON.stringify(data, null, 2)}</code>
</pre>
),
})
}
  • Title: Shadcn 表单示例
  • Author: cccs7
  • Created at : 2025-04-08 23:32:00
  • Updated at : 2025-04-08 23:32:39
  • Link: https://cs7eric.github.io/2025/04/08/Shadcn 表单示例/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments
On this page
Shadcn 表单示例