|
| 1 | +--- |
| 2 | +title: Advanced Form Patterns |
| 3 | +description: Advanced form patterns including dynamic fields, custom validation, and field dependencies using rc-field-form. |
| 4 | +--- |
| 5 | + |
| 6 | +import { |
| 7 | + FormFieldTypesPreview, |
| 8 | + FormCustomValidationPreview, |
| 9 | + FormDynamicFieldsPreview, |
| 10 | + FormFieldDependenciesPreview, |
| 11 | + FormAsyncValidationPreview |
| 12 | +} from '@docs/components/examples'; |
| 13 | + |
| 14 | +<ExternalLinks |
| 15 | + links={[ |
| 16 | + { title: 'rc-field-form Docs', url: 'https://github.com/react-component/field-form' }, |
| 17 | + ]} |
| 18 | +/> |
| 19 | + |
| 20 | +<div className="h-5" /> |
| 21 | + |
| 22 | +## Field Types Example |
| 23 | + |
| 24 | +Demonstrates various input field types including text, email, phone, password, and textarea fields. |
| 25 | + |
| 26 | +<Tabs defaultValue="preview"> |
| 27 | + <TabsList> |
| 28 | + <TabsTrigger value="preview">Preview</TabsTrigger> |
| 29 | + <TabsTrigger value="code">Code</TabsTrigger> |
| 30 | + </TabsList> |
| 31 | + <TabsContent value="preview"> |
| 32 | + <PreviewCard preview={<FormFieldTypesPreview />} /> |
| 33 | + </TabsContent> |
| 34 | + <TabsContent value="code"> |
| 35 | + ```json doc-gen:file |
| 36 | + { |
| 37 | + "file": "./node_modules/@rnr/rnr-ui/src/examples/form/form-field-types.tsx", |
| 38 | + "codeblock": { |
| 39 | + "lang": "tsx" |
| 40 | + } |
| 41 | + } |
| 42 | + ``` |
| 43 | + </TabsContent> |
| 44 | +</Tabs> |
| 45 | + |
| 46 | +## Custom Validation Example |
| 47 | + |
| 48 | +Shows how to implement custom validation rules with complex business logic and field dependencies. |
| 49 | + |
| 50 | +<Tabs defaultValue="preview"> |
| 51 | + <TabsList> |
| 52 | + <TabsTrigger value="preview">Preview</TabsTrigger> |
| 53 | + <TabsTrigger value="code">Code</TabsTrigger> |
| 54 | + </TabsList> |
| 55 | + <TabsContent value="preview"> |
| 56 | + <PreviewCard preview={<FormCustomValidationPreview />} /> |
| 57 | + </TabsContent> |
| 58 | + <TabsContent value="code"> |
| 59 | + ```json doc-gen:file |
| 60 | + { |
| 61 | + "file": "./node_modules/@rnr/rnr-ui/src/examples/form/form-custom-validation.tsx", |
| 62 | + "codeblock": { |
| 63 | + "lang": "tsx" |
| 64 | + } |
| 65 | + } |
| 66 | + ``` |
| 67 | + </TabsContent> |
| 68 | +</Tabs> |
| 69 | + |
| 70 | +## Dynamic Fields Example |
| 71 | + |
| 72 | +Demonstrates how to create forms with dynamically added and removed fields. |
| 73 | + |
| 74 | +<Tabs defaultValue="preview"> |
| 75 | + <TabsList> |
| 76 | + <TabsTrigger value="preview">Preview</TabsTrigger> |
| 77 | + <TabsTrigger value="code">Code</TabsTrigger> |
| 78 | + </TabsList> |
| 79 | + <TabsContent value="preview"> |
| 80 | + <PreviewCard preview={<FormDynamicFieldsPreview />} /> |
| 81 | + </TabsContent> |
| 82 | + <TabsContent value="code"> |
| 83 | + ```json doc-gen:file |
| 84 | + { |
| 85 | + "file": "./node_modules/@rnr/rnr-ui/src/examples/form/form-dynamic-fields.tsx", |
| 86 | + "codeblock": { |
| 87 | + "lang": "tsx" |
| 88 | + } |
| 89 | + } |
| 90 | + ``` |
| 91 | + </TabsContent> |
| 92 | +</Tabs> |
| 93 | + |
| 94 | +## Field Dependencies Example |
| 95 | + |
| 96 | +Shows how to create conditional fields and validation based on other field values. |
| 97 | + |
| 98 | +<Tabs defaultValue="preview"> |
| 99 | + <TabsList> |
| 100 | + <TabsTrigger value="preview">Preview</TabsTrigger> |
| 101 | + <TabsTrigger value="code">Code</TabsTrigger> |
| 102 | + </TabsList> |
| 103 | + <TabsContent value="preview"> |
| 104 | + <PreviewCard preview={<FormFieldDependenciesPreview />} /> |
| 105 | + </TabsContent> |
| 106 | + <TabsContent value="code"> |
| 107 | + ```json doc-gen:file |
| 108 | + { |
| 109 | + "file": "./node_modules/@rnr/rnr-ui/src/examples/form/form-field-dependencies.tsx", |
| 110 | + "codeblock": { |
| 111 | + "lang": "tsx" |
| 112 | + } |
| 113 | + } |
| 114 | + ``` |
| 115 | + </TabsContent> |
| 116 | +</Tabs> |
| 117 | + |
| 118 | +## Async Validation Example |
| 119 | + |
| 120 | +Demonstrates asynchronous validation with simulated API calls for username, email, and phone validation. |
| 121 | + |
| 122 | +<Tabs defaultValue="preview"> |
| 123 | + <TabsList> |
| 124 | + <TabsTrigger value="preview">Preview</TabsTrigger> |
| 125 | + <TabsTrigger value="code">Code</TabsTrigger> |
| 126 | + </TabsList> |
| 127 | + <TabsContent value="preview"> |
| 128 | + <PreviewCard preview={<FormAsyncValidationPreview />} /> |
| 129 | + </TabsContent> |
| 130 | + <TabsContent value="code"> |
| 131 | + ```json doc-gen:file |
| 132 | + { |
| 133 | + "file": "./node_modules/@rnr/rnr-ui/src/examples/form/form-async-validation.tsx", |
| 134 | + "codeblock": { |
| 135 | + "lang": "tsx" |
| 136 | + } |
| 137 | + } |
| 138 | + ``` |
| 139 | + </TabsContent> |
| 140 | +</Tabs> |
| 141 | + |
| 142 | +## Key Concepts |
| 143 | + |
| 144 | +### Custom Validators |
| 145 | + |
| 146 | +Custom validators receive three parameters: `rule`, `value`, and `callback`. They should return a Promise that resolves when validation passes or rejects with an error message when validation fails. |
| 147 | + |
| 148 | +```tsx |
| 149 | +const validateUsername = async (_: any, value: string) => { |
| 150 | + if (!value) { |
| 151 | + return Promise.reject(new Error('Username is required')); |
| 152 | + } |
| 153 | + // Additional validation logic |
| 154 | + return Promise.resolve(); |
| 155 | +}; |
| 156 | + |
| 157 | +<Form.Item |
| 158 | + name="username" |
| 159 | + label="Username" |
| 160 | + rules={[{ validator: validateUsername }]} |
| 161 | +> |
| 162 | + <TextInput placeholder="Enter username" /> |
| 163 | +</Form.Item> |
| 164 | +``` |
| 165 | + |
| 166 | +### Field Dependencies |
| 167 | + |
| 168 | +Use `dependencies` to make one field depend on another. The dependent field will re-validate when the dependency changes. |
| 169 | + |
| 170 | +```tsx |
| 171 | +<Form.Item |
| 172 | + name="confirmPassword" |
| 173 | + label="Confirm Password" |
| 174 | + dependencies={['password']} |
| 175 | + rules={[{ validator: validateConfirmPassword }]} |
| 176 | +> |
| 177 | + <TextInput placeholder="Confirm password" secureTextEntry /> |
| 178 | +</Form.Item> |
| 179 | +``` |
| 180 | + |
| 181 | +### Dynamic Fields |
| 182 | + |
| 183 | +Manage arrays of fields using React state and render them dynamically with proper field names. |
| 184 | + |
| 185 | +```tsx |
| 186 | +{emails.map((_, index) => ( |
| 187 | + <Form.Item |
| 188 | + key={index} |
| 189 | + name={['emails', index]} |
| 190 | + rules={[{ required: true, message: 'Email is required' }]} |
| 191 | + > |
| 192 | + <TextInput placeholder={`Email ${index + 1}`} /> |
| 193 | + </Form.Item> |
| 194 | +))} |
| 195 | +``` |
| 196 | + |
| 197 | +### Async Validation |
| 198 | + |
| 199 | +Async validators can perform API calls or other asynchronous operations. Use `validateTrigger="onBlur"` to validate on field blur instead of every change. |
| 200 | + |
| 201 | +```tsx |
| 202 | +<Form.Item |
| 203 | + name="username" |
| 204 | + label="Username" |
| 205 | + rules={[{ validator: validateUsername }]} |
| 206 | + validateTrigger="onBlur" |
| 207 | +> |
| 208 | + <TextInput placeholder="Enter username" /> |
| 209 | +</Form.Item> |
| 210 | +``` |
| 211 | + |
| 212 | +## Best Practices |
| 213 | + |
| 214 | +1. **Use `validateTrigger="onBlur"`** for async validation to avoid excessive API calls |
| 215 | +2. **Provide clear error messages** that help users understand what they need to fix |
| 216 | +3. **Use field dependencies** to create conditional validation logic |
| 217 | +4. **Handle loading states** during async validation to improve user experience |
| 218 | +5. **Validate on the client first** before making async calls to reduce server load |
| 219 | +6. **Use proper TypeScript types** for better development experience and error catching |
0 commit comments