rhf-stepper 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +379 -0
- package/dist/index.js +11 -7
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +11 -7
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
# rhf-stepper
|
|
2
|
+
|
|
3
|
+
A lightweight multi-step form helper for [react-hook-form](https://react-hook-form.com). Build wizards, multi-step forms, and stepped workflows with automatic per-step validation.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install rhf-stepper
|
|
9
|
+
# or
|
|
10
|
+
pnpm add rhf-stepper
|
|
11
|
+
# or
|
|
12
|
+
yarn add rhf-stepper
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
### Peer Dependencies
|
|
16
|
+
|
|
17
|
+
```json
|
|
18
|
+
{
|
|
19
|
+
"react": ">=17.0.0",
|
|
20
|
+
"react-hook-form": ">=7.0.0"
|
|
21
|
+
}
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
```tsx
|
|
27
|
+
import { useForm } from 'react-hook-form'
|
|
28
|
+
import { Form, Step, Controller, useFormContext } from 'rhf-stepper'
|
|
29
|
+
|
|
30
|
+
type FormValues = {
|
|
31
|
+
name: string
|
|
32
|
+
email: string
|
|
33
|
+
address: string
|
|
34
|
+
city: string
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function MyMultiStepForm() {
|
|
38
|
+
const form = useForm<FormValues>()
|
|
39
|
+
|
|
40
|
+
return (
|
|
41
|
+
<Form form={form} onSubmit={(data) => console.log(data)}>
|
|
42
|
+
{({ currentStep }) => (
|
|
43
|
+
<>
|
|
44
|
+
<Step>
|
|
45
|
+
{currentStep === 0 && (
|
|
46
|
+
<>
|
|
47
|
+
<Controller name="name" render={({ field }) => <input {...field} placeholder="Name" />} />
|
|
48
|
+
<Controller name="email" render={({ field }) => <input {...field} placeholder="Email" />} />
|
|
49
|
+
</>
|
|
50
|
+
)}
|
|
51
|
+
</Step>
|
|
52
|
+
|
|
53
|
+
<Step>
|
|
54
|
+
{currentStep === 1 && (
|
|
55
|
+
<>
|
|
56
|
+
<Controller name="address" render={({ field }) => <input {...field} placeholder="Address" />} />
|
|
57
|
+
<Controller name="city" render={({ field }) => <input {...field} placeholder="City" />} />
|
|
58
|
+
</>
|
|
59
|
+
)}
|
|
60
|
+
</Step>
|
|
61
|
+
|
|
62
|
+
<StepNavigation />
|
|
63
|
+
</>
|
|
64
|
+
)}
|
|
65
|
+
</Form>
|
|
66
|
+
)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function StepNavigation() {
|
|
70
|
+
const { next, prev, isFirstStep, isLastStep } = useFormContext()
|
|
71
|
+
|
|
72
|
+
return (
|
|
73
|
+
<div>
|
|
74
|
+
{!isFirstStep && <button type="button" onClick={prev}>Back</button>}
|
|
75
|
+
{!isLastStep && <button type="button" onClick={next}>Next</button>}
|
|
76
|
+
{isLastStep && <button type="submit">Submit</button>}
|
|
77
|
+
</div>
|
|
78
|
+
)
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
> **Important:** `<Step>` components must always be mounted in the tree. Conditionally render the `<Controller>` fields inside each `<Step>` based on `currentStep` to show/hide step content.
|
|
83
|
+
|
|
84
|
+
## API Reference
|
|
85
|
+
|
|
86
|
+
### `<Form>`
|
|
87
|
+
|
|
88
|
+
The root component that replaces the standard `<form>` element. Manages step state and validation.
|
|
89
|
+
|
|
90
|
+
```tsx
|
|
91
|
+
<Form
|
|
92
|
+
form={form}
|
|
93
|
+
onSubmit={handleSubmit}
|
|
94
|
+
stepValidationMode="forward"
|
|
95
|
+
>
|
|
96
|
+
{children}
|
|
97
|
+
</Form>
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
#### Props
|
|
101
|
+
|
|
102
|
+
| Prop | Type | Default | Description |
|
|
103
|
+
|------|------|---------|-------------|
|
|
104
|
+
| `form` | `UseFormReturn<T>` | *required* | The form instance from `useForm()` |
|
|
105
|
+
| `onSubmit` | `(values: T) => void` | *required* | Called with form values on valid submission |
|
|
106
|
+
| `stepValidationMode` | `'forward' \| 'all' \| 'none'` | `'forward'` | When to validate step fields (see below) |
|
|
107
|
+
| `children` | `ReactNode \| (context) => ReactNode` | *required* | Form content. Accepts a render function for context access |
|
|
108
|
+
| `ref` | `Ref<HTMLFormElement>` | - | Ref forwarded to the underlying `<form>` element |
|
|
109
|
+
|
|
110
|
+
All other props are passed through to the underlying `<form>` element (e.g., `className`, `id`).
|
|
111
|
+
|
|
112
|
+
#### Step Validation Modes
|
|
113
|
+
|
|
114
|
+
| Mode | Description |
|
|
115
|
+
|------|-------------|
|
|
116
|
+
| `'forward'` | Validates current step fields only when navigating forward (via `next()` or `setCurrentStep` to a later step) |
|
|
117
|
+
| `'all'` | Validates current step fields on every navigation (forward and backward) |
|
|
118
|
+
| `'none'` | No automatic validation. Navigate freely between steps |
|
|
119
|
+
|
|
120
|
+
#### Render Function
|
|
121
|
+
|
|
122
|
+
When using a render function as children, you receive the full `FormContextValue`:
|
|
123
|
+
|
|
124
|
+
```tsx
|
|
125
|
+
<Form form={form} onSubmit={handleSubmit}>
|
|
126
|
+
{({ currentStep, isFirstStep, isLastStep, next, prev }) => (
|
|
127
|
+
// render steps based on currentStep
|
|
128
|
+
)}
|
|
129
|
+
</Form>
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
---
|
|
133
|
+
|
|
134
|
+
### `<Step>`
|
|
135
|
+
|
|
136
|
+
Groups `<Controller>` fields into a logical step. Fields inside a `<Step>` are automatically registered and validated together. Each `<Step>` defines a step boundary -- conditionally render its children based on `currentStep` to control which step is visible.
|
|
137
|
+
|
|
138
|
+
> **Important:** `<Step>` components must always be mounted in the tree. Do not conditionally render the `<Step>` itself -- only its children.
|
|
139
|
+
|
|
140
|
+
```tsx
|
|
141
|
+
<Step>
|
|
142
|
+
{currentStep === 0 && (
|
|
143
|
+
<>
|
|
144
|
+
<Controller name="firstName" render={({ field }) => <input {...field} />} />
|
|
145
|
+
<Controller name="lastName" render={({ field }) => <input {...field} />} />
|
|
146
|
+
</>
|
|
147
|
+
)}
|
|
148
|
+
</Step>
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Steps can be nested for sub-step grouping:
|
|
152
|
+
|
|
153
|
+
```tsx
|
|
154
|
+
<Step>
|
|
155
|
+
<Step>
|
|
156
|
+
{currentStep === 0 && (
|
|
157
|
+
<Controller name="a" render={({ field }) => <input {...field} />} />
|
|
158
|
+
)}
|
|
159
|
+
</Step>
|
|
160
|
+
<Step>
|
|
161
|
+
{currentStep === 1 && (
|
|
162
|
+
<Controller name="b" render={({ field }) => <input {...field} />} />
|
|
163
|
+
)}
|
|
164
|
+
</Step>
|
|
165
|
+
</Step>
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
---
|
|
169
|
+
|
|
170
|
+
### `<Controller>`
|
|
171
|
+
|
|
172
|
+
A drop-in replacement for react-hook-form's `Controller`. It automatically registers the field with the nearest `<Step>` for step-aware validation.
|
|
173
|
+
|
|
174
|
+
```tsx
|
|
175
|
+
<Controller
|
|
176
|
+
name="email"
|
|
177
|
+
rules={{ required: 'Email is required' }}
|
|
178
|
+
render={({ field, fieldState }) => (
|
|
179
|
+
<div>
|
|
180
|
+
<input {...field} />
|
|
181
|
+
{fieldState.error && <span>{fieldState.error.message}</span>}
|
|
182
|
+
</div>
|
|
183
|
+
)}
|
|
184
|
+
/>
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
#### Props
|
|
188
|
+
|
|
189
|
+
Same as [react-hook-form's Controller](https://react-hook-form.com/docs/usecontroller/controller). The `control` prop is optional -- it's automatically resolved from the `<Form>` context.
|
|
190
|
+
|
|
191
|
+
---
|
|
192
|
+
|
|
193
|
+
### `useFormContext()`
|
|
194
|
+
|
|
195
|
+
Hook to access the stepper state from any component inside `<Form>`.
|
|
196
|
+
|
|
197
|
+
```tsx
|
|
198
|
+
const {
|
|
199
|
+
form,
|
|
200
|
+
currentStep,
|
|
201
|
+
setCurrentStep,
|
|
202
|
+
currentStepNode,
|
|
203
|
+
currentStepArr,
|
|
204
|
+
validatedFields,
|
|
205
|
+
isFirstStep,
|
|
206
|
+
isLastStep,
|
|
207
|
+
next,
|
|
208
|
+
prev,
|
|
209
|
+
} = useFormContext<MyFormValues>()
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
#### Return Value (`FormContextValue<T>`)
|
|
213
|
+
|
|
214
|
+
| Property | Type | Description |
|
|
215
|
+
|----------|------|-------------|
|
|
216
|
+
| `form` | `UseFormReturn<T>` | The react-hook-form instance |
|
|
217
|
+
| `currentStep` | `number \| number[] \| null` | Current step index. `number[]` for nested steps |
|
|
218
|
+
| `setCurrentStep` | `(step: number \| number[]) => Promise<void>` | Navigate to a specific step. Validates before navigating (based on `stepValidationMode`) |
|
|
219
|
+
| `currentStepNode` | `StepTree \| undefined` | The current step's node in the step tree |
|
|
220
|
+
| `currentStepArr` | `string[] \| null` | Field names registered in the current step |
|
|
221
|
+
| `validatedFields` | `string[]` | Field names that have been validated via step navigation |
|
|
222
|
+
| `isFirstStep` | `boolean` | `true` if on the first step |
|
|
223
|
+
| `isLastStep` | `boolean` | `true` if on the last step |
|
|
224
|
+
| `next` | `() => void` | Navigate to the next step (validates current step first) |
|
|
225
|
+
| `prev` | `() => void` | Navigate to the previous step |
|
|
226
|
+
|
|
227
|
+
---
|
|
228
|
+
|
|
229
|
+
## Examples
|
|
230
|
+
|
|
231
|
+
### Basic Two-Step Form
|
|
232
|
+
|
|
233
|
+
```tsx
|
|
234
|
+
import { useForm } from 'react-hook-form'
|
|
235
|
+
import { Form, Step, Controller, useFormContext } from 'rhf-stepper'
|
|
236
|
+
|
|
237
|
+
type SignupForm = {
|
|
238
|
+
email: string
|
|
239
|
+
password: string
|
|
240
|
+
firstName: string
|
|
241
|
+
lastName: string
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function SignupWizard() {
|
|
245
|
+
const form = useForm<SignupForm>()
|
|
246
|
+
|
|
247
|
+
return (
|
|
248
|
+
<Form form={form} onSubmit={(data) => console.log(data)}>
|
|
249
|
+
{({ currentStep }) => (
|
|
250
|
+
<>
|
|
251
|
+
<Step>
|
|
252
|
+
{currentStep === 0 && (
|
|
253
|
+
<>
|
|
254
|
+
<Controller
|
|
255
|
+
name="email"
|
|
256
|
+
rules={{ required: 'Email is required' }}
|
|
257
|
+
render={({ field, fieldState }) => (
|
|
258
|
+
<div>
|
|
259
|
+
<input {...field} type="email" placeholder="Email" />
|
|
260
|
+
{fieldState.error && <p>{fieldState.error.message}</p>}
|
|
261
|
+
</div>
|
|
262
|
+
)}
|
|
263
|
+
/>
|
|
264
|
+
<Controller
|
|
265
|
+
name="password"
|
|
266
|
+
rules={{ required: 'Password is required', minLength: { value: 8, message: 'Min 8 characters' } }}
|
|
267
|
+
render={({ field, fieldState }) => (
|
|
268
|
+
<div>
|
|
269
|
+
<input {...field} type="password" placeholder="Password" />
|
|
270
|
+
{fieldState.error && <p>{fieldState.error.message}</p>}
|
|
271
|
+
</div>
|
|
272
|
+
)}
|
|
273
|
+
/>
|
|
274
|
+
</>
|
|
275
|
+
)}
|
|
276
|
+
</Step>
|
|
277
|
+
|
|
278
|
+
<Step>
|
|
279
|
+
{currentStep === 1 && (
|
|
280
|
+
<>
|
|
281
|
+
<Controller
|
|
282
|
+
name="firstName"
|
|
283
|
+
rules={{ required: 'First name is required' }}
|
|
284
|
+
render={({ field }) => <input {...field} placeholder="First Name" />}
|
|
285
|
+
/>
|
|
286
|
+
<Controller
|
|
287
|
+
name="lastName"
|
|
288
|
+
rules={{ required: 'Last name is required' }}
|
|
289
|
+
render={({ field }) => <input {...field} placeholder="Last Name" />}
|
|
290
|
+
/>
|
|
291
|
+
</>
|
|
292
|
+
)}
|
|
293
|
+
</Step>
|
|
294
|
+
|
|
295
|
+
<Navigation />
|
|
296
|
+
</>
|
|
297
|
+
)}
|
|
298
|
+
</Form>
|
|
299
|
+
)
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function Navigation() {
|
|
303
|
+
const { next, prev, isFirstStep, isLastStep } = useFormContext()
|
|
304
|
+
|
|
305
|
+
return (
|
|
306
|
+
<div>
|
|
307
|
+
{!isFirstStep && <button type="button" onClick={prev}>Back</button>}
|
|
308
|
+
{!isLastStep && <button type="button" onClick={next}>Next</button>}
|
|
309
|
+
{isLastStep && <button type="submit">Submit</button>}
|
|
310
|
+
</div>
|
|
311
|
+
)
|
|
312
|
+
}
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
### Jump to a Specific Step
|
|
316
|
+
|
|
317
|
+
```tsx
|
|
318
|
+
function StepIndicator() {
|
|
319
|
+
const { currentStep, setCurrentStep } = useFormContext()
|
|
320
|
+
|
|
321
|
+
return (
|
|
322
|
+
<nav>
|
|
323
|
+
<button type="button" onClick={() => setCurrentStep(0)}>Account</button>
|
|
324
|
+
<button type="button" onClick={() => setCurrentStep(1)}>Profile</button>
|
|
325
|
+
<button type="button" onClick={() => setCurrentStep(2)}>Review</button>
|
|
326
|
+
</nav>
|
|
327
|
+
)
|
|
328
|
+
}
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
### Skip Validation
|
|
332
|
+
|
|
333
|
+
```tsx
|
|
334
|
+
<Form form={form} onSubmit={handleSubmit} stepValidationMode="none">
|
|
335
|
+
{/* Users can freely navigate between steps without validation */}
|
|
336
|
+
</Form>
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
### Accessing the Form Instance
|
|
340
|
+
|
|
341
|
+
`useFormContext` returns the full `react-hook-form` instance under the `form` property:
|
|
342
|
+
|
|
343
|
+
```tsx
|
|
344
|
+
function ResetButton() {
|
|
345
|
+
const { form } = useFormContext<MyFormValues>()
|
|
346
|
+
|
|
347
|
+
return (
|
|
348
|
+
<button type="button" onClick={() => form.reset()}>
|
|
349
|
+
Reset
|
|
350
|
+
</button>
|
|
351
|
+
)
|
|
352
|
+
}
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
## Types
|
|
356
|
+
|
|
357
|
+
```ts
|
|
358
|
+
import type {
|
|
359
|
+
FormContextValue,
|
|
360
|
+
FormProps,
|
|
361
|
+
ControllerProps,
|
|
362
|
+
ControllerRenderArgs,
|
|
363
|
+
StepTree,
|
|
364
|
+
StepValidationMode,
|
|
365
|
+
} from 'rhf-stepper'
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
| Type | Description |
|
|
369
|
+
|------|-------------|
|
|
370
|
+
| `FormContextValue<T>` | Return type of `useFormContext()` |
|
|
371
|
+
| `FormProps<T>` | Props for the `<Form>` component |
|
|
372
|
+
| `ControllerProps<T, N>` | Props for the `<Controller>` component |
|
|
373
|
+
| `ControllerRenderArgs<T, N>` | Arguments passed to the Controller `render` function |
|
|
374
|
+
| `StepTree` | Recursive type representing the step structure (`string \| StepTree[]`) |
|
|
375
|
+
| `StepValidationMode` | `'all' \| 'forward' \| 'none'` |
|
|
376
|
+
|
|
377
|
+
## License
|
|
378
|
+
|
|
379
|
+
MIT
|
package/dist/index.js
CHANGED
|
@@ -19,6 +19,10 @@ function Step({ children }) {
|
|
|
19
19
|
changeStepAtIndex: changeStepAtIndexFromParent,
|
|
20
20
|
registrationKey: registrationKeyFromParent
|
|
21
21
|
} = stepContext != null ? stepContext : formContext;
|
|
22
|
+
const isRoot = react.useMemo(() => {
|
|
23
|
+
if (!stepContext) return true;
|
|
24
|
+
return false;
|
|
25
|
+
}, [stepContext]);
|
|
22
26
|
const stepRef = react.useRef(void 0);
|
|
23
27
|
const [steps, setSteps] = react.useState([]);
|
|
24
28
|
const [registrationKey, setRegistrationKey] = react.useState(0);
|
|
@@ -30,14 +34,14 @@ function Step({ children }) {
|
|
|
30
34
|
});
|
|
31
35
|
}, []);
|
|
32
36
|
react.useEffect(() => {
|
|
33
|
-
if (steps.length
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
if (isRoot && steps.length === 0) return;
|
|
38
|
+
const stepList = steps.length ? steps : [""];
|
|
39
|
+
if (stepRef.current !== void 0) {
|
|
40
|
+
changeStepAtIndexFromParent(stepList, stepRef.current);
|
|
41
|
+
} else {
|
|
42
|
+
registerStepFromParent(stepList, stepRef);
|
|
39
43
|
}
|
|
40
|
-
}, [registrationKeyFromParent, steps]);
|
|
44
|
+
}, [registrationKeyFromParent, steps, isRoot]);
|
|
41
45
|
react.useEffect(() => {
|
|
42
46
|
if (stepFromParent !== void 0) {
|
|
43
47
|
rebuildStepsFromParent();
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/step.tsx","../src/form.tsx","../src/controller.tsx"],"names":["createContext","useContext","useRef","useState","useCallback","steps","useEffect","stepRef","useMemo","prev","jsx","forwardRef","RHFController"],"mappings":";;;;;;;AAcA,IAAM,WAAA,GAAcA,oBAAuC,IAAI,CAAA;AAE/D,SAAS,OAAA,GAAmC;AAC1C,EAAA,OAAOC,iBAAW,WAAW,CAAA;AAC/B;AAEA,SAAS,IAAA,CAAK,EAAE,QAAA,EAAS,EAAkC;AACzD,EAAA,MAAM,cAAc,OAAA,EAAQ;AAC5B,EAAA,MAAM,cAAc,sBAAA,EAAuB;AAE3C,EAAA,MAAM;AAAA,IACJ,YAAA,EAAc,sBAAA;AAAA,IACd,IAAA,EAAM,cAAA;AAAA,IACN,YAAA,EAAc,sBAAA;AAAA,IACd,iBAAA,EAAmB,2BAAA;AAAA,IACnB,eAAA,EAAiB;AAAA,MACf,WAAA,IAAA,IAAA,GAAA,WAAA,GAAe,WAAA;AACnB,EAAA,MAAM,OAAA,GAAUC,aAA2B,MAAS,CAAA;AACpD,EAAA,MAAM,CAAC,KAAA,EAAO,QAAQ,CAAA,GAAIC,cAAA,CAA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;AACA,IAAA,OAAO,MAAM;AACX,MAAA,WAAA,IAAA,IAAA,GAAA,MAAA,GAAA,WAAA,CAAa,YAAA,EAAA;AAAA,IACf,CAAA;AAAA,EACF,CAAA,EAAG,EAAE,CAAA;AAEL,EAAA,uBAAOI,cAAAA,CAACE,wBAAA,EAAA,EAAc,MAAY,OAAA,EAAS,eAAA,EAAkB,GAAG,IAAA,EAAM,CAAA;AACxE;AAEA,UAAA,CAAW,WAAA,GAAc,YAAA","file":"index.js","sourcesContent":["import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react'\n\nimport { useInternalFormContext } from './form'\nimport { type StepTree } from './types'\n\ntype StepContextValue = {\n step?: number\n registrationKey: number\n registerField: (elements: StepTree) => void\n registerStep: (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number) => void\n rebuildSteps: () => void\n changeStepAtIndex: (steps: StepTree, index: number) => void\n}\n\nconst StepContext = createContext<StepContextValue | null>(null)\n\nfunction useStep(): StepContextValue | null {\n return useContext(StepContext)\n}\n\nfunction Step({ children }: { children: React.ReactNode }) {\n const stepContext = useStep()\n const formContext = useInternalFormContext()\n\n const {\n registerStep: registerStepFromParent,\n step: stepFromParent,\n rebuildSteps: rebuildStepsFromParent,\n changeStepAtIndex: changeStepAtIndexFromParent,\n registrationKey: registrationKeyFromParent,\n } = stepContext ?? formContext\n const stepRef = useRef<number | undefined>(undefined)\n const [steps, setSteps] = useState<StepTree>([])\n const [registrationKey, setRegistrationKey] = useState(0)\n\n const changeStepAtIndex = useCallback((steps: StepTree, index: number): void => {\n setSteps((prevSteps) => {\n const newSteps = [...prevSteps]\n newSteps[index] = steps\n return newSteps\n })\n }, [])\n\n useEffect(() => {\n if (steps.length > 0) {\n if (stepRef.current !== undefined) {\n changeStepAtIndexFromParent(steps, stepRef.current)\n } else {\n registerStepFromParent(steps, stepRef)\n }\n }\n }, [registrationKeyFromParent, steps])\n\n useEffect(() => {\n if (stepFromParent !== undefined) {\n rebuildStepsFromParent()\n }\n return () => {\n rebuildStepsFromParent()\n }\n }, [])\n\n const registerField = useCallback(\n (elements: StepTree): void => {\n setSteps((prevSteps) => {\n return [...prevSteps, ...elements]\n })\n },\n [steps],\n )\n\n const registerStep = useCallback(\n (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number): void => {\n setSteps((prevSteps: StepTree) => {\n const stepNumber = step ?? prevSteps.length\n stepRef.current = stepNumber\n const newSteps = [...prevSteps]\n newSteps.splice(stepNumber, 0, elements)\n return newSteps\n })\n },\n [steps],\n )\n\n const rebuildSteps = useCallback(() => {\n setSteps([])\n setRegistrationKey((prev) => prev + 1)\n }, [])\n\n const contextValue = useMemo<StepContextValue>(\n () => ({\n // eslint-disable-next-line react-hooks/refs\n step: stepRef.current,\n registrationKey,\n changeStepAtIndex,\n registerField,\n registerStep,\n rebuildSteps,\n }),\n [registrationKey, changeStepAtIndex, registerField, registerStep, rebuildSteps],\n )\n\n // eslint-disable-next-line react-hooks/refs\n return <StepContext.Provider value={contextValue}>{children}</StepContext.Provider>\n}\n\nStep.displayName = 'Step'\n\nexport { Step, useStep }\nexport type { StepContextValue }\n","import { createContext, forwardRef, useCallback, useContext, useMemo, useRef, useState } from 'react'\nimport { type FieldValues, Path, type UseFormReturn } from 'react-hook-form'\n\nimport { Step } from './step'\nimport { type StepTree, type StepValidationMode } from './types'\n\nfunction comparePaths(a: number[], b: number[]): number {\n for (let i = 0; i < Math.max(a.length, b.length); i++) {\n const ai = a[i] ?? -1\n const bi = b[i] ?? -1\n if (ai !== bi) return ai - bi\n }\n return 0\n}\n\nfunction getNodeAtPath(tree: StepTree, path: number[]): StepTree | undefined {\n let current: StepTree = tree\n for (const index of path) {\n if (!Array.isArray(current) || index >= current.length) return undefined\n current = current[index]\n }\n return current\n}\n\nfunction isLeafParent(node: StepTree): node is string[] {\n return Array.isArray(node) && node.length > 0 && node.every((child) => typeof child === 'string')\n}\n\nfunction getFirstLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const node = getNodeAtPath(tree, path)\n if (node === undefined) return null\n if (isLeafParent(node)) return path\n if (Array.isArray(node) && node.length > 0) {\n for (let i = 0; i < node.length; i++) {\n const found = getFirstLeafParentPath(tree, [...path, i])\n if (found) return found\n }\n }\n return null\n}\n\nfunction getLastLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const node = getNodeAtPath(tree, path)\n if (node === undefined) return null\n if (isLeafParent(node)) return path\n if (Array.isArray(node) && node.length > 0) {\n for (let i = node.length - 1; i >= 0; i--) {\n const found = getLastLeafParentPath(tree, [...path, i])\n if (found) return found\n }\n }\n return null\n}\n\nfunction getPrevLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const current = [...path]\n while (current.length > 0) {\n const lastIndex = current[current.length - 1]\n const parentPath = current.slice(0, -1)\n const parent = getNodeAtPath(tree, parentPath)\n\n if (Array.isArray(parent)) {\n for (let i = lastIndex - 1; i >= 0; i--) {\n const found = getLastLeafParentPath(tree, [...parentPath, i])\n if (found) return found\n }\n }\n\n current.pop()\n }\n\n return null\n}\n\nfunction getNextLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const current = [...path]\n while (current.length > 0) {\n const lastIndex = current[current.length - 1]\n const parentPath = current.slice(0, -1)\n const parent = getNodeAtPath(tree, parentPath)\n\n if (Array.isArray(parent)) {\n for (let i = lastIndex + 1; i < parent.length; i++) {\n const found = getFirstLeafParentPath(tree, [...parentPath, i])\n if (found) return found\n }\n }\n\n current.pop()\n }\n\n return null\n}\n\ntype FormContextValue<TFieldValues extends FieldValues = FieldValues> = {\n form: UseFormReturn<TFieldValues>\n currentStep: number | number[] | null\n setCurrentStep: (step: number | number[]) => Promise<void>\n currentStepNode: StepTree | undefined\n currentStepArr: string[] | null\n validatedFields: string[]\n isFirstStep: boolean\n isLastStep: boolean\n next: () => void\n prev: () => void\n}\n\ntype InternalFormContextValue = {\n step?: number\n registrationKey: number\n rebuildSteps: () => void\n registerStep: (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number) => void\n changeStepAtIndex: (elements: StepTree, index: number) => void\n}\n\nconst FormContext = createContext<FormContextValue | null>(null)\nconst InternalFormContext = createContext<InternalFormContextValue | null>(null)\n\nfunction useFormContext<TFieldValues extends FieldValues = FieldValues>(): FormContextValue<TFieldValues> {\n const context = useContext(FormContext)\n if (!context) {\n throw new Error('useFormContext must be used within a <Form>')\n }\n return context as unknown as FormContextValue<TFieldValues>\n}\n\nfunction useInternalFormContext(): InternalFormContextValue {\n const context = useContext(InternalFormContext)\n if (!context) {\n throw new Error('useInternalFormContext must be used within a <Form>')\n }\n return context\n}\n\ninterface FormProps<TFieldValues extends FieldValues = FieldValues>\n extends Omit<React.ComponentProps<'form'>, 'onSubmit' | 'children'> {\n form: UseFormReturn<TFieldValues>\n onSubmit: (values: TFieldValues) => void\n stepValidationMode?: StepValidationMode\n children: React.ReactNode | ((context: FormContextValue<TFieldValues>) => React.ReactNode)\n}\n\nfunction FormInner<TFieldValues extends FieldValues = FieldValues>(\n { form, onSubmit, stepValidationMode = 'forward', children, ...props }: FormProps<TFieldValues>,\n ref: React.Ref<HTMLFormElement>,\n) {\n const [steps, setSteps] = useState<StepTree>([])\n const [currentStep, setCurrentStep] = useState<number[] | null>(null)\n const [validatedFields, setValidatedFields] = useState<string[]>([])\n const [registrationKey, setRegistrationKey] = useState(0)\n\n const stepRef = useRef<number | undefined>(undefined)\n\n const _currentStep = useMemo<number | number[] | null>(() => {\n if (!currentStep) return null\n const sliced = currentStep.slice(1)\n return sliced.length === 1 ? sliced[0] : sliced\n }, [currentStep])\n\n const currentStepNode = useMemo(\n () => (currentStep ? getNodeAtPath(steps, currentStep) : undefined),\n [steps, currentStep],\n )\n\n const currentStepArr = useMemo<string[] | null>(() => {\n if (!currentStepNode) return null\n return isLeafParent(currentStepNode) ? currentStepNode : null\n }, [currentStepNode])\n\n const _setCurrentStep = useCallback(\n async (step: number | number[]) => {\n const path = typeof step === 'number' ? [0, step] : [0, ...step]\n if (currentStep && currentStepArr && stepValidationMode !== 'none') {\n const isForward = comparePaths(path, currentStep) > 0\n const shouldValidate = stepValidationMode === 'all' || (stepValidationMode === 'forward' && isForward)\n if (shouldValidate) {\n const isValid = await form.trigger(currentStepArr as Path<TFieldValues>[])\n if (!isValid) {\n setValidatedFields((prev) => prev.filter((field) => !currentStepArr.includes(field)))\n return\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n }\n setCurrentStep(path)\n },\n [currentStep, currentStepArr, form, stepValidationMode],\n )\n\n const isFirstStep = useMemo(\n () => (currentStep ? getPrevLeafParentPath(steps, currentStep) === null : true),\n [steps, currentStep],\n )\n\n const isLastStep = useMemo(\n () => (currentStep ? getNextLeafParentPath(steps, currentStep) === null : true),\n [steps, currentStep],\n )\n\n const next = useCallback(async () => {\n if (!currentStep) return\n const nextPath = getNextLeafParentPath(steps, currentStep)\n if (currentStepArr && stepValidationMode !== 'none') {\n const isValid = await form.trigger(currentStepArr as Path<TFieldValues>[])\n if (!isValid) {\n setValidatedFields((prev) => prev.filter((field) => !currentStepArr.includes(field)))\n return\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n if (nextPath) {\n setCurrentStep(nextPath)\n }\n }, [steps, currentStep, currentStepArr, form, stepValidationMode])\n\n const prev = useCallback(() => {\n if (!currentStep) return\n const prevPath = getPrevLeafParentPath(steps, currentStep)\n if (prevPath) {\n setCurrentStep(prevPath)\n }\n }, [steps, currentStep])\n\n const registerStep = useCallback(\n (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number): void => {\n setSteps((prevSteps: StepTree) => {\n const stepNumber = step ?? (Array.isArray(prevSteps) ? prevSteps.length : 0)\n stepRef.current = stepNumber\n const newSteps = Array.isArray(prevSteps) ? [...prevSteps] : [prevSteps]\n newSteps.splice(stepNumber, 0, elements)\n\n if (currentStep === null) {\n const firstLeafParent = getFirstLeafParentPath(newSteps, [0])\n if (firstLeafParent) {\n setCurrentStep(firstLeafParent)\n }\n }\n\n return newSteps\n })\n },\n [currentStep],\n )\n\n const rebuildSteps = useCallback(() => {\n setSteps([])\n setRegistrationKey((prev) => prev + 1)\n }, [])\n\n const changeStepAtIndex = useCallback((steps: StepTree, index: number): void => {\n setSteps((prevSteps) => {\n const newSteps = Array.isArray(prevSteps) ? [...prevSteps] : [prevSteps]\n newSteps[index] = steps\n return newSteps\n })\n }, [])\n\n const publicContextValue = useMemo<FormContextValue>(\n () => ({\n currentStep: _currentStep,\n setCurrentStep: _setCurrentStep,\n currentStepNode,\n currentStepArr,\n validatedFields,\n isFirstStep,\n isLastStep,\n form: form as unknown as UseFormReturn<FieldValues>,\n next,\n prev,\n }),\n [\n form,\n _currentStep,\n _setCurrentStep,\n currentStepNode,\n currentStepArr,\n validatedFields,\n isFirstStep,\n isLastStep,\n next,\n prev,\n ],\n )\n\n const internalContextValue = useMemo<InternalFormContextValue>(\n () => ({\n // eslint-disable-next-line react-hooks/refs\n step: stepRef.current,\n registrationKey,\n rebuildSteps,\n registerStep,\n changeStepAtIndex,\n }),\n [registrationKey, rebuildSteps, registerStep, changeStepAtIndex],\n )\n\n const resolvedChildren =\n // eslint-disable-next-line react-hooks/refs\n typeof children === 'function' ? children(publicContextValue as unknown as FormContextValue<TFieldValues>) : children\n\n return (\n // eslint-disable-next-line react-hooks/refs\n <FormContext.Provider value={publicContextValue}>\n <InternalFormContext.Provider value={internalContextValue}>\n <form ref={ref} onSubmit={form.handleSubmit(onSubmit)} {...props}>\n <Step>{resolvedChildren}</Step>\n </form>\n </InternalFormContext.Provider>\n </FormContext.Provider>\n )\n}\n\nconst Form = forwardRef(FormInner) as <TFieldValues extends FieldValues = FieldValues>(\n props: FormProps<TFieldValues> & { ref?: React.Ref<HTMLFormElement> },\n) => React.JSX.Element\n\n;(Form as React.NamedExoticComponent & { displayName?: string }).displayName = 'Form'\n\nexport { Form, useFormContext, useInternalFormContext }\nexport type { FormContextValue, InternalFormContextValue, FormProps }\n","import { ComponentProps, useEffect } from 'react'\nimport {\n type Control,\n Controller as RHFController,\n type ControllerFieldState,\n type ControllerRenderProps,\n type FieldPath,\n type FieldValues,\n type UseFormStateReturn,\n} from 'react-hook-form'\n\nimport { useFormContext } from './form'\nimport { useStep } from './step'\n\ntype ControllerRenderArgs<\n TFieldValues extends FieldValues = FieldValues,\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,\n> = {\n field: ControllerRenderProps<TFieldValues, TName>\n fieldState: ControllerFieldState\n formState: UseFormStateReturn<TFieldValues>\n}\n\ntype ControllerProps<\n TFieldValues extends FieldValues = FieldValues,\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,\n> = ComponentProps<typeof RHFController<TFieldValues, TName>>\n\nfunction Controller<\n TFieldValues extends FieldValues = FieldValues,\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,\n>({ name, control, ...rest }: ControllerProps<TFieldValues, TName>) {\n const formContext = useFormContext<TFieldValues>()\n const stepContext = useStep()\n const resolvedControl = control ?? (formContext.form.control as unknown as Control<TFieldValues>)\n\n useEffect(() => {\n if (stepContext) {\n stepContext.registerField([name as string])\n }\n }, [stepContext?.registrationKey])\n\n useEffect(() => {\n if (stepContext?.step !== undefined) {\n stepContext?.rebuildSteps()\n }\n return () => {\n stepContext?.rebuildSteps()\n }\n }, [])\n\n return <RHFController name={name} control={resolvedControl} {...rest} />\n}\n\nController.displayName = 'Controller'\n\nexport { Controller }\nexport type { ControllerProps, ControllerRenderArgs }\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/step.tsx","../src/form.tsx","../src/controller.tsx"],"names":["createContext","useContext","useMemo","useRef","useState","useCallback","steps","useEffect","stepRef","prev","jsx","forwardRef","RHFController"],"mappings":";;;;;;;AAcA,IAAM,WAAA,GAAcA,oBAAuC,IAAI,CAAA;AAE/D,SAAS,OAAA,GAAmC;AAC1C,EAAA,OAAOC,iBAAW,WAAW,CAAA;AAC/B;AAEA,SAAS,IAAA,CAAK,EAAE,QAAA,EAAS,EAAkC;AACzD,EAAA,MAAM,cAAc,OAAA,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AS,WAGP,EAAE,IAAA,EAAM,OAAA,EAAS,GAAG,MAAK,EAAyC;AAClE,EAAA,MAAM,cAAc,cAAA,EAA6B;AACjD,EAAA,MAAM,cAAc,OAAA,EAAQ;AAC5B,EAAA,MAAM,eAAA,GAAkB,OAAA,IAAA,IAAA,GAAA,OAAA,GAAY,WAAA,CAAY,IAAA,CAAK,OAAA;AAErD,EAAAJ,gBAAU,MAAM;AACd,IAAA,IAAI,WAAA,EAAa;AACf,MAAA,WAAA,CAAY,aAAA,CAAc,CAAC,IAAc,CAAC,CAAA;AAAA,IAC5C;AAAA,EACF,CAAA,EAAG,CAAC,WAAA,IAAA,IAAA,GAAA,MAAA,GAAA,WAAA,CAAa,eAAe,CAAC,CAAA;AAEjC,EAAAA,gBAAU,MAAM;AACd,IAAA,IAAA,CAAI,WAAA,IAAA,IAAA,GAAA,MAAA,GAAA,WAAA,CAAa,UAAS,MAAA,EAAW;AACnC,MAAA,WAAA,IAAA,IAAA,GAAA,MAAA,GAAA,WAAA,CAAa,YAAA,EAAA;AAAA,IACf;AACA,IAAA,OAAO,MAAM;AACX,MAAA,WAAA,IAAA,IAAA,GAAA,MAAA,GAAA,WAAA,CAAa,YAAA,EAAA;AAAA,IACf,CAAA;AAAA,EACF,CAAA,EAAG,EAAE,CAAA;AAEL,EAAA,uBAAOG,cAAAA,CAACE,wBAAA,EAAA,EAAc,MAAY,OAAA,EAAS,eAAA,EAAkB,GAAG,IAAA,EAAM,CAAA;AACxE;AAEA,UAAA,CAAW,WAAA,GAAc,YAAA","file":"index.js","sourcesContent":["import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react'\n\nimport { useInternalFormContext } from './form'\nimport { type StepTree } from './types'\n\ntype StepContextValue = {\n step?: number\n registrationKey: number\n registerField: (elements: StepTree) => void\n registerStep: (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number) => void\n rebuildSteps: () => void\n changeStepAtIndex: (steps: StepTree, index: number) => void\n}\n\nconst StepContext = createContext<StepContextValue | null>(null)\n\nfunction useStep(): StepContextValue | null {\n return useContext(StepContext)\n}\n\nfunction Step({ children }: { children: React.ReactNode }) {\n const stepContext = useStep()\n const formContext = useInternalFormContext()\n\n const {\n registerStep: registerStepFromParent,\n step: stepFromParent,\n rebuildSteps: rebuildStepsFromParent,\n changeStepAtIndex: changeStepAtIndexFromParent,\n registrationKey: registrationKeyFromParent,\n } = stepContext ?? formContext\n\n const isRoot = useMemo(() => {\n if (!stepContext) return true\n return false\n }, [stepContext])\n const stepRef = useRef<number | undefined>(undefined)\n const [steps, setSteps] = useState<StepTree>([])\n const [registrationKey, setRegistrationKey] = useState(0)\n\n const changeStepAtIndex = useCallback((steps: StepTree, index: number): void => {\n setSteps((prevSteps) => {\n const newSteps = [...prevSteps]\n newSteps[index] = steps\n return newSteps\n })\n }, [])\n\n useEffect(() => {\n if (isRoot && steps.length === 0) return\n const stepList = steps.length ? steps : ['']\n if (stepRef.current !== undefined) {\n changeStepAtIndexFromParent(stepList, stepRef.current)\n } else {\n registerStepFromParent(stepList, stepRef)\n }\n }, [registrationKeyFromParent, steps, isRoot])\n\n useEffect(() => {\n if (stepFromParent !== undefined) {\n rebuildStepsFromParent()\n }\n return () => {\n rebuildStepsFromParent()\n }\n }, [])\n\n const registerField = useCallback(\n (elements: StepTree): void => {\n setSteps((prevSteps) => {\n return [...prevSteps, ...elements]\n })\n },\n [steps],\n )\n\n const registerStep = useCallback(\n (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number): void => {\n setSteps((prevSteps: StepTree) => {\n const stepNumber = step ?? prevSteps.length\n stepRef.current = stepNumber\n const newSteps = [...prevSteps]\n newSteps.splice(stepNumber, 0, elements)\n return newSteps\n })\n },\n [steps],\n )\n\n const rebuildSteps = useCallback(() => {\n setSteps([])\n setRegistrationKey((prev) => prev + 1)\n }, [])\n\n const contextValue = useMemo<StepContextValue>(\n () => ({\n // eslint-disable-next-line react-hooks/refs\n step: stepRef.current,\n registrationKey,\n changeStepAtIndex,\n registerField,\n registerStep,\n rebuildSteps,\n }),\n [registrationKey, changeStepAtIndex, registerField, registerStep, rebuildSteps],\n )\n\n // eslint-disable-next-line react-hooks/refs\n return <StepContext.Provider value={contextValue}>{children}</StepContext.Provider>\n}\n\nStep.displayName = 'Step'\n\nexport { Step, useStep }\nexport type { StepContextValue }\n","import { createContext, forwardRef, useCallback, useContext, useMemo, useRef, useState } from 'react'\nimport { type FieldValues, Path, type UseFormReturn } from 'react-hook-form'\n\nimport { Step } from './step'\nimport { type StepTree, type StepValidationMode } from './types'\n\nfunction comparePaths(a: number[], b: number[]): number {\n for (let i = 0; i < Math.max(a.length, b.length); i++) {\n const ai = a[i] ?? -1\n const bi = b[i] ?? -1\n if (ai !== bi) return ai - bi\n }\n return 0\n}\n\nfunction getNodeAtPath(tree: StepTree, path: number[]): StepTree | undefined {\n let current: StepTree = tree\n for (const index of path) {\n if (!Array.isArray(current) || index >= current.length) return undefined\n current = current[index]\n }\n return current\n}\n\nfunction isLeafParent(node: StepTree): node is string[] {\n return Array.isArray(node) && node.length > 0 && node.every((child) => typeof child === 'string')\n}\n\nfunction getFirstLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const node = getNodeAtPath(tree, path)\n if (node === undefined) return null\n if (isLeafParent(node)) return path\n if (Array.isArray(node) && node.length > 0) {\n for (let i = 0; i < node.length; i++) {\n const found = getFirstLeafParentPath(tree, [...path, i])\n if (found) return found\n }\n }\n return null\n}\n\nfunction getLastLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const node = getNodeAtPath(tree, path)\n if (node === undefined) return null\n if (isLeafParent(node)) return path\n if (Array.isArray(node) && node.length > 0) {\n for (let i = node.length - 1; i >= 0; i--) {\n const found = getLastLeafParentPath(tree, [...path, i])\n if (found) return found\n }\n }\n return null\n}\n\nfunction getPrevLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const current = [...path]\n while (current.length > 0) {\n const lastIndex = current[current.length - 1]\n const parentPath = current.slice(0, -1)\n const parent = getNodeAtPath(tree, parentPath)\n\n if (Array.isArray(parent)) {\n for (let i = lastIndex - 1; i >= 0; i--) {\n const found = getLastLeafParentPath(tree, [...parentPath, i])\n if (found) return found\n }\n }\n\n current.pop()\n }\n\n return null\n}\n\nfunction getNextLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const current = [...path]\n while (current.length > 0) {\n const lastIndex = current[current.length - 1]\n const parentPath = current.slice(0, -1)\n const parent = getNodeAtPath(tree, parentPath)\n\n if (Array.isArray(parent)) {\n for (let i = lastIndex + 1; i < parent.length; i++) {\n const found = getFirstLeafParentPath(tree, [...parentPath, i])\n if (found) return found\n }\n }\n\n current.pop()\n }\n\n return null\n}\n\ntype FormContextValue<TFieldValues extends FieldValues = FieldValues> = {\n form: UseFormReturn<TFieldValues>\n currentStep: number | number[] | null\n setCurrentStep: (step: number | number[]) => Promise<void>\n currentStepNode: StepTree | undefined\n currentStepArr: string[] | null\n validatedFields: string[]\n isFirstStep: boolean\n isLastStep: boolean\n next: () => void\n prev: () => void\n}\n\ntype InternalFormContextValue = {\n step?: number\n registrationKey: number\n rebuildSteps: () => void\n registerStep: (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number) => void\n changeStepAtIndex: (elements: StepTree, index: number) => void\n}\n\nconst FormContext = createContext<FormContextValue | null>(null)\nconst InternalFormContext = createContext<InternalFormContextValue | null>(null)\n\nfunction useFormContext<TFieldValues extends FieldValues = FieldValues>(): FormContextValue<TFieldValues> {\n const context = useContext(FormContext)\n if (!context) {\n throw new Error('useFormContext must be used within a <Form>')\n }\n return context as unknown as FormContextValue<TFieldValues>\n}\n\nfunction useInternalFormContext(): InternalFormContextValue {\n const context = useContext(InternalFormContext)\n if (!context) {\n throw new Error('useInternalFormContext must be used within a <Form>')\n }\n return context\n}\n\ninterface FormProps<TFieldValues extends FieldValues = FieldValues>\n extends Omit<React.ComponentProps<'form'>, 'onSubmit' | 'children'> {\n form: UseFormReturn<TFieldValues>\n onSubmit: (values: TFieldValues) => void\n stepValidationMode?: StepValidationMode\n children: React.ReactNode | ((context: FormContextValue<TFieldValues>) => React.ReactNode)\n}\n\nfunction FormInner<TFieldValues extends FieldValues = FieldValues>(\n { form, onSubmit, stepValidationMode = 'forward', children, ...props }: FormProps<TFieldValues>,\n ref: React.Ref<HTMLFormElement>,\n) {\n const [steps, setSteps] = useState<StepTree>([])\n const [currentStep, setCurrentStep] = useState<number[] | null>(null)\n const [validatedFields, setValidatedFields] = useState<string[]>([])\n const [registrationKey, setRegistrationKey] = useState(0)\n\n const stepRef = useRef<number | undefined>(undefined)\n\n const _currentStep = useMemo<number | number[] | null>(() => {\n if (!currentStep) return null\n const sliced = currentStep.slice(1)\n return sliced.length === 1 ? sliced[0] : sliced\n }, [currentStep])\n\n const currentStepNode = useMemo(\n () => (currentStep ? getNodeAtPath(steps, currentStep) : undefined),\n [steps, currentStep],\n )\n\n const currentStepArr = useMemo<string[] | null>(() => {\n if (!currentStepNode) return null\n return isLeafParent(currentStepNode) ? currentStepNode : null\n }, [currentStepNode])\n\n const _setCurrentStep = useCallback(\n async (step: number | number[]) => {\n const path = typeof step === 'number' ? [0, step] : [0, ...step]\n if (currentStep && currentStepArr && stepValidationMode !== 'none') {\n const isForward = comparePaths(path, currentStep) > 0\n const shouldValidate = stepValidationMode === 'all' || (stepValidationMode === 'forward' && isForward)\n if (shouldValidate) {\n const isValid = await form.trigger(currentStepArr as Path<TFieldValues>[])\n if (!isValid) {\n setValidatedFields((prev) => prev.filter((field) => !currentStepArr.includes(field)))\n return\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n }\n setCurrentStep(path)\n },\n [currentStep, currentStepArr, form, stepValidationMode],\n )\n\n const isFirstStep = useMemo(\n () => (currentStep ? getPrevLeafParentPath(steps, currentStep) === null : true),\n [steps, currentStep],\n )\n\n const isLastStep = useMemo(\n () => (currentStep ? getNextLeafParentPath(steps, currentStep) === null : true),\n [steps, currentStep],\n )\n\n const next = useCallback(async () => {\n if (!currentStep) return\n const nextPath = getNextLeafParentPath(steps, currentStep)\n if (currentStepArr && stepValidationMode !== 'none') {\n const isValid = await form.trigger(currentStepArr as Path<TFieldValues>[])\n if (!isValid) {\n setValidatedFields((prev) => prev.filter((field) => !currentStepArr.includes(field)))\n return\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n if (nextPath) {\n setCurrentStep(nextPath)\n }\n }, [steps, currentStep, currentStepArr, form, stepValidationMode])\n\n const prev = useCallback(() => {\n if (!currentStep) return\n const prevPath = getPrevLeafParentPath(steps, currentStep)\n if (prevPath) {\n setCurrentStep(prevPath)\n }\n }, [steps, currentStep])\n\n const registerStep = useCallback(\n (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number): void => {\n setSteps((prevSteps: StepTree) => {\n const stepNumber = step ?? (Array.isArray(prevSteps) ? prevSteps.length : 0)\n stepRef.current = stepNumber\n const newSteps = Array.isArray(prevSteps) ? [...prevSteps] : [prevSteps]\n newSteps.splice(stepNumber, 0, elements)\n\n if (currentStep === null) {\n const firstLeafParent = getFirstLeafParentPath(newSteps, [0])\n if (firstLeafParent) {\n setCurrentStep(firstLeafParent)\n }\n }\n\n return newSteps\n })\n },\n [currentStep],\n )\n\n const rebuildSteps = useCallback(() => {\n setSteps([])\n setRegistrationKey((prev) => prev + 1)\n }, [])\n\n const changeStepAtIndex = useCallback((steps: StepTree, index: number): void => {\n setSteps((prevSteps) => {\n const newSteps = Array.isArray(prevSteps) ? [...prevSteps] : [prevSteps]\n newSteps[index] = steps\n return newSteps\n })\n }, [])\n\n const publicContextValue = useMemo<FormContextValue>(\n () => ({\n currentStep: _currentStep,\n setCurrentStep: _setCurrentStep,\n currentStepNode,\n currentStepArr,\n validatedFields,\n isFirstStep,\n isLastStep,\n form: form as unknown as UseFormReturn<FieldValues>,\n next,\n prev,\n }),\n [\n form,\n _currentStep,\n _setCurrentStep,\n currentStepNode,\n currentStepArr,\n validatedFields,\n isFirstStep,\n isLastStep,\n next,\n prev,\n ],\n )\n\n const internalContextValue = useMemo<InternalFormContextValue>(\n () => ({\n // eslint-disable-next-line react-hooks/refs\n step: stepRef.current,\n registrationKey,\n rebuildSteps,\n registerStep,\n changeStepAtIndex,\n }),\n [registrationKey, rebuildSteps, registerStep, changeStepAtIndex],\n )\n\n const resolvedChildren =\n // eslint-disable-next-line react-hooks/refs\n typeof children === 'function' ? children(publicContextValue as unknown as FormContextValue<TFieldValues>) : children\n\n return (\n // eslint-disable-next-line react-hooks/refs\n <FormContext.Provider value={publicContextValue}>\n <InternalFormContext.Provider value={internalContextValue}>\n <form ref={ref} onSubmit={form.handleSubmit(onSubmit)} {...props}>\n <Step>{resolvedChildren}</Step>\n </form>\n </InternalFormContext.Provider>\n </FormContext.Provider>\n )\n}\n\nconst Form = forwardRef(FormInner) as <TFieldValues extends FieldValues = FieldValues>(\n props: FormProps<TFieldValues> & { ref?: React.Ref<HTMLFormElement> },\n) => React.JSX.Element\n\n;(Form as React.NamedExoticComponent & { displayName?: string }).displayName = 'Form'\n\nexport { Form, useFormContext, useInternalFormContext }\nexport type { FormContextValue, FormProps, InternalFormContextValue }\n","import { ComponentProps, useEffect } from 'react'\nimport {\n type Control,\n Controller as RHFController,\n type ControllerFieldState,\n type ControllerRenderProps,\n type FieldPath,\n type FieldValues,\n type UseFormStateReturn,\n} from 'react-hook-form'\n\nimport { useFormContext } from './form'\nimport { useStep } from './step'\n\ntype ControllerRenderArgs<\n TFieldValues extends FieldValues = FieldValues,\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,\n> = {\n field: ControllerRenderProps<TFieldValues, TName>\n fieldState: ControllerFieldState\n formState: UseFormStateReturn<TFieldValues>\n}\n\ntype ControllerProps<\n TFieldValues extends FieldValues = FieldValues,\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,\n> = ComponentProps<typeof RHFController<TFieldValues, TName>>\n\nfunction Controller<\n TFieldValues extends FieldValues = FieldValues,\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,\n>({ name, control, ...rest }: ControllerProps<TFieldValues, TName>) {\n const formContext = useFormContext<TFieldValues>()\n const stepContext = useStep()\n const resolvedControl = control ?? (formContext.form.control as unknown as Control<TFieldValues>)\n\n useEffect(() => {\n if (stepContext) {\n stepContext.registerField([name as string])\n }\n }, [stepContext?.registrationKey])\n\n useEffect(() => {\n if (stepContext?.step !== undefined) {\n stepContext?.rebuildSteps()\n }\n return () => {\n stepContext?.rebuildSteps()\n }\n }, [])\n\n return <RHFController name={name} control={resolvedControl} {...rest} />\n}\n\nController.displayName = 'Controller'\n\nexport { Controller }\nexport type { ControllerProps, ControllerRenderArgs }\n"]}
|
package/dist/index.mjs
CHANGED
|
@@ -17,6 +17,10 @@ function Step({ children }) {
|
|
|
17
17
|
changeStepAtIndex: changeStepAtIndexFromParent,
|
|
18
18
|
registrationKey: registrationKeyFromParent
|
|
19
19
|
} = stepContext != null ? stepContext : formContext;
|
|
20
|
+
const isRoot = useMemo(() => {
|
|
21
|
+
if (!stepContext) return true;
|
|
22
|
+
return false;
|
|
23
|
+
}, [stepContext]);
|
|
20
24
|
const stepRef = useRef(void 0);
|
|
21
25
|
const [steps, setSteps] = useState([]);
|
|
22
26
|
const [registrationKey, setRegistrationKey] = useState(0);
|
|
@@ -28,14 +32,14 @@ function Step({ children }) {
|
|
|
28
32
|
});
|
|
29
33
|
}, []);
|
|
30
34
|
useEffect(() => {
|
|
31
|
-
if (steps.length
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
if (isRoot && steps.length === 0) return;
|
|
36
|
+
const stepList = steps.length ? steps : [""];
|
|
37
|
+
if (stepRef.current !== void 0) {
|
|
38
|
+
changeStepAtIndexFromParent(stepList, stepRef.current);
|
|
39
|
+
} else {
|
|
40
|
+
registerStepFromParent(stepList, stepRef);
|
|
37
41
|
}
|
|
38
|
-
}, [registrationKeyFromParent, steps]);
|
|
42
|
+
}, [registrationKeyFromParent, steps, isRoot]);
|
|
39
43
|
useEffect(() => {
|
|
40
44
|
if (stepFromParent !== void 0) {
|
|
41
45
|
rebuildStepsFromParent();
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/step.tsx","../src/form.tsx","../src/controller.tsx"],"names":["steps","stepRef","createContext","useContext","useState","useRef","useMemo","useCallback","prev","jsx","useEffect","RHFController"],"mappings":";;;;;AAcA,IAAM,WAAA,GAAc,cAAuC,IAAI,CAAA;AAE/D,SAAS,OAAA,GAAmC;AAC1C,EAAA,OAAO,WAAW,WAAW,CAAA;AAC/B;AAEA,SAAS,IAAA,CAAK,EAAE,QAAA,EAAS,EAAkC;AACzD,EAAA,MAAM,cAAc,OAAA,EAAQ;AAC5B,EAAA,MAAM,cAAc,sBAAA,EAAuB;AAE3C,EAAA,MAAM;AAAA,IACJ,YAAA,EAAc,sBAAA;AAAA,IACd,IAAA,EAAM,cAAA;AAAA,IACN,YAAA,EAAc,sBAAA;AAAA,IACd,iBAAA,EAAmB,2BAAA;AAAA,IACnB,eAAA,EAAiB;AAAA,MACf,WAAA,IAAA,IAAA,GAAA,WAAA,GAAe,WAAA;AACnB,EAAA,MAAM,OAAA,GAAU,OAA2B,MAAS,CAAA;AACpD,EAAA,MAAM,CAAC,KAAA,EAAO,QAAQ,CAAA,GAAI,QAAA,CAAmB,EAAE,CAAA;AAC/C,EAAA,MAAM,CAAC,eAAA,EAAiB,kBAAkB,CAAA,GAAI,SAAS,CAAC,CAAA;AAExD,EAAA,MAAM,iBAAA,GAAoB,WAAA,CAAY,CAACA,MAAAA,EAAiB,KAAA,KAAwB;AAC9E,IAAA,QAAA,CAAS,CAAC,SAAA,KAAc;AACtB,MAAA,MAAM,QAAA,GAAW,CAAC,GAAG,SAAS,CAAA;AAC9B,MAAA,QAAA,CAAS,KAAK,CAAA,GAAIA,MAAAA;AAClB,MAAA,OAAO,QAAA;AAAA,IACT,CAAC,CAAA;AAAA,EACH,CAAA,EAAG,EAAE,CAAA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EAAA,gBAAA,EAAiB,CAAA,EAC1B,GACF,CAAA,EACF;AAAA;AAEJ;AAEA,IAAM,IAAA,GAAO,WAAW,SAAS;AAI/B,IAAA,CAA+D,WAAA,GAAc,MAAA;AChS/E,SAAS,WAGP,EAAE,IAAA,EAAM,OAAA,EAAS,GAAG,MAAK,EAAyC;AAClE,EAAA,MAAM,cAAc,cAAA,EAA6B;AACjD,EAAA,MAAM,cAAc,OAAA,EAAQ;AAC5B,EAAA,MAAM,eAAA,GAAkB,OAAA,IAAA,IAAA,GAAA,OAAA,GAAY,WAAA,CAAY,IAAA,CAAK,OAAA;AAErD,EAAAC,UAAU,MAAM;AACd,IAAA,IAAI,WAAA,EAAa;AACf,MAAA,WAAA,CAAY,aAAA,CAAc,CAAC,IAAc,CAAC,CAAA;AAAA,IAC5C;AAAA,EACF,CAAA,EAAG,CAAC,WAAA,IAAA,IAAA,GAAA,MAAA,GAAA,WAAA,CAAa,eAAe,CAAC,CAAA;AAEjC,EAAAA,UAAU,MAAM;AACd,IAAA,IAAA,CAAI,WAAA,IAAA,IAAA,GAAA,MAAA,GAAA,WAAA,CAAa,UAAS,MAAA,EAAW;AACnC,MAAA,WAAA,IAAA,IAAA,GAAA,MAAA,GAAA,WAAA,CAAa,YAAA,EAAA;AAAA,IACf;AACA,IAAA,OAAO,MAAM;AACX,MAAA,WAAA,IAAA,IAAA,GAAA,MAAA,GAAA,WAAA,CAAa,YAAA,EAAA;AAAA,IACf,CAAA;AAAA,EACF,CAAA,EAAG,EAAE,CAAA;AAEL,EAAA,uBAAOD,GAAAA,CAACE,YAAA,EAAA,EAAc,MAAY,OAAA,EAAS,eAAA,EAAkB,GAAG,IAAA,EAAM,CAAA;AACxE;AAEA,UAAA,CAAW,WAAA,GAAc,YAAA","file":"index.mjs","sourcesContent":["import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react'\n\nimport { useInternalFormContext } from './form'\nimport { type StepTree } from './types'\n\ntype StepContextValue = {\n step?: number\n registrationKey: number\n registerField: (elements: StepTree) => void\n registerStep: (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number) => void\n rebuildSteps: () => void\n changeStepAtIndex: (steps: StepTree, index: number) => void\n}\n\nconst StepContext = createContext<StepContextValue | null>(null)\n\nfunction useStep(): StepContextValue | null {\n return useContext(StepContext)\n}\n\nfunction Step({ children }: { children: React.ReactNode }) {\n const stepContext = useStep()\n const formContext = useInternalFormContext()\n\n const {\n registerStep: registerStepFromParent,\n step: stepFromParent,\n rebuildSteps: rebuildStepsFromParent,\n changeStepAtIndex: changeStepAtIndexFromParent,\n registrationKey: registrationKeyFromParent,\n } = stepContext ?? formContext\n const stepRef = useRef<number | undefined>(undefined)\n const [steps, setSteps] = useState<StepTree>([])\n const [registrationKey, setRegistrationKey] = useState(0)\n\n const changeStepAtIndex = useCallback((steps: StepTree, index: number): void => {\n setSteps((prevSteps) => {\n const newSteps = [...prevSteps]\n newSteps[index] = steps\n return newSteps\n })\n }, [])\n\n useEffect(() => {\n if (steps.length > 0) {\n if (stepRef.current !== undefined) {\n changeStepAtIndexFromParent(steps, stepRef.current)\n } else {\n registerStepFromParent(steps, stepRef)\n }\n }\n }, [registrationKeyFromParent, steps])\n\n useEffect(() => {\n if (stepFromParent !== undefined) {\n rebuildStepsFromParent()\n }\n return () => {\n rebuildStepsFromParent()\n }\n }, [])\n\n const registerField = useCallback(\n (elements: StepTree): void => {\n setSteps((prevSteps) => {\n return [...prevSteps, ...elements]\n })\n },\n [steps],\n )\n\n const registerStep = useCallback(\n (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number): void => {\n setSteps((prevSteps: StepTree) => {\n const stepNumber = step ?? prevSteps.length\n stepRef.current = stepNumber\n const newSteps = [...prevSteps]\n newSteps.splice(stepNumber, 0, elements)\n return newSteps\n })\n },\n [steps],\n )\n\n const rebuildSteps = useCallback(() => {\n setSteps([])\n setRegistrationKey((prev) => prev + 1)\n }, [])\n\n const contextValue = useMemo<StepContextValue>(\n () => ({\n // eslint-disable-next-line react-hooks/refs\n step: stepRef.current,\n registrationKey,\n changeStepAtIndex,\n registerField,\n registerStep,\n rebuildSteps,\n }),\n [registrationKey, changeStepAtIndex, registerField, registerStep, rebuildSteps],\n )\n\n // eslint-disable-next-line react-hooks/refs\n return <StepContext.Provider value={contextValue}>{children}</StepContext.Provider>\n}\n\nStep.displayName = 'Step'\n\nexport { Step, useStep }\nexport type { StepContextValue }\n","import { createContext, forwardRef, useCallback, useContext, useMemo, useRef, useState } from 'react'\nimport { type FieldValues, Path, type UseFormReturn } from 'react-hook-form'\n\nimport { Step } from './step'\nimport { type StepTree, type StepValidationMode } from './types'\n\nfunction comparePaths(a: number[], b: number[]): number {\n for (let i = 0; i < Math.max(a.length, b.length); i++) {\n const ai = a[i] ?? -1\n const bi = b[i] ?? -1\n if (ai !== bi) return ai - bi\n }\n return 0\n}\n\nfunction getNodeAtPath(tree: StepTree, path: number[]): StepTree | undefined {\n let current: StepTree = tree\n for (const index of path) {\n if (!Array.isArray(current) || index >= current.length) return undefined\n current = current[index]\n }\n return current\n}\n\nfunction isLeafParent(node: StepTree): node is string[] {\n return Array.isArray(node) && node.length > 0 && node.every((child) => typeof child === 'string')\n}\n\nfunction getFirstLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const node = getNodeAtPath(tree, path)\n if (node === undefined) return null\n if (isLeafParent(node)) return path\n if (Array.isArray(node) && node.length > 0) {\n for (let i = 0; i < node.length; i++) {\n const found = getFirstLeafParentPath(tree, [...path, i])\n if (found) return found\n }\n }\n return null\n}\n\nfunction getLastLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const node = getNodeAtPath(tree, path)\n if (node === undefined) return null\n if (isLeafParent(node)) return path\n if (Array.isArray(node) && node.length > 0) {\n for (let i = node.length - 1; i >= 0; i--) {\n const found = getLastLeafParentPath(tree, [...path, i])\n if (found) return found\n }\n }\n return null\n}\n\nfunction getPrevLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const current = [...path]\n while (current.length > 0) {\n const lastIndex = current[current.length - 1]\n const parentPath = current.slice(0, -1)\n const parent = getNodeAtPath(tree, parentPath)\n\n if (Array.isArray(parent)) {\n for (let i = lastIndex - 1; i >= 0; i--) {\n const found = getLastLeafParentPath(tree, [...parentPath, i])\n if (found) return found\n }\n }\n\n current.pop()\n }\n\n return null\n}\n\nfunction getNextLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const current = [...path]\n while (current.length > 0) {\n const lastIndex = current[current.length - 1]\n const parentPath = current.slice(0, -1)\n const parent = getNodeAtPath(tree, parentPath)\n\n if (Array.isArray(parent)) {\n for (let i = lastIndex + 1; i < parent.length; i++) {\n const found = getFirstLeafParentPath(tree, [...parentPath, i])\n if (found) return found\n }\n }\n\n current.pop()\n }\n\n return null\n}\n\ntype FormContextValue<TFieldValues extends FieldValues = FieldValues> = {\n form: UseFormReturn<TFieldValues>\n currentStep: number | number[] | null\n setCurrentStep: (step: number | number[]) => Promise<void>\n currentStepNode: StepTree | undefined\n currentStepArr: string[] | null\n validatedFields: string[]\n isFirstStep: boolean\n isLastStep: boolean\n next: () => void\n prev: () => void\n}\n\ntype InternalFormContextValue = {\n step?: number\n registrationKey: number\n rebuildSteps: () => void\n registerStep: (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number) => void\n changeStepAtIndex: (elements: StepTree, index: number) => void\n}\n\nconst FormContext = createContext<FormContextValue | null>(null)\nconst InternalFormContext = createContext<InternalFormContextValue | null>(null)\n\nfunction useFormContext<TFieldValues extends FieldValues = FieldValues>(): FormContextValue<TFieldValues> {\n const context = useContext(FormContext)\n if (!context) {\n throw new Error('useFormContext must be used within a <Form>')\n }\n return context as unknown as FormContextValue<TFieldValues>\n}\n\nfunction useInternalFormContext(): InternalFormContextValue {\n const context = useContext(InternalFormContext)\n if (!context) {\n throw new Error('useInternalFormContext must be used within a <Form>')\n }\n return context\n}\n\ninterface FormProps<TFieldValues extends FieldValues = FieldValues>\n extends Omit<React.ComponentProps<'form'>, 'onSubmit' | 'children'> {\n form: UseFormReturn<TFieldValues>\n onSubmit: (values: TFieldValues) => void\n stepValidationMode?: StepValidationMode\n children: React.ReactNode | ((context: FormContextValue<TFieldValues>) => React.ReactNode)\n}\n\nfunction FormInner<TFieldValues extends FieldValues = FieldValues>(\n { form, onSubmit, stepValidationMode = 'forward', children, ...props }: FormProps<TFieldValues>,\n ref: React.Ref<HTMLFormElement>,\n) {\n const [steps, setSteps] = useState<StepTree>([])\n const [currentStep, setCurrentStep] = useState<number[] | null>(null)\n const [validatedFields, setValidatedFields] = useState<string[]>([])\n const [registrationKey, setRegistrationKey] = useState(0)\n\n const stepRef = useRef<number | undefined>(undefined)\n\n const _currentStep = useMemo<number | number[] | null>(() => {\n if (!currentStep) return null\n const sliced = currentStep.slice(1)\n return sliced.length === 1 ? sliced[0] : sliced\n }, [currentStep])\n\n const currentStepNode = useMemo(\n () => (currentStep ? getNodeAtPath(steps, currentStep) : undefined),\n [steps, currentStep],\n )\n\n const currentStepArr = useMemo<string[] | null>(() => {\n if (!currentStepNode) return null\n return isLeafParent(currentStepNode) ? currentStepNode : null\n }, [currentStepNode])\n\n const _setCurrentStep = useCallback(\n async (step: number | number[]) => {\n const path = typeof step === 'number' ? [0, step] : [0, ...step]\n if (currentStep && currentStepArr && stepValidationMode !== 'none') {\n const isForward = comparePaths(path, currentStep) > 0\n const shouldValidate = stepValidationMode === 'all' || (stepValidationMode === 'forward' && isForward)\n if (shouldValidate) {\n const isValid = await form.trigger(currentStepArr as Path<TFieldValues>[])\n if (!isValid) {\n setValidatedFields((prev) => prev.filter((field) => !currentStepArr.includes(field)))\n return\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n }\n setCurrentStep(path)\n },\n [currentStep, currentStepArr, form, stepValidationMode],\n )\n\n const isFirstStep = useMemo(\n () => (currentStep ? getPrevLeafParentPath(steps, currentStep) === null : true),\n [steps, currentStep],\n )\n\n const isLastStep = useMemo(\n () => (currentStep ? getNextLeafParentPath(steps, currentStep) === null : true),\n [steps, currentStep],\n )\n\n const next = useCallback(async () => {\n if (!currentStep) return\n const nextPath = getNextLeafParentPath(steps, currentStep)\n if (currentStepArr && stepValidationMode !== 'none') {\n const isValid = await form.trigger(currentStepArr as Path<TFieldValues>[])\n if (!isValid) {\n setValidatedFields((prev) => prev.filter((field) => !currentStepArr.includes(field)))\n return\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n if (nextPath) {\n setCurrentStep(nextPath)\n }\n }, [steps, currentStep, currentStepArr, form, stepValidationMode])\n\n const prev = useCallback(() => {\n if (!currentStep) return\n const prevPath = getPrevLeafParentPath(steps, currentStep)\n if (prevPath) {\n setCurrentStep(prevPath)\n }\n }, [steps, currentStep])\n\n const registerStep = useCallback(\n (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number): void => {\n setSteps((prevSteps: StepTree) => {\n const stepNumber = step ?? (Array.isArray(prevSteps) ? prevSteps.length : 0)\n stepRef.current = stepNumber\n const newSteps = Array.isArray(prevSteps) ? [...prevSteps] : [prevSteps]\n newSteps.splice(stepNumber, 0, elements)\n\n if (currentStep === null) {\n const firstLeafParent = getFirstLeafParentPath(newSteps, [0])\n if (firstLeafParent) {\n setCurrentStep(firstLeafParent)\n }\n }\n\n return newSteps\n })\n },\n [currentStep],\n )\n\n const rebuildSteps = useCallback(() => {\n setSteps([])\n setRegistrationKey((prev) => prev + 1)\n }, [])\n\n const changeStepAtIndex = useCallback((steps: StepTree, index: number): void => {\n setSteps((prevSteps) => {\n const newSteps = Array.isArray(prevSteps) ? [...prevSteps] : [prevSteps]\n newSteps[index] = steps\n return newSteps\n })\n }, [])\n\n const publicContextValue = useMemo<FormContextValue>(\n () => ({\n currentStep: _currentStep,\n setCurrentStep: _setCurrentStep,\n currentStepNode,\n currentStepArr,\n validatedFields,\n isFirstStep,\n isLastStep,\n form: form as unknown as UseFormReturn<FieldValues>,\n next,\n prev,\n }),\n [\n form,\n _currentStep,\n _setCurrentStep,\n currentStepNode,\n currentStepArr,\n validatedFields,\n isFirstStep,\n isLastStep,\n next,\n prev,\n ],\n )\n\n const internalContextValue = useMemo<InternalFormContextValue>(\n () => ({\n // eslint-disable-next-line react-hooks/refs\n step: stepRef.current,\n registrationKey,\n rebuildSteps,\n registerStep,\n changeStepAtIndex,\n }),\n [registrationKey, rebuildSteps, registerStep, changeStepAtIndex],\n )\n\n const resolvedChildren =\n // eslint-disable-next-line react-hooks/refs\n typeof children === 'function' ? children(publicContextValue as unknown as FormContextValue<TFieldValues>) : children\n\n return (\n // eslint-disable-next-line react-hooks/refs\n <FormContext.Provider value={publicContextValue}>\n <InternalFormContext.Provider value={internalContextValue}>\n <form ref={ref} onSubmit={form.handleSubmit(onSubmit)} {...props}>\n <Step>{resolvedChildren}</Step>\n </form>\n </InternalFormContext.Provider>\n </FormContext.Provider>\n )\n}\n\nconst Form = forwardRef(FormInner) as <TFieldValues extends FieldValues = FieldValues>(\n props: FormProps<TFieldValues> & { ref?: React.Ref<HTMLFormElement> },\n) => React.JSX.Element\n\n;(Form as React.NamedExoticComponent & { displayName?: string }).displayName = 'Form'\n\nexport { Form, useFormContext, useInternalFormContext }\nexport type { FormContextValue, InternalFormContextValue, FormProps }\n","import { ComponentProps, useEffect } from 'react'\nimport {\n type Control,\n Controller as RHFController,\n type ControllerFieldState,\n type ControllerRenderProps,\n type FieldPath,\n type FieldValues,\n type UseFormStateReturn,\n} from 'react-hook-form'\n\nimport { useFormContext } from './form'\nimport { useStep } from './step'\n\ntype ControllerRenderArgs<\n TFieldValues extends FieldValues = FieldValues,\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,\n> = {\n field: ControllerRenderProps<TFieldValues, TName>\n fieldState: ControllerFieldState\n formState: UseFormStateReturn<TFieldValues>\n}\n\ntype ControllerProps<\n TFieldValues extends FieldValues = FieldValues,\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,\n> = ComponentProps<typeof RHFController<TFieldValues, TName>>\n\nfunction Controller<\n TFieldValues extends FieldValues = FieldValues,\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,\n>({ name, control, ...rest }: ControllerProps<TFieldValues, TName>) {\n const formContext = useFormContext<TFieldValues>()\n const stepContext = useStep()\n const resolvedControl = control ?? (formContext.form.control as unknown as Control<TFieldValues>)\n\n useEffect(() => {\n if (stepContext) {\n stepContext.registerField([name as string])\n }\n }, [stepContext?.registrationKey])\n\n useEffect(() => {\n if (stepContext?.step !== undefined) {\n stepContext?.rebuildSteps()\n }\n return () => {\n stepContext?.rebuildSteps()\n }\n }, [])\n\n return <RHFController name={name} control={resolvedControl} {...rest} />\n}\n\nController.displayName = 'Controller'\n\nexport { Controller }\nexport type { ControllerProps, ControllerRenderArgs }\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/step.tsx","../src/form.tsx","../src/controller.tsx"],"names":["steps","stepRef","createContext","useContext","useState","useRef","useMemo","useCallback","prev","jsx","useEffect","RHFController"],"mappings":";;;;;AAcA,IAAM,WAAA,GAAc,cAAuC,IAAI,CAAA;AAE/D,SAAS,OAAA,GAAmC;AAC1C,EAAA,OAAO,WAAW,WAAW,CAAA;AAC/B;AAEA,SAAS,IAAA,CAAK,EAAE,QAAA,EAAS,EAAkC;AACzD,EAAA,MAAM,cAAc,OAAA,EAAQ;AAC5B,EAAA,MAAM,cAAc,sBAAA,EAAuB;AAE3C,EAAA,MAAM;AAAA,IACJ,YAAA,EAAc,sBAAA;AAAA,IACd,IAAA,EAAM,cAAA;AAAA,IACN,YAAA,EAAc,sBAAA;AAAA,IACd,iBAAA,EAAmB,2BAAA;AAAA,IACnB,eAAA,EAAiB;AAAA,MACf,WAAA,IAAA,IAAA,GAAA,WAAA,GAAe,WAAA;AAEnB,EAAA,MAAM,MAAA,GAAS,QAAQ,MAAM;AAC3B,IAAA,IAAI,CAAC,aAAa,OAAO,IAAA;AACzB,IAAA,OAAO,KAAA;AAAA,EACT,CAAA,EAAG,CAAC,WAAW,CAAC,CAAA;AAChB,EAAA,MAAM,OAA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,IAAA,IAAA,CAAI,WAAA,IAAA,IAAA,GAAA,MAAA,GAAA,WAAA,CAAa,UAAS,MAAA,EAAW;AACnC,MAAA,WAAA,IAAA,IAAA,GAAA,MAAA,GAAA,WAAA,CAAa,YAAA,EAAA;AAAA,IACf;AACA,IAAA,OAAO,MAAM;AACX,MAAA,WAAA,IAAA,IAAA,GAAA,MAAA,GAAA,WAAA,CAAa,YAAA,EAAA;AAAA,IACf,CAAA;AAAA,EACF,CAAA,EAAG,EAAE,CAAA;AAEL,EAAA,uBAAOD,GAAAA,CAACE,YAAA,EAAA,EAAc,MAAY,OAAA,EAAS,eAAA,EAAkB,GAAG,IAAA,EAAM,CAAA;AACxE;AAEA,UAAA,CAAW,WAAA,GAAc,YAAA","file":"index.mjs","sourcesContent":["import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react'\n\nimport { useInternalFormContext } from './form'\nimport { type StepTree } from './types'\n\ntype StepContextValue = {\n step?: number\n registrationKey: number\n registerField: (elements: StepTree) => void\n registerStep: (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number) => void\n rebuildSteps: () => void\n changeStepAtIndex: (steps: StepTree, index: number) => void\n}\n\nconst StepContext = createContext<StepContextValue | null>(null)\n\nfunction useStep(): StepContextValue | null {\n return useContext(StepContext)\n}\n\nfunction Step({ children }: { children: React.ReactNode }) {\n const stepContext = useStep()\n const formContext = useInternalFormContext()\n\n const {\n registerStep: registerStepFromParent,\n step: stepFromParent,\n rebuildSteps: rebuildStepsFromParent,\n changeStepAtIndex: changeStepAtIndexFromParent,\n registrationKey: registrationKeyFromParent,\n } = stepContext ?? formContext\n\n const isRoot = useMemo(() => {\n if (!stepContext) return true\n return false\n }, [stepContext])\n const stepRef = useRef<number | undefined>(undefined)\n const [steps, setSteps] = useState<StepTree>([])\n const [registrationKey, setRegistrationKey] = useState(0)\n\n const changeStepAtIndex = useCallback((steps: StepTree, index: number): void => {\n setSteps((prevSteps) => {\n const newSteps = [...prevSteps]\n newSteps[index] = steps\n return newSteps\n })\n }, [])\n\n useEffect(() => {\n if (isRoot && steps.length === 0) return\n const stepList = steps.length ? steps : ['']\n if (stepRef.current !== undefined) {\n changeStepAtIndexFromParent(stepList, stepRef.current)\n } else {\n registerStepFromParent(stepList, stepRef)\n }\n }, [registrationKeyFromParent, steps, isRoot])\n\n useEffect(() => {\n if (stepFromParent !== undefined) {\n rebuildStepsFromParent()\n }\n return () => {\n rebuildStepsFromParent()\n }\n }, [])\n\n const registerField = useCallback(\n (elements: StepTree): void => {\n setSteps((prevSteps) => {\n return [...prevSteps, ...elements]\n })\n },\n [steps],\n )\n\n const registerStep = useCallback(\n (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number): void => {\n setSteps((prevSteps: StepTree) => {\n const stepNumber = step ?? prevSteps.length\n stepRef.current = stepNumber\n const newSteps = [...prevSteps]\n newSteps.splice(stepNumber, 0, elements)\n return newSteps\n })\n },\n [steps],\n )\n\n const rebuildSteps = useCallback(() => {\n setSteps([])\n setRegistrationKey((prev) => prev + 1)\n }, [])\n\n const contextValue = useMemo<StepContextValue>(\n () => ({\n // eslint-disable-next-line react-hooks/refs\n step: stepRef.current,\n registrationKey,\n changeStepAtIndex,\n registerField,\n registerStep,\n rebuildSteps,\n }),\n [registrationKey, changeStepAtIndex, registerField, registerStep, rebuildSteps],\n )\n\n // eslint-disable-next-line react-hooks/refs\n return <StepContext.Provider value={contextValue}>{children}</StepContext.Provider>\n}\n\nStep.displayName = 'Step'\n\nexport { Step, useStep }\nexport type { StepContextValue }\n","import { createContext, forwardRef, useCallback, useContext, useMemo, useRef, useState } from 'react'\nimport { type FieldValues, Path, type UseFormReturn } from 'react-hook-form'\n\nimport { Step } from './step'\nimport { type StepTree, type StepValidationMode } from './types'\n\nfunction comparePaths(a: number[], b: number[]): number {\n for (let i = 0; i < Math.max(a.length, b.length); i++) {\n const ai = a[i] ?? -1\n const bi = b[i] ?? -1\n if (ai !== bi) return ai - bi\n }\n return 0\n}\n\nfunction getNodeAtPath(tree: StepTree, path: number[]): StepTree | undefined {\n let current: StepTree = tree\n for (const index of path) {\n if (!Array.isArray(current) || index >= current.length) return undefined\n current = current[index]\n }\n return current\n}\n\nfunction isLeafParent(node: StepTree): node is string[] {\n return Array.isArray(node) && node.length > 0 && node.every((child) => typeof child === 'string')\n}\n\nfunction getFirstLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const node = getNodeAtPath(tree, path)\n if (node === undefined) return null\n if (isLeafParent(node)) return path\n if (Array.isArray(node) && node.length > 0) {\n for (let i = 0; i < node.length; i++) {\n const found = getFirstLeafParentPath(tree, [...path, i])\n if (found) return found\n }\n }\n return null\n}\n\nfunction getLastLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const node = getNodeAtPath(tree, path)\n if (node === undefined) return null\n if (isLeafParent(node)) return path\n if (Array.isArray(node) && node.length > 0) {\n for (let i = node.length - 1; i >= 0; i--) {\n const found = getLastLeafParentPath(tree, [...path, i])\n if (found) return found\n }\n }\n return null\n}\n\nfunction getPrevLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const current = [...path]\n while (current.length > 0) {\n const lastIndex = current[current.length - 1]\n const parentPath = current.slice(0, -1)\n const parent = getNodeAtPath(tree, parentPath)\n\n if (Array.isArray(parent)) {\n for (let i = lastIndex - 1; i >= 0; i--) {\n const found = getLastLeafParentPath(tree, [...parentPath, i])\n if (found) return found\n }\n }\n\n current.pop()\n }\n\n return null\n}\n\nfunction getNextLeafParentPath(tree: StepTree, path: number[]): number[] | null {\n const current = [...path]\n while (current.length > 0) {\n const lastIndex = current[current.length - 1]\n const parentPath = current.slice(0, -1)\n const parent = getNodeAtPath(tree, parentPath)\n\n if (Array.isArray(parent)) {\n for (let i = lastIndex + 1; i < parent.length; i++) {\n const found = getFirstLeafParentPath(tree, [...parentPath, i])\n if (found) return found\n }\n }\n\n current.pop()\n }\n\n return null\n}\n\ntype FormContextValue<TFieldValues extends FieldValues = FieldValues> = {\n form: UseFormReturn<TFieldValues>\n currentStep: number | number[] | null\n setCurrentStep: (step: number | number[]) => Promise<void>\n currentStepNode: StepTree | undefined\n currentStepArr: string[] | null\n validatedFields: string[]\n isFirstStep: boolean\n isLastStep: boolean\n next: () => void\n prev: () => void\n}\n\ntype InternalFormContextValue = {\n step?: number\n registrationKey: number\n rebuildSteps: () => void\n registerStep: (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number) => void\n changeStepAtIndex: (elements: StepTree, index: number) => void\n}\n\nconst FormContext = createContext<FormContextValue | null>(null)\nconst InternalFormContext = createContext<InternalFormContextValue | null>(null)\n\nfunction useFormContext<TFieldValues extends FieldValues = FieldValues>(): FormContextValue<TFieldValues> {\n const context = useContext(FormContext)\n if (!context) {\n throw new Error('useFormContext must be used within a <Form>')\n }\n return context as unknown as FormContextValue<TFieldValues>\n}\n\nfunction useInternalFormContext(): InternalFormContextValue {\n const context = useContext(InternalFormContext)\n if (!context) {\n throw new Error('useInternalFormContext must be used within a <Form>')\n }\n return context\n}\n\ninterface FormProps<TFieldValues extends FieldValues = FieldValues>\n extends Omit<React.ComponentProps<'form'>, 'onSubmit' | 'children'> {\n form: UseFormReturn<TFieldValues>\n onSubmit: (values: TFieldValues) => void\n stepValidationMode?: StepValidationMode\n children: React.ReactNode | ((context: FormContextValue<TFieldValues>) => React.ReactNode)\n}\n\nfunction FormInner<TFieldValues extends FieldValues = FieldValues>(\n { form, onSubmit, stepValidationMode = 'forward', children, ...props }: FormProps<TFieldValues>,\n ref: React.Ref<HTMLFormElement>,\n) {\n const [steps, setSteps] = useState<StepTree>([])\n const [currentStep, setCurrentStep] = useState<number[] | null>(null)\n const [validatedFields, setValidatedFields] = useState<string[]>([])\n const [registrationKey, setRegistrationKey] = useState(0)\n\n const stepRef = useRef<number | undefined>(undefined)\n\n const _currentStep = useMemo<number | number[] | null>(() => {\n if (!currentStep) return null\n const sliced = currentStep.slice(1)\n return sliced.length === 1 ? sliced[0] : sliced\n }, [currentStep])\n\n const currentStepNode = useMemo(\n () => (currentStep ? getNodeAtPath(steps, currentStep) : undefined),\n [steps, currentStep],\n )\n\n const currentStepArr = useMemo<string[] | null>(() => {\n if (!currentStepNode) return null\n return isLeafParent(currentStepNode) ? currentStepNode : null\n }, [currentStepNode])\n\n const _setCurrentStep = useCallback(\n async (step: number | number[]) => {\n const path = typeof step === 'number' ? [0, step] : [0, ...step]\n if (currentStep && currentStepArr && stepValidationMode !== 'none') {\n const isForward = comparePaths(path, currentStep) > 0\n const shouldValidate = stepValidationMode === 'all' || (stepValidationMode === 'forward' && isForward)\n if (shouldValidate) {\n const isValid = await form.trigger(currentStepArr as Path<TFieldValues>[])\n if (!isValid) {\n setValidatedFields((prev) => prev.filter((field) => !currentStepArr.includes(field)))\n return\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n }\n setCurrentStep(path)\n },\n [currentStep, currentStepArr, form, stepValidationMode],\n )\n\n const isFirstStep = useMemo(\n () => (currentStep ? getPrevLeafParentPath(steps, currentStep) === null : true),\n [steps, currentStep],\n )\n\n const isLastStep = useMemo(\n () => (currentStep ? getNextLeafParentPath(steps, currentStep) === null : true),\n [steps, currentStep],\n )\n\n const next = useCallback(async () => {\n if (!currentStep) return\n const nextPath = getNextLeafParentPath(steps, currentStep)\n if (currentStepArr && stepValidationMode !== 'none') {\n const isValid = await form.trigger(currentStepArr as Path<TFieldValues>[])\n if (!isValid) {\n setValidatedFields((prev) => prev.filter((field) => !currentStepArr.includes(field)))\n return\n }\n setValidatedFields((prev) => [...new Set([...prev, ...currentStepArr])])\n }\n if (nextPath) {\n setCurrentStep(nextPath)\n }\n }, [steps, currentStep, currentStepArr, form, stepValidationMode])\n\n const prev = useCallback(() => {\n if (!currentStep) return\n const prevPath = getPrevLeafParentPath(steps, currentStep)\n if (prevPath) {\n setCurrentStep(prevPath)\n }\n }, [steps, currentStep])\n\n const registerStep = useCallback(\n (elements: StepTree, stepRef: React.RefObject<number | undefined>, step?: number): void => {\n setSteps((prevSteps: StepTree) => {\n const stepNumber = step ?? (Array.isArray(prevSteps) ? prevSteps.length : 0)\n stepRef.current = stepNumber\n const newSteps = Array.isArray(prevSteps) ? [...prevSteps] : [prevSteps]\n newSteps.splice(stepNumber, 0, elements)\n\n if (currentStep === null) {\n const firstLeafParent = getFirstLeafParentPath(newSteps, [0])\n if (firstLeafParent) {\n setCurrentStep(firstLeafParent)\n }\n }\n\n return newSteps\n })\n },\n [currentStep],\n )\n\n const rebuildSteps = useCallback(() => {\n setSteps([])\n setRegistrationKey((prev) => prev + 1)\n }, [])\n\n const changeStepAtIndex = useCallback((steps: StepTree, index: number): void => {\n setSteps((prevSteps) => {\n const newSteps = Array.isArray(prevSteps) ? [...prevSteps] : [prevSteps]\n newSteps[index] = steps\n return newSteps\n })\n }, [])\n\n const publicContextValue = useMemo<FormContextValue>(\n () => ({\n currentStep: _currentStep,\n setCurrentStep: _setCurrentStep,\n currentStepNode,\n currentStepArr,\n validatedFields,\n isFirstStep,\n isLastStep,\n form: form as unknown as UseFormReturn<FieldValues>,\n next,\n prev,\n }),\n [\n form,\n _currentStep,\n _setCurrentStep,\n currentStepNode,\n currentStepArr,\n validatedFields,\n isFirstStep,\n isLastStep,\n next,\n prev,\n ],\n )\n\n const internalContextValue = useMemo<InternalFormContextValue>(\n () => ({\n // eslint-disable-next-line react-hooks/refs\n step: stepRef.current,\n registrationKey,\n rebuildSteps,\n registerStep,\n changeStepAtIndex,\n }),\n [registrationKey, rebuildSteps, registerStep, changeStepAtIndex],\n )\n\n const resolvedChildren =\n // eslint-disable-next-line react-hooks/refs\n typeof children === 'function' ? children(publicContextValue as unknown as FormContextValue<TFieldValues>) : children\n\n return (\n // eslint-disable-next-line react-hooks/refs\n <FormContext.Provider value={publicContextValue}>\n <InternalFormContext.Provider value={internalContextValue}>\n <form ref={ref} onSubmit={form.handleSubmit(onSubmit)} {...props}>\n <Step>{resolvedChildren}</Step>\n </form>\n </InternalFormContext.Provider>\n </FormContext.Provider>\n )\n}\n\nconst Form = forwardRef(FormInner) as <TFieldValues extends FieldValues = FieldValues>(\n props: FormProps<TFieldValues> & { ref?: React.Ref<HTMLFormElement> },\n) => React.JSX.Element\n\n;(Form as React.NamedExoticComponent & { displayName?: string }).displayName = 'Form'\n\nexport { Form, useFormContext, useInternalFormContext }\nexport type { FormContextValue, FormProps, InternalFormContextValue }\n","import { ComponentProps, useEffect } from 'react'\nimport {\n type Control,\n Controller as RHFController,\n type ControllerFieldState,\n type ControllerRenderProps,\n type FieldPath,\n type FieldValues,\n type UseFormStateReturn,\n} from 'react-hook-form'\n\nimport { useFormContext } from './form'\nimport { useStep } from './step'\n\ntype ControllerRenderArgs<\n TFieldValues extends FieldValues = FieldValues,\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,\n> = {\n field: ControllerRenderProps<TFieldValues, TName>\n fieldState: ControllerFieldState\n formState: UseFormStateReturn<TFieldValues>\n}\n\ntype ControllerProps<\n TFieldValues extends FieldValues = FieldValues,\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,\n> = ComponentProps<typeof RHFController<TFieldValues, TName>>\n\nfunction Controller<\n TFieldValues extends FieldValues = FieldValues,\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,\n>({ name, control, ...rest }: ControllerProps<TFieldValues, TName>) {\n const formContext = useFormContext<TFieldValues>()\n const stepContext = useStep()\n const resolvedControl = control ?? (formContext.form.control as unknown as Control<TFieldValues>)\n\n useEffect(() => {\n if (stepContext) {\n stepContext.registerField([name as string])\n }\n }, [stepContext?.registrationKey])\n\n useEffect(() => {\n if (stepContext?.step !== undefined) {\n stepContext?.rebuildSteps()\n }\n return () => {\n stepContext?.rebuildSteps()\n }\n }, [])\n\n return <RHFController name={name} control={resolvedControl} {...rest} />\n}\n\nController.displayName = 'Controller'\n\nexport { Controller }\nexport type { ControllerProps, ControllerRenderArgs }\n"]}
|