|
1 | 1 | 'use client'; |
2 | 2 |
|
3 | 3 | import React, { useEffect } from 'react'; |
4 | | -import { Form, FormField, Input, useForm, useWatch } from 'soybean-react-ui'; |
| 4 | +import { Card, Form, FormField, Input, useForm, useWatch } from 'soybean-react-ui'; |
5 | 5 |
|
6 | 6 | import { showToastCode } from './toast'; |
7 | 7 |
|
| 8 | +type Inputs = { |
| 9 | + age: number; |
| 10 | + info: { city: string; company: string }; |
| 11 | + password: string; |
| 12 | + username: string; |
| 13 | +}; |
| 14 | + |
8 | 15 | export default function WatchDemo() { |
9 | | - const [form] = useForm<{ age: number; password: string; username: string }>(); |
| 16 | + const [form] = useForm<Inputs>(); |
10 | 17 |
|
11 | 18 | // 1) Watch single field |
12 | 19 | const username = useWatch(form, 'username'); |
13 | 20 |
|
14 | 21 | // 2) Watch multiple fields |
15 | 22 | const { age, password } = useWatch(form, ['age', 'password']); |
16 | 23 |
|
| 24 | + // 3) Watch nested fields |
| 25 | + const info = useWatch(form, 'info'); |
| 26 | + |
| 27 | + console.log('info', info); |
| 28 | + |
17 | 29 | const { age: NewAge, password: NewPassword, username: NewUsername } = useWatch(form); |
18 | 30 |
|
19 | 31 | useEffect(() => { |
20 | 32 | showToastCode('NewValues', { NewAge, NewPassword, NewUsername }); |
21 | 33 | }, [NewAge, NewPassword, NewUsername]); |
22 | 34 |
|
23 | 35 | return ( |
24 | | - <div className="p-4 space-y-4 w-[480px] max-sm:w-full"> |
25 | | - <Form form={form}> |
26 | | - <FormField |
27 | | - label="Username" |
28 | | - name="username" |
29 | | - > |
30 | | - <Input placeholder="Enter username" /> |
31 | | - </FormField> |
32 | | - |
33 | | - <FormField |
34 | | - label="Age" |
35 | | - name="age" |
36 | | - rules={[{ message: 'Must be 18 or older', min: 18, type: 'number' }]} |
37 | | - > |
38 | | - <Input placeholder="" /> |
39 | | - </FormField> |
40 | | - |
41 | | - <FormField |
42 | | - label="Password" |
43 | | - name="password" |
44 | | - rules={[{ message: 'Password must be at least 6 characters', minLength: 6 }]} |
45 | | - > |
46 | | - <Input placeholder="" /> |
47 | | - </FormField> |
48 | | - </Form> |
49 | | - |
50 | | - {/* 4) Dynamic rendering */} |
51 | | - <div className="mt-4 p-2 border rounded"> |
52 | | - <h3 className="font-bold">Real-time Watch Results:</h3> |
53 | | - <p>Username: {username}</p> |
54 | | - <p>Age: {age}</p> |
55 | | - <p>Password: {password}</p> |
| 36 | + <Card title="Use Watch"> |
| 37 | + <div className="space-y-4 w-[480px] max-sm:w-full"> |
| 38 | + <Form form={form}> |
| 39 | + <FormField |
| 40 | + label="Username" |
| 41 | + name="username" |
| 42 | + > |
| 43 | + <Input placeholder="Enter username" /> |
| 44 | + </FormField> |
| 45 | + |
| 46 | + <FormField |
| 47 | + label="Age" |
| 48 | + name="age" |
| 49 | + rules={[{ message: 'Must be 18 or older', min: 18, type: 'number' }]} |
| 50 | + > |
| 51 | + <Input placeholder="Enter age" /> |
| 52 | + </FormField> |
| 53 | + |
| 54 | + <FormField |
| 55 | + label="Password" |
| 56 | + name="password" |
| 57 | + rules={[{ message: 'Password must be at least 6 characters', minLength: 6 }]} |
| 58 | + > |
| 59 | + <Input placeholder="Enter password" /> |
| 60 | + </FormField> |
| 61 | + |
| 62 | + <FormField |
| 63 | + label="Info&City" |
| 64 | + name="info.city" |
| 65 | + rules={[{ message: 'Password must be at least 6 characters', minLength: 6 }]} |
| 66 | + > |
| 67 | + <Input placeholder="Enter info.city" /> |
| 68 | + </FormField> |
| 69 | + |
| 70 | + <FormField |
| 71 | + label="Info&company" |
| 72 | + name="info.company" |
| 73 | + rules={[{ message: 'Password must be at least 6 characters', minLength: 6 }]} |
| 74 | + > |
| 75 | + <Input placeholder="Enter info.company" /> |
| 76 | + </FormField> |
| 77 | + </Form> |
| 78 | + |
| 79 | + {/* 4) Dynamic rendering */} |
| 80 | + <div className="mt-4 p-2 border rounded"> |
| 81 | + <h3 className="font-bold">Real-time Watch Results:</h3> |
| 82 | + <p>Username: {username}</p> |
| 83 | + <p>Age: {age}</p> |
| 84 | + <p>Password: {password}</p> |
| 85 | + </div> |
56 | 86 | </div> |
57 | | - </div> |
| 87 | + </Card> |
58 | 88 | ); |
59 | 89 | } |
0 commit comments