Skip to content

Commit d7bace6

Browse files
committed
feat: add the demonstration of using Watch
1 parent c10ff05 commit d7bace6

File tree

1 file changed

+65
-35
lines changed

1 file changed

+65
-35
lines changed
Lines changed: 65 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,89 @@
11
'use client';
22

33
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';
55

66
import { showToastCode } from './toast';
77

8+
type Inputs = {
9+
age: number;
10+
info: { city: string; company: string };
11+
password: string;
12+
username: string;
13+
};
14+
815
export default function WatchDemo() {
9-
const [form] = useForm<{ age: number; password: string; username: string }>();
16+
const [form] = useForm<Inputs>();
1017

1118
// 1) Watch single field
1219
const username = useWatch(form, 'username');
1320

1421
// 2) Watch multiple fields
1522
const { age, password } = useWatch(form, ['age', 'password']);
1623

24+
// 3) Watch nested fields
25+
const info = useWatch(form, 'info');
26+
27+
console.log('info', info);
28+
1729
const { age: NewAge, password: NewPassword, username: NewUsername } = useWatch(form);
1830

1931
useEffect(() => {
2032
showToastCode('NewValues', { NewAge, NewPassword, NewUsername });
2133
}, [NewAge, NewPassword, NewUsername]);
2234

2335
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>
5686
</div>
57-
</div>
87+
</Card>
5888
);
5989
}

0 commit comments

Comments
 (0)