react-simple-formkit 2.3.0 → 2.4.1
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 +134 -68
- package/dist/react-simple-formkit.js +4 -4
- package/dist/react-simple-formkit.mjs +568 -511
- package/dist/react-simple-formkit.umd.js +4 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# React Simple FormKit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A lightweight library for managing forms as uncontrolled. State updates only when actively watched. Simple and quick to configure with outstanding efficiency.
|
|
4
4
|
|
|
5
5
|
# Table of Contents
|
|
6
6
|
|
|
@@ -8,22 +8,25 @@ Supports managing forms as uncontrolled. State updates only when watched. Simple
|
|
|
8
8
|
- [Features](#features)
|
|
9
9
|
- [Quick start](#quick-start)
|
|
10
10
|
- [Core concepts](#core-concepts)
|
|
11
|
-
- [
|
|
11
|
+
- [Input Field Modes](#input-field-modes)
|
|
12
12
|
- [Watching for updates](#watching-for-updates)
|
|
13
13
|
- [The Power of watch (Unified Observation)](#the-power-of-watch-unified-observation)
|
|
14
|
-
- [
|
|
14
|
+
- [Managing values](#managing-values)
|
|
15
15
|
- [Get value](#get-value)
|
|
16
16
|
- [Set value](#set-value)
|
|
17
17
|
- [Default values and reset](#default-values-and-reset)
|
|
18
|
-
- [
|
|
18
|
+
- [Managing form state](#managing-form-state)
|
|
19
19
|
- [Get form state](#get-form-state)
|
|
20
20
|
- [Set form state](#set-form-state)
|
|
21
|
-
- [
|
|
21
|
+
- [Managing field states](#managing-field-states)
|
|
22
22
|
- [Get field states](#get-field-states)
|
|
23
23
|
- [Set field states](#set-field-states)
|
|
24
|
-
- [
|
|
24
|
+
- [Managing errors](#managing-errors)
|
|
25
25
|
- [Get field error](#get-field-error)
|
|
26
26
|
- [Set field error](#set-field-error)
|
|
27
|
+
- [Grouping fields (nested object)](#grouping-fields-nested-object)
|
|
28
|
+
- [Utilities](#utilities)
|
|
29
|
+
- [restoreFromDotNotation](#restoreFromDotNotation)
|
|
27
30
|
- [APIs](#apis)
|
|
28
31
|
- [useForm](#useForm)
|
|
29
32
|
- [Form](#form)
|
|
@@ -47,14 +50,15 @@ Supports managing forms as uncontrolled. State updates only when watched. Simple
|
|
|
47
50
|
|
|
48
51
|
# Features
|
|
49
52
|
|
|
50
|
-
- **Lightweight**:
|
|
51
|
-
- **Easy setup**:
|
|
52
|
-
- **Optimized for uncontrolled forms**:
|
|
53
|
+
- **Lightweight**: Only ~**15KB**
|
|
54
|
+
- **Easy setup**: Simple and quick configuration
|
|
55
|
+
- **Optimized for uncontrolled forms**: No unnecessary re-renders
|
|
53
56
|
- Comprehensive form management:
|
|
54
|
-
- [
|
|
55
|
-
- [
|
|
56
|
-
- [
|
|
57
|
-
- [
|
|
57
|
+
- [Managing values](#managing-values): get/set values, defaults, reset, ...
|
|
58
|
+
- [Managing form state](#managing-form-state): isDirty, isError, dirtyFields, touchedFields, errorFields, ...
|
|
59
|
+
- [Managing field states](#managing-field-states): isDirty, isTouched, isError, custom states, ...
|
|
60
|
+
- [Managing errors](#managing-errors): form errors, field errors, ...
|
|
61
|
+
- [Grouping fields (nested object)](#grouping-fields-nested-object): Supports nested fields with dot notation.
|
|
58
62
|
|
|
59
63
|
# Quick start
|
|
60
64
|
|
|
@@ -79,9 +83,7 @@ return (
|
|
|
79
83
|
|
|
80
84
|
# Core concepts
|
|
81
85
|
|
|
82
|
-
##
|
|
83
|
-
|
|
84
|
-
React Simple FormKit provides three flexible modes to help you balance performance and feature requirements.
|
|
86
|
+
## Input Field Modes
|
|
85
87
|
|
|
86
88
|
### 1. `Uncontrolled`:
|
|
87
89
|
|
|
@@ -89,7 +91,7 @@ React Simple FormKit provides three flexible modes to help you balance performan
|
|
|
89
91
|
|
|
90
92
|
- **Use Cases**:
|
|
91
93
|
- Standard HTML inputs (e.g. `<input>`, `<textarea>`, `<select>`...)
|
|
92
|
-
- UI library components that
|
|
94
|
+
- UI library components that act as simple wrappers around native elements and follow standard browser behavior
|
|
93
95
|
|
|
94
96
|
- **How it works**: The browser manages form by default. However, you still maintain **full visibility**: you can [watch](#watching-for-updates) the entire form state, field states, and values.
|
|
95
97
|
|
|
@@ -99,7 +101,8 @@ React Simple FormKit provides three flexible modes to help you balance performan
|
|
|
99
101
|
|
|
100
102
|
**Best for**: Absolute control over input or complex UIs.
|
|
101
103
|
|
|
102
|
-
**Use Cases**:
|
|
104
|
+
- **Use Cases**:
|
|
105
|
+
- Advanced UI components with complex **INPUTS AND OUTPUTS**. For example, a multiple select component expects an array as its input/output, but your form state might need to store the value as a string.
|
|
103
106
|
|
|
104
107
|
By using `Controller`, you can transform the value in both directions:
|
|
105
108
|
|
|
@@ -116,11 +119,12 @@ Example:
|
|
|
116
119
|
```
|
|
117
120
|
<Controller
|
|
118
121
|
name="multipleSelect"
|
|
119
|
-
render={({ value = "", onChange, name }) => {
|
|
122
|
+
render={({ value = "", onChange, onBlur, name }) => {
|
|
120
123
|
return (
|
|
121
124
|
<Select
|
|
122
125
|
multiple
|
|
123
126
|
name={name}
|
|
127
|
+
onBlur={onBlur}
|
|
124
128
|
value={value.split(",")}
|
|
125
129
|
onChange={(e) => {
|
|
126
130
|
const value = e.target.value;
|
|
@@ -137,50 +141,44 @@ Example:
|
|
|
137
141
|
/>
|
|
138
142
|
```
|
|
139
143
|
|
|
140
|
-
|
|
144
|
+
Example `fieldState`:
|
|
141
145
|
|
|
142
|
-
|
|
143
|
-
|
|
146
|
+
- Only when you get `fieldState` from render props, `Controller` will auto subscribe to fieldState changes.
|
|
147
|
+
- If you need to set custom `fieldState`, you can use [`actions.setFieldState`](#useform).
|
|
144
148
|
|
|
145
|
-
return (
|
|
146
|
-
<Form control={control} onChange={console.log}>
|
|
147
|
-
{/* Uncontrolled */}
|
|
148
|
-
<input name="number1" placeholder="Enter a number" />
|
|
149
|
-
{/* Controlled by Controller. And the value at this field can be controlled by actions.setValue() */}
|
|
150
|
-
<Controller
|
|
151
|
-
name="multipleSelect"
|
|
152
|
-
render={({ value = "", onChange, name }) => {
|
|
153
|
-
return (
|
|
154
|
-
<Select
|
|
155
|
-
multiple
|
|
156
|
-
name={name}
|
|
157
|
-
value={value.split(",")}
|
|
158
|
-
onChange={(e) => {
|
|
159
|
-
const value = e.target.value;
|
|
160
|
-
// value as array by default but on autofill value as string
|
|
161
|
-
onChange(typeof value === "string" ? value : value.join(","));
|
|
162
|
-
}}
|
|
163
|
-
>
|
|
164
|
-
<MenuItem value="10">Ten</MenuItem>
|
|
165
|
-
<MenuItem value="20">Twenty</MenuItem>
|
|
166
|
-
<MenuItem value="30">Thirty</MenuItem>
|
|
167
|
-
</Select>
|
|
168
|
-
);
|
|
169
|
-
}}
|
|
170
|
-
/>
|
|
171
|
-
<button type="button" onClick={() => alert(JSON.stringify(actions.getValues()))}>
|
|
172
|
-
Get value
|
|
173
|
-
</button>
|
|
174
|
-
<button type="submit">Submit</button>
|
|
175
|
-
</Form>
|
|
176
|
-
)
|
|
177
149
|
```
|
|
178
|
-
|
|
179
|
-
|
|
150
|
+
<Controller
|
|
151
|
+
name="multipleSelect"
|
|
152
|
+
render={({ value = "", onChange, onBlur, name, fieldState }) => {
|
|
153
|
+
if(fieldState.hidden) return null
|
|
154
|
+
return (
|
|
155
|
+
<FormControl error={Boolean(fieldState.error)}>
|
|
156
|
+
<Select
|
|
157
|
+
multiple
|
|
158
|
+
name={name}
|
|
159
|
+
onBlur={onBlur}
|
|
160
|
+
value={value.split(",")}
|
|
161
|
+
error
|
|
162
|
+
onChange={(e) => {
|
|
163
|
+
const value = e.target.value;
|
|
164
|
+
// value as array by default but on autofill value as string
|
|
165
|
+
onChange(typeof value === "string" ? value : value.join(","));
|
|
166
|
+
}}
|
|
167
|
+
>
|
|
168
|
+
<MenuItem value="10">Ten</MenuItem>
|
|
169
|
+
<MenuItem value="20">Twenty</MenuItem>
|
|
170
|
+
<MenuItem value="30">Thirty</MenuItem>
|
|
171
|
+
</Select>
|
|
172
|
+
<FormHelperText>{fieldState.error}</FormHelperText>
|
|
173
|
+
</FormControl>
|
|
174
|
+
);
|
|
175
|
+
}}
|
|
176
|
+
/>
|
|
177
|
+
```
|
|
180
178
|
|
|
181
179
|
## Watching for updates
|
|
182
180
|
|
|
183
|
-
State updates only when observed via `watch()`, `useWatch()`, `subscribe()`, or by tracking changes through the Form component's `onChange`, `onBlur` callbacks. You can watch Values, Form States, Field States, and Errors through the same interface.
|
|
181
|
+
State updates only when observed via `watch()`, `useWatch()`, `subscribe()`, or by tracking changes through the Form component's `onChange`, `onBlur` callbacks. You can watch Values, Form States, Field States, and Errors through the same name interface.
|
|
184
182
|
|
|
185
183
|
- [watch(name)](#watch) will trigger a re-render at the form level.
|
|
186
184
|
- [useWatch({ name })](#usewatch) will trigger a re-render only in the component where it is called.
|
|
@@ -189,6 +187,7 @@ State updates only when observed via `watch()`, `useWatch()`, `subscribe()`, or
|
|
|
189
187
|
- [onBlur](#form) is just a handler callback that is called when field blurred.
|
|
190
188
|
- [actions.subscribe('onChange', callback)](#useform) subscribe onChange callback instead of passing `onChange` in [Form](#form) component.
|
|
191
189
|
- [actions.subscribe('onBlur', callback)](#useform) subscribe onBlur callback instead of passing `onBlur` in [Form](#form) component.
|
|
190
|
+
- [actions.trigger(name)](#actions.trigger) trigger watchers (e.g. `watch`, `useWatch`, `subscribe`) re-update values if needed.
|
|
192
191
|
|
|
193
192
|
> **Rule\*:** By default, onBlur works automatically for uncontrolled fields. However, for controlled fields, you must explicitly pass the onBlur prop when rendering the field.
|
|
194
193
|
|
|
@@ -207,7 +206,7 @@ The `watch` function (and `useWatch`, `subscribe`) is the "brain" of your form.
|
|
|
207
206
|
|
|
208
207
|
> **💡Why it matters\*:** This unified approach ensures you only need to learn one API to track the entire lifecycle of your form.
|
|
209
208
|
|
|
210
|
-
#
|
|
209
|
+
# Managing values
|
|
211
210
|
|
|
212
211
|
## Get value
|
|
213
212
|
|
|
@@ -289,7 +288,7 @@ return (
|
|
|
289
288
|
);
|
|
290
289
|
```
|
|
291
290
|
|
|
292
|
-
#
|
|
291
|
+
# Managing form state
|
|
293
292
|
|
|
294
293
|
## Set form state
|
|
295
294
|
|
|
@@ -317,7 +316,7 @@ console.log(actions.getFormState(["isDirty", "isError"]))
|
|
|
317
316
|
|
|
318
317
|
> **Note\*:** watch(), useWatch(), and subscribe() share the same name format.
|
|
319
318
|
|
|
320
|
-
#
|
|
319
|
+
# Managing field states
|
|
321
320
|
|
|
322
321
|
## Set field states
|
|
323
322
|
|
|
@@ -343,7 +342,7 @@ console.log(actions.getFieldStates(["fieldStates.fieldName.isDirty", "fieldState
|
|
|
343
342
|
|
|
344
343
|
> **Note\*:** watch(), useWatch(), and subscribe() share the same name format.
|
|
345
344
|
|
|
346
|
-
#
|
|
345
|
+
# Managing errors
|
|
347
346
|
|
|
348
347
|
## Get field error
|
|
349
348
|
|
|
@@ -379,6 +378,72 @@ return (
|
|
|
379
378
|
)
|
|
380
379
|
```
|
|
381
380
|
|
|
381
|
+
# Grouping fields (nested object)
|
|
382
|
+
|
|
383
|
+
- **Use cases**: When you need to group multiple fields into an object. By registering these fields with dot notation, you can manage these fieldStates, errors, values as a nested object.
|
|
384
|
+
|
|
385
|
+
- **Example**: Suppose an address has two fields, line1 and line2. You want to track their individual states (fieldStates, errors, values) separately, but you also need to group them under a single address object for easier management and form submission.
|
|
386
|
+
|
|
387
|
+
## Registering
|
|
388
|
+
|
|
389
|
+
```
|
|
390
|
+
// uncontrolled
|
|
391
|
+
<input name="address.line1" />
|
|
392
|
+
<input name="address.line2" />
|
|
393
|
+
<input name="address.line3" />
|
|
394
|
+
// controlled
|
|
395
|
+
<Controller
|
|
396
|
+
name="address.line4"
|
|
397
|
+
render={({ value = "", onChange }) => {
|
|
398
|
+
return <input value={value} onChange={onChange} />;
|
|
399
|
+
}}
|
|
400
|
+
/>
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
> **Rule\***: Nested fields must be registered using dot notation through inputs or Controllers `name` prop. If not registered, the field will be treated as a regular field with an object type value.
|
|
404
|
+
|
|
405
|
+
## Watching
|
|
406
|
+
|
|
407
|
+
```
|
|
408
|
+
const addressValue = watch("address")
|
|
409
|
+
const addressLine1 = watch("address.line1")
|
|
410
|
+
const addressLine1Error = watch("errors.address.line1")
|
|
411
|
+
const addressLine1FieldState = watch("fieldStates.address.line1")
|
|
412
|
+
```
|
|
413
|
+
|
|
414
|
+
## Updating
|
|
415
|
+
|
|
416
|
+
```
|
|
417
|
+
const { actions } = useForm()
|
|
418
|
+
actions.setValue("address.line1", "hello")
|
|
419
|
+
actions.setError("address.line1", "error message")
|
|
420
|
+
actions.setFieldState("address.line1", "hidden", true)
|
|
421
|
+
actions.setFieldState("address.line1", "disabled", true)
|
|
422
|
+
actions.setFieldState("address.line1", "custom", { hello: "world" })
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
> **Rule\***: If nested field is registered, all the fieldState, error, and value will be stored in the bottom level fields (leaf nodes). You cannot set a primitive value for a parent field.
|
|
426
|
+
|
|
427
|
+
# Utilities
|
|
428
|
+
|
|
429
|
+
## restoreFromDotNotation(object)
|
|
430
|
+
|
|
431
|
+
Convert dot notation objects to nested objects
|
|
432
|
+
|
|
433
|
+
```
|
|
434
|
+
import { restoreFromDotNotation } from "react-simple-formkit";
|
|
435
|
+
|
|
436
|
+
const dotNotationObject = watch(["errors.email", "errors.password"])
|
|
437
|
+
const nestedObject = restoreFromDotNotation(dotNotationObject)
|
|
438
|
+
// Output:
|
|
439
|
+
// {
|
|
440
|
+
// "errors": {
|
|
441
|
+
// "email": "Email is required",
|
|
442
|
+
// "password": "Password is required",
|
|
443
|
+
// }
|
|
444
|
+
// }
|
|
445
|
+
```
|
|
446
|
+
|
|
382
447
|
# APIs
|
|
383
448
|
|
|
384
449
|
## useForm
|
|
@@ -393,13 +458,13 @@ Generic props:
|
|
|
393
458
|
Return:
|
|
394
459
|
|
|
395
460
|
- `control`: contains methods and utilities to control the form.
|
|
396
|
-
- `watch(name)`: `Function` [Example](#
|
|
461
|
+
- `watch(name)`: `Function` [Example](#managing-values)
|
|
397
462
|
- `actions` is an object that contains utilities
|
|
398
463
|
- `actions.reset()`: `(newDefaultValues?: Object, options?: { clearCustomFormStates?: Boolean, clearCustomFieldStates?: Boolean }) => void` [Example](#default-values-and-reset)
|
|
399
|
-
- `actions.getValues()`: `(name?: String | Array) => Object` [Example](#
|
|
400
|
-
- `actions.getErrors()`: `(name?: String | Array) => Object` [Example](#
|
|
401
|
-
- `actions.getFormState()`: `(name?: String | Array) => Object` [Example](#
|
|
402
|
-
- `actions.getFieldStates()`: `(name?: String | Array) => Object` [Example](#
|
|
464
|
+
- `actions.getValues()`: `(name?: String | Array) => Object` [Example](#managing-values)
|
|
465
|
+
- `actions.getErrors()`: `(name?: String | Array) => Object` [Example](#managing-errors)
|
|
466
|
+
- `actions.getFormState()`: `(name?: String | Array) => Object` [Example](#managing-form-state)
|
|
467
|
+
- `actions.getFieldStates()`: `(name?: String | Array) => Object` [Example](#managing-field-states)
|
|
403
468
|
- `actions.setValue()`: `(name: String, value: Any, options?: { shouldDirty?: Boolean, shouldTouched?: Boolean }) => void` [Example](#set-value)
|
|
404
469
|
- `actions.setError()`: `(name: String, error: Any) => void` [Example](#set-field-error)
|
|
405
470
|
- `actions.setFieldState()`: `(name: String, property: String, value: Any) => void` [Example](#set-field-states)
|
|
@@ -413,6 +478,7 @@ Return:
|
|
|
413
478
|
- `actions.subscribeBlur()`: `(callback) => unsubscribe: Function()`
|
|
414
479
|
- `actions.getNumberFields()`: `() => Array`
|
|
415
480
|
- `actions.getDefaultValues()`: `() => Object`
|
|
481
|
+
- `actions.trigger(name)`: `(name: String | Array) => void` trigger watchers (e.g. `watch`, `useWatch`, `subscribe`) re-update values if needed.
|
|
416
482
|
|
|
417
483
|
## Form
|
|
418
484
|
|
|
@@ -442,7 +508,7 @@ Return: everything in render function above
|
|
|
442
508
|
|
|
443
509
|
## useWatch
|
|
444
510
|
|
|
445
|
-
[Example](#
|
|
511
|
+
[Example](#managing-values)
|
|
446
512
|
|
|
447
513
|
Generic props:
|
|
448
514
|
|
|
@@ -490,4 +556,4 @@ Return:
|
|
|
490
556
|
|
|
491
557
|
# Contact
|
|
492
558
|
|
|
493
|
-
For any ideas or issues, please
|
|
559
|
+
For any ideas or issues, please contact me at **anhhuy2000@gmail.com**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("react");var ne={exports:{}},H={};/**
|
|
2
2
|
* @license React
|
|
3
3
|
* react-jsx-runtime.production.js
|
|
4
4
|
*
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*
|
|
7
7
|
* This source code is licensed under the MIT license found in the
|
|
8
8
|
* LICENSE file in the root directory of this source tree.
|
|
9
|
-
*/var ie;function
|
|
9
|
+
*/var ie;function ke(){if(ie)return H;ie=1;var u=Symbol.for("react.transitional.element"),t=Symbol.for("react.fragment");function o(i,a,f){var c=null;if(f!==void 0&&(c=""+f),a.key!==void 0&&(c=""+a.key),"key"in a){f={};for(var b in a)b!=="key"&&(f[b]=a[b])}else f=a;return a=f.ref,{$$typeof:u,type:i,key:c,ref:a!==void 0?a:null,props:f}}return H.Fragment=t,H.jsx=o,H.jsxs=o,H}var X={};/**
|
|
10
10
|
* @license React
|
|
11
11
|
* react-jsx-runtime.development.js
|
|
12
12
|
*
|
|
@@ -14,9 +14,9 @@
|
|
|
14
14
|
*
|
|
15
15
|
* This source code is licensed under the MIT license found in the
|
|
16
16
|
* LICENSE file in the root directory of this source tree.
|
|
17
|
-
*/var fe;function ye(){return fe||(fe=1,process.env.NODE_ENV!=="production"&&function(){function
|
|
17
|
+
*/var fe;function ye(){return fe||(fe=1,process.env.NODE_ENV!=="production"&&function(){function u(r){if(r==null)return null;if(typeof r=="function")return r.$$typeof===ue?null:r.displayName||r.name||null;if(typeof r=="string")return r;switch(r){case v:return"Fragment";case j:return"Profiler";case F:return"StrictMode";case oe:return"Suspense";case Z:return"SuspenseList";case Q:return"Activity"}if(typeof r=="object")switch(typeof r.tag=="number"&&console.error("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."),r.$$typeof){case S:return"Portal";case M:return(r.displayName||"Context")+".Provider";case N:return(r._context.displayName||"Context")+".Consumer";case U:var g=r.render;return r=r.displayName,r||(r=g.displayName||g.name||"",r=r!==""?"ForwardRef("+r+")":"ForwardRef"),r;case B:return g=r.displayName||null,g!==null?g:u(r.type)||"Memo";case y:g=r._payload,r=r._init;try{return u(r(g))}catch{}}return null}function t(r){return""+r}function o(r){try{t(r);var g=!1}catch{g=!0}if(g){g=console;var p=g.error,_=typeof Symbol=="function"&&Symbol.toStringTag&&r[Symbol.toStringTag]||r.constructor.name||"Object";return p.call(g,"The provided key is an unsupported type %s. This value must be coerced to a string before using it here.",_),t(r)}}function i(r){if(r===v)return"<>";if(typeof r=="object"&&r!==null&&r.$$typeof===y)return"<...>";try{var g=u(r);return g?"<"+g+">":"<...>"}catch{return"<...>"}}function a(){var r=J.A;return r===null?null:r.getOwner()}function f(){return Error("react-stack-top-frame")}function c(r){if(K.call(r,"key")){var g=Object.getOwnPropertyDescriptor(r,"key").get;if(g&&g.isReactWarning)return!1}return r.key!==void 0}function b(r,g){function p(){re||(re=!0,console.error("%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://react.dev/link/special-props)",g))}p.isReactWarning=!0,Object.defineProperty(r,"key",{get:p,configurable:!0})}function d(){var r=u(this.type);return x[r]||(x[r]=!0,console.error("Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release.")),r=this.props.ref,r!==void 0?r:null}function m(r,g,p,_,W,D,q,G){return p=D.ref,r={$$typeof:O,type:r,key:g,props:D,_owner:W},(p!==void 0?p:null)!==null?Object.defineProperty(r,"ref",{enumerable:!1,get:d}):Object.defineProperty(r,"ref",{enumerable:!1,value:null}),r._store={},Object.defineProperty(r._store,"validated",{configurable:!1,enumerable:!1,writable:!0,value:0}),Object.defineProperty(r,"_debugInfo",{configurable:!1,enumerable:!1,writable:!0,value:null}),Object.defineProperty(r,"_debugStack",{configurable:!1,enumerable:!1,writable:!0,value:q}),Object.defineProperty(r,"_debugTask",{configurable:!1,enumerable:!1,writable:!0,value:G}),Object.freeze&&(Object.freeze(r.props),Object.freeze(r)),r}function l(r,g,p,_,W,D,q,G){var A=g.children;if(A!==void 0)if(_)if(ee(A)){for(_=0;_<A.length;_++)R(A[_]);Object.freeze&&Object.freeze(A)}else console.error("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead.");else R(A);if(K.call(g,"key")){A=u(r);var e=Object.keys(g).filter(function(E){return E!=="key"});_=0<e.length?"{key: someKey, "+e.join(": ..., ")+": ...}":"{key: someKey}",z[A+_]||(e=0<e.length?"{"+e.join(": ..., ")+": ...}":"{}",console.error(`A props object containing a "key" prop is being spread into JSX:
|
|
18
18
|
let props = %s;
|
|
19
19
|
<%s {...props} />
|
|
20
20
|
React keys must be passed directly to JSX without using spread:
|
|
21
21
|
let props = %s;
|
|
22
|
-
<%s key={someKey} {...props} />`,F,T,P,T),re[T+F]=!0)}if(T=null,S!==void 0&&(h(S),T=""+S),d(f)&&(h(f.key),T=""+f.key),"key"in f){S={};for(var D in f)D!=="key"&&(S[D]=f[D])}else S=f;return T&&C(S,typeof e=="function"?e.displayName||e.name||"Unknown":e),b(e,T,O,x,g(),S,q,J)}function c(e){typeof e=="object"&&e!==null&&e.$$typeof===i&&e._store&&(e._store.validated=1)}var l=t,i=Symbol.for("react.transitional.element"),n=Symbol.for("react.portal"),E=Symbol.for("react.fragment"),A=Symbol.for("react.strict_mode"),N=Symbol.for("react.profiler"),Y=Symbol.for("react.consumer"),I=Symbol.for("react.context"),se=Symbol.for("react.forward_ref"),ne=Symbol.for("react.suspense"),ue=Symbol.for("react.suspense_list"),oe=Symbol.for("react.memo"),j=Symbol.for("react.lazy"),X=Symbol.for("react.activity"),B=Symbol.for("react.client.reference"),W=l.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,L=Object.prototype.hasOwnProperty,Z=Array.isArray,U=console.createTask?console.createTask:function(){return null};l={"react-stack-bottom-frame":function(e){return e()}};var Q,K={},ee=l["react-stack-bottom-frame"].bind(l,k)(),_=U(R(k)),re={};G.Fragment=E,G.jsx=function(e,f,S,F,x){var O=1e4>W.recentlyCreatedOwnerStacks++;return u(e,f,S,!1,F,x,O?Error("react-stack-top-frame"):ee,O?U(R(e)):_)},G.jsxs=function(e,f,S,F,x){var O=1e4>W.recentlyCreatedOwnerStacks++;return u(e,f,S,!0,F,x,O?Error("react-stack-top-frame"):ee,O?U(R(e)):_)}}()),G}var de;function ve(){return de||(de=1,process.env.NODE_ENV==="production"?te.exports=Se():te.exports=ye()),te.exports}var be=ve();const y=()=>{},me=t.createContext({ref:null,watch:y,actions:{reset:y,resetField:y,setValue:y,getValues:y,getErrors:y,getFieldStates:y,getFormStates:y,setError:y,clearError:y,clearErrors:y,checkValidity:y,getNumberFields:y,getFieldValidity:y,getDefaultValues:y,setFieldState:y,resetFieldState:y,getControlledFields:y},registerController:y,registerHookWatcher:y,lastReloadedAt:y,loadFormValues:y,getWatchValue:y,channels:{}}),le=()=>t.useContext(me),pe=({id:s,control:o,method:h,action:R,children:g,onChange:k,onBlur:d,onSubmit:C=()=>{},onInput:v=()=>{},onReset:b=()=>{},numberFields:u=[],className:c,...l})=>{const i=t.useCallback(n=>{o.ref&&(o.ref.current=n,o.ref.current&&o.initForm())},[o]);return t.useEffect(()=>{let n=()=>{},E=()=>{};return k&&(n=o.channels.subscribe("onChange",k)),d&&(E=o.channels.subscribe("onBlur",d)),()=>{n(),E()}},[o.lastReloadedAt]),be.jsx(me.Provider,{value:o,children:be.jsx("form",{id:s,ref:i,action:R,method:h,className:c,onInput:v,onSubmit:n=>{h||n.preventDefault();const E=o.loadFormValues();C(E)},onChange:n=>{const E=n.target.name;!E||o.actions.getControlledFields().has(E)||o.actions.setValue(E,n.target.value)},onBlur:n=>{const E=n.target.name;if(!E||o.actions.getControlledFields().has(E))return;const A=n.target.value;o.channels.publish("onBlur",E,A,o.actions.getValues())},onReset:n=>{o.actions.reset(),b(n)},...l,children:g},o.lastReloadedAt)})},Re=({control:s,name:o,compute:h})=>{const{getWatchValue:R,registerHookWatcher:g}=s||le(),k=R({name:o,compute:h}),[d,C]=t.useState(k);return t.useEffect(()=>g({name:o,compute:h,value:d,setValue:C}),[]),d},ke=({name:s,defaultValue:o})=>{const{actions:h,registerController:R,channels:g}=le(),k=t.useRef(),d=h.getDefaultValues()[s]||o||"",[C,v]=t.useState(d),b=Re({name:`fieldStates.${s}`}),u=t.useCallback((l,{shouldDirty:i=!0,shouldOnChange:n=!0}={})=>{var A,N;let E=l;(A=l==null?void 0:l.target)!=null&&A.value&&(E=l.target.value),(N=l==null?void 0:l.target)!=null&&N.checked&&(E=l.target.checked+""),v(E),h.setValue(s,E,{shouldDirty:i,shouldOnChange:n})},[h.setValue]),c=t.useCallback(l=>{const i=h.getValues(),n=l??i[s];g.publish("onBlur",s,n,i)},[]);return t.useEffect(()=>R(s,v),[]),{ref:k,name:s,defaultValue:d,value:C,onChange:u,onBlur:c,fieldState:b}},Fe=({name:s,defaultValue:o,render:h=({ref:R,name:g,defaultValue:k,value:d,onChange:C,onBlur:v,fieldState:b})=>null})=>{const R=ke({name:s,defaultValue:o});return h(R)},Te=["errors","fieldStates","formState"],H=(s={},o="")=>o.split(".").reduce((h,R)=>h&&h[R]!==void 0?h[R]:void 0,s),Ee=(s={},o="",h)=>{const R={...s};let g=s;const k=o.split(".");return k.forEach((d,C)=>{C<k.length-1?(g[d]||(g[d]={}),R[d]={...g[d]},g=R[d]):g[d]=h}),R},V=s=>Te.some(o=>s.startsWith(o))?s:"values."+s,_e=["badInput","customError","patternMismatch","rangeOverflow","rangeUnderflow","stepMismatch","tooLong","tooShort","typeMismatch","valueMissing"],ge=s=>{typeof s.setCustomValidity=="function"&&s.setCustomValidity("");let o=s.validity,h=_e.find(R=>o[R]);if(h)return{type:h,message:s.validationMessage}},we=()=>{const s=t.useRef(new Map),o=t.useRef(new Map),h=t.useCallback(()=>o.current,[]),R=t.useCallback(()=>s.current,[]),g=t.useCallback(()=>{s.current.clear(),o.current.clear()},[]),k=t.useCallback((b,...u)=>{const c=s.current.get(b),l=o.current.get(b);c&&c.forEach(i=>i(...u)),l&&l.forEach(i=>i(...u)),s.current.forEach((i,n)=>{n!==b&&b.startsWith(n)&&i.forEach(E=>E(...u))}),o.current.forEach((i,n)=>{n!==b&&b.startsWith(n)&&i.forEach(E=>E(...u))})},[]),d=t.useCallback(b=>{s.current.forEach((u,c)=>{c.startsWith(b)&&u.forEach(l=>l())}),o.current.forEach((u,c)=>{c.startsWith(b)&&u.forEach(l=>l())})},[]),C=t.useCallback((b,u)=>(s.current.has(b)||s.current.set(b,new Set),s.current.get(b).add(u),()=>{var c;return(c=s.current.get(b))==null?void 0:c.delete(u)}),[]),v=t.useCallback((b,u)=>{o.current.has(b)||(o.current.set(b,new Set),o.current.get(b).add(u))},[]);return{reset:g,publish:k,subscribe:C,subscribeWatch:v,getEvents:R,getWatchEvents:h,trigger:d}},Ae=()=>{const[s,o]=t.useState(),h=t.useCallback(()=>o(new Date().toString()),[]);return[s,h]},Oe=()=>{const[,s]=t.useState({});return t.useCallback(()=>s({}),[])},Pe=({channels:s,getValues:o,getErrors:h,getFieldStates:R,getFormState:g})=>{const k=Oe(),d=t.useCallback(({name:u,compute:c})=>{if(typeof c=="function")return c(o());if(Array.isArray(u)){const l={};return u.forEach(i=>{l[i]=d({name:i})}),l}return H({...o(),errors:{...h()},formState:{...g()},fieldStates:{...R()}},u)},[]),C=t.useCallback(u=>{if(!u)return s.subscribeWatch("values",()=>k()),o();if(typeof u=="string"){const c=V(u);return s.subscribeWatch(c,()=>k()),d({name:u})}if(Array.isArray(u)){const c={};return u.forEach(l=>{const i=V(l);s.subscribeWatch(i,()=>k()),c[l]=d({name:l})}),{...c}}throw new Error("Parameters of watch must be string or array of string")},[]),v=t.useCallback(({name:u,compute:c,setValue:l})=>{if(typeof c=="function")return s.subscribe("values",()=>{const n=d({compute:c});l(n)});if(!u)return s.subscribe("values",()=>{l(o())});if(typeof u=="string"){const i=V(u);return s.subscribe(i,()=>{const E=d({name:u});l(E)})}if(Array.isArray(u)){const i=d({name:u}),n=[];return u.forEach(E=>{const A=V(E),N=s.subscribe(A,()=>{const Y=d({name:E});i[E]=Y,l({...i})});n.push(N)}),()=>n.forEach(E=>E())}throw new Error("Parameters of name must be string or array of string or compute must be a function")},[]),b=t.useCallback((u,c)=>{if(!u)return s.subscribe("values",()=>c(o()));if(["onChange","onBlur"].includes(u))return s.subscribe(u,c);if(typeof u=="string"){const l=V(u);return s.subscribe(l,()=>c(d({name:u})))}if(Array.isArray(u)){const l={},i=[];return u.forEach(n=>{const E=V(n),A=s.subscribe(E,()=>{l[n]=d({name:n}),c({...l})});i.push(A)}),()=>i.forEach(n=>n())}throw new Error("Parameters of name must be string or array of string")},[]);return{watch:C,registerHookWatcher:v,getWatchValue:d,subscribe:b}},he={lastReset:null,isDirty:!1,isError:!1,errorFields:[],dirtyFields:[],touchedFields:[]},ce={isDirty:!1,isTouched:!1,error:null},je=[],xe={},Ne=({numberFields:s=je,defaultValues:o=xe,shouldUnRegister:h=!1,shouldConvertNumber:R=!1}={})=>{const[g,k]=Ae(),d=t.useRef(),C=t.useRef(s),v=t.useRef(new Map),b=t.useRef({}),u=t.useRef({}),c=t.useRef({...he}),l=t.useRef({...o}),i=t.useRef({...o}),n=we(),E=t.useCallback(r=>{u.current=Object.keys(i.current).reduce((a,m)=>({...a,[m]:{...r?{}:u.current[m]||{},...ce}}),{})},[]),A=t.useCallback((r={},{clearCustomFormStates:a=!1,clearCustomFieldStates:m=!1}={})=>{i.current={...i.current,...r},l.current={...i.current},v.current.forEach((p,$)=>{p(i.current[$]??"")}),b.current={},c.current={...a?{}:c.current,...he},C.current=s,v.current=new Map,n.reset(),E(m),s.length>0&&(R=!0),k()},[]),N=t.useCallback(()=>{const r=Object.fromEntries(new FormData(d.current));return v.current.forEach((m,p)=>{r[p]=l.current[p]}),R&&C.current.forEach(m=>r.hasOwnProperty(m)&&(r[m]=Number(r[m])||0)),{...l.current,...r}},[]),Y=t.useCallback(()=>!!Object.values(W()).find(r=>r.isDirty),[]),I=t.useCallback(()=>!!Object.values(B()).find(r=>!!r),[]),se=t.useCallback(r=>ge(r.target),[]),ne=t.useCallback(()=>i.current,[]),ue=t.useCallback(()=>C.current,[]),oe=t.useCallback(()=>v.current,[]),j=t.useCallback((r,a)=>a?Array.isArray(a)?a.reduce((m,p)=>({...m,[p]:H(r,p)}),{}):H(r,a):{...r},[]),X=t.useCallback(r=>j(l.current,r),[j]),B=t.useCallback(r=>j(b.current,r),[j]),W=t.useCallback(r=>j(u.current,r),[j]),L=t.useCallback(r=>j({...c.current,lastReset:g},r),[g]),{watch:Z,registerHookWatcher:U,getWatchValue:Q,subscribe:K}=Pe({channels:n,getValues:X,getErrors:B,getFieldStates:W,getFormState:L}),ee=t.useCallback((r,a)=>{const m=c.current;H(m,r)!==a&&(c.current=Ee(c.current,r,a),n.publish(`formState.${r}`,a))},[]),_=t.useCallback((r,a,m)=>{const p=u.current[r]||{...ce};H(p,a)!==m&&(u.current=Ee(u.current,`${r}.${a}`,m),n.publish(`fieldStates.${r}.${a}`,m),a==="error"&&(c.current.errorFields=Object.keys(b.current).filter(w=>!!b.current[w]),n.publish("formState.errorFields",c.current.errorFields)),a==="isDirty"&&(c.current.dirtyFields=Object.keys(u.current).filter(w=>u.current[w].isDirty),n.publish("formState.dirtyFields",c.current.dirtyFields)),a==="isTouched"&&(c.current.touchedFields=Object.keys(u.current).filter(w=>u.current[w].isTouched),n.publish("formState.touchedFields",c.current.touchedFields)))},[]),re=t.useCallback(r=>{_(r,"isDirty",!1),_(r,"isTouched",!1),_(r,"error",null)},[]),e=t.useCallback((r,a)=>{if(b.current[r]===a)return;b.current[r]=a,_(r,"error",a),n.publish(`errors.${r}`,a);const p=I();c.current.isError!==p&&(c.current.isError=p,n.publish("formState.isError",p))},[]),f=t.useCallback(r=>{if(!b.current[r])return;b.current[r]=null,_(r,"error",null),n.publish(`errors.${r}`,null);const a=I();c.current.isError!==a&&(c.current.isError=a,n.publish("formState.isError",a))},[]),S=t.useCallback(()=>{if(Object.keys(b.current)===0)return;Object.keys(b.current).forEach(a=>f(a)),b.current={};const r=I();c.current.isError!==r&&(c.current.isError=r,n.publish("formState.isError",r))},[]),F=t.useCallback(r=>{const a=ge(r.target);a?e(r.target.name,a):f(r.target.name)},[]),x=t.useCallback((r,a,{shouldDirty:m=!0,shouldOnChange:p=!0}={})=>{if(a===l.current[r])return;R&&C.current.includes(r)&&(a=Number(a)||void 0),_(r,"isTouched",!0);const $=a!==i.current[r];m&&u.current[r].isDirty!==$&&_(r,"isDirty",$);const w=Y();c.current.isDirty!==w&&(c.current.isDirty=w,n.publish("formState.isDirty",w)),l.current[r]=a;const M=v.current.get(r);M&&M(a),n.publish(`values.${r}`,a,{shouldDirty:m,shouldOnChange:p}),p&&n.publish("onChange",r,a,l.current)},[]),O=t.useCallback((r,a)=>(v.current.set(r,a),u.current[r]={...ce},()=>{h&&v.current.delete(r)}),[]),q=t.useCallback((r,{keepError:a,keepDirty:m,keepTouched:p,keepCustomState:$,defaultValue:w})=>{const M=v.current.get(r);if(!M)throw new Error(`Cannot reset "${r}" because it's uncontrolled field`);i.current[r]=w,l.current[r]=w,M&&M(w),a||f(r),m||_(r,"isDirty",!1),p||_(r,"isTouched",!1)},[]),J=t.useCallback(r=>{if(!r)return n.trigger("values");if(Array.isArray(r))return r.forEach(m=>{const p=V(m);n.trigger(p)});const a=V(r);n.trigger(a)},[]),T=t.useCallback((r,a=P.getValues(r))=>{n.publish("onBlur",r,a,P.getValues())},[]),P=t.useMemo(()=>({subscribe:K,reset:A,trigger:J,reload:k,resetField:q,setValue:x,getValues:X,getErrors:B,getFieldStates:W,getFormState:L,setError:e,clearError:f,clearErrors:S,checkValidity:F,getNumberFields:ue,getFieldValidity:se,getDefaultValues:ne,setFieldState:_,triggerFieldBlur:T,resetFieldState:re,getControlledFields:oe,setFormState:ee,getEvents:n.getEvents,getWatchEvents:n.getWatchEvents}),[g]),D=t.useCallback(()=>{[...d.current.querySelectorAll("[name]")].forEach(m=>{!m.defaultValue&&!v.current.has(m.name)&&i.current[m.name]&&(m.defaultValue=i.current[m.name]),m.type==="number"&&!C.current.includes(m.name)&&C.current.push(m.name)});const a=N();l.current={...a},i.current={...a}},[]),ae=t.useMemo(()=>({ref:d,watch:Z,actions:P,initForm:D,registerController:O,registerHookWatcher:U,lastReloadedAt:g,loadFormValues:N,getWatchValue:Q,channels:n}),[g]),Ce=t.useMemo(()=>({control:ae,actions:P,watch:Z}),[g]);return t.useLayoutEffect(()=>{d.current&&D()},[g]),t.useLayoutEffect(()=>{E(!1)},[]),Ce};exports.Controller=Fe;exports.Form=pe;exports.useController=ke;exports.useForm=Ne;exports.useFormContext=le;exports.useWatch=Re;
|
|
22
|
+
<%s key={someKey} {...props} />`,_,A,e,A),z[A+_]=!0)}if(A=null,p!==void 0&&(o(p),A=""+p),c(g)&&(o(g.key),A=""+g.key),"key"in g){p={};for(var n in g)n!=="key"&&(p[n]=g[n])}else p=g;return A&&b(p,typeof r=="function"?r.displayName||r.name||"Unknown":r),m(r,A,D,W,a(),p,q,G)}function R(r){typeof r=="object"&&r!==null&&r.$$typeof===O&&r._store&&(r._store.validated=1)}var C=s,O=Symbol.for("react.transitional.element"),S=Symbol.for("react.portal"),v=Symbol.for("react.fragment"),F=Symbol.for("react.strict_mode"),j=Symbol.for("react.profiler"),N=Symbol.for("react.consumer"),M=Symbol.for("react.context"),U=Symbol.for("react.forward_ref"),oe=Symbol.for("react.suspense"),Z=Symbol.for("react.suspense_list"),B=Symbol.for("react.memo"),y=Symbol.for("react.lazy"),Q=Symbol.for("react.activity"),ue=Symbol.for("react.client.reference"),J=C.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,K=Object.prototype.hasOwnProperty,ee=Array.isArray,Y=console.createTask?console.createTask:function(){return null};C={"react-stack-bottom-frame":function(r){return r()}};var re,x={},te=C["react-stack-bottom-frame"].bind(C,f)(),se=Y(i(f)),z={};X.Fragment=v,X.jsx=function(r,g,p,_,W){var D=1e4>J.recentlyCreatedOwnerStacks++;return l(r,g,p,!1,_,W,D?Error("react-stack-top-frame"):te,D?Y(i(r)):se)},X.jsxs=function(r,g,p,_,W){var D=1e4>J.recentlyCreatedOwnerStacks++;return l(r,g,p,!0,_,W,D?Error("react-stack-top-frame"):te,D?Y(i(r)):se)}}()),X}var de;function Fe(){return de||(de=1,process.env.NODE_ENV==="production"?ne.exports=ke():ne.exports=ye()),ne.exports}var be=Fe();const k=()=>{},he=s.createContext({ref:null,watch:k,actions:{reset:k,resetField:k,setValue:k,getValues:k,getErrors:k,getFieldStates:k,getFormStates:k,setError:k,clearError:k,clearErrors:k,checkValidity:k,getNumberFields:k,getFieldValidity:k,getDefaultValues:k,setFieldState:k,resetFieldState:k,getControlledFields:k},registerController:k,registerHookWatcher:k,lastReloadedAt:k,loadFormValues:k,getWatchValue:k,channels:{}}),le=()=>s.useContext(he),Te=({id:u,control:t,method:o,action:i,children:a,onChange:f,onBlur:c,onSubmit:b=()=>{},onInput:d=()=>{},onReset:m=()=>{},numberFields:l=[],className:R,...C})=>{const O=s.useCallback(S=>{t.ref&&(t.ref.current=S,t.ref.current&&t.initForm())},[t]);return s.useEffect(()=>{let S=()=>{},v=()=>{};return f&&(S=t.channels.subscribe("onChange",f)),c&&(v=t.channels.subscribe("onBlur",c)),()=>{S(),v()}},[t.lastReloadedAt]),be.jsx(he.Provider,{value:t,children:be.jsx("form",{id:u,ref:O,action:i,method:o,className:R,onInput:d,onSubmit:S=>{o||S.preventDefault();const v=t.loadFormValues();b(v)},onChange:S=>{const v=S.target.name;!v||t.actions.getControlledFields().has(v)||t.actions.setValue(v,S.target.value)},onBlur:S=>{const v=S.target.name;if(!v||t.actions.getControlledFields().has(v))return;const F=S.target.value;t.channels.publish("onBlur",v,F,t.actions.getValues())},onReset:S=>{t.actions.reset(),m(S)},...C,children:a},t.lastReloadedAt)})},ae={isDirty:!1,isTouched:!1,error:null},Ee={lastReset:null,isDirty:!1,isError:!1,errorFields:[],dirtyFields:[],touchedFields:[]},_e={},Ae=["badInput","customError","patternMismatch","rangeOverflow","rangeUnderflow","stepMismatch","tooLong","tooShort","typeMismatch","valueMissing"],P=(u={},t="")=>t.split(".").reduce((o,i)=>o&&o[i]!==void 0?o[i]:void 0,u),$=(u={},t="",o)=>{const i={...u};let a=i;const f=t.split(".");return f.forEach((c,b)=>{b<f.length-1?(a[c]||(a[c]={}),Array.isArray(a[c])?a[c]=[...a[c]]:a[c]={...a[c]},a=a[c]):a[c]=o}),i},me=u=>{let t={...u};return Object.keys(t).forEach(o=>{if(o.includes(".")){const i=P(t,o);t=$(t,o,i||""),delete t[o]}}),t},Oe=["errors","fieldStates","formState"],L=u=>Oe.some(t=>u.startsWith(t))?u:"values."+u,ge=u=>{typeof u.setCustomValidity=="function"&&u.setCustomValidity("");let t=u.validity,o=Ae.find(i=>t[i]);if(o)return{type:o,message:u.validationMessage}},Re=(u=new Set,t={},o="")=>Object.keys(t).reduce((i,a)=>{const f=t[a];return u.has(o+a)?[...i,...Re(u,f,o+a+".")]:f.isDirty?[...i,o+a]:i},[]),ve=(u=new Set,t={},o="")=>Object.keys(t).reduce((i,a)=>{const f=t[a];return u.has(o+a)?[...i,...ve(u,f,o+a+".")]:f.isTouched?[...i,o+a]:i},[]),Se=(u=new Set,t={},o="")=>Object.keys(t).reduce((i,a)=>{const f=t[a];return u.has(o+a)?[...i,...Se(u,f,o+a+".")]:f?[...i,o+a]:i},[]),je=({control:u,name:t,compute:o})=>{const{getWatchValue:i,registerHookWatcher:a}=u||le(),f=i({name:t,compute:o}),[c,b]=s.useState(f);return s.useEffect(()=>a({name:t,compute:o,value:c,setValue:b}),[]),c},Ce=({control:u,name:t,defaultValue:o})=>{const{actions:i,registerController:a,channels:f,getWatchValue:c}=u||le(),b=s.useRef(),d=s.useRef(),m=P(i.getDefaultValues(),t)||o||"",[l,R]=s.useState(m),[C,O]=s.useState({}),S=s.useCallback((F,{shouldDirty:j=!0,shouldOnChange:N=!0}={})=>{var U;let M=F;((U=F==null?void 0:F.target)==null?void 0:U.value)!==void 0&&(M=F.target.value),R(M),i.setValue(t,M,{shouldDirty:j,shouldOnChange:N})},[i.setValue]),v=s.useCallback(F=>{const j=i.getValues(),N=F??P(j,t);f.publish("onBlur",t,N,j)},[]);return s.useEffect(()=>{const F=a(t,R);return()=>{F(),d.current&&d.current()}},[]),new Proxy({ref:b,name:t,defaultValue:m,value:l,onChange:S,onBlur:v,fieldState:C},{get(F,j,N){return typeof j=="string"&&j==="fieldState"&&(d.current&&d.current(),d.current=i.subscribe(`fieldStates.${t}`,()=>{O(c({name:`fieldStates.${t}`}))})),Reflect.get(F,j,N)}})},we=({name:u,control:t,defaultValue:o,render:i=({ref:a,name:f,defaultValue:c,value:b,onChange:d,onBlur:m,fieldState:l})=>null})=>{const a=Ce({name:u,defaultValue:o,control:t});return i(a)},Pe=()=>{const[u,t]=s.useState(),o=s.useCallback(()=>t(new Date().toString()),[]);return[u,o]},Ne=()=>{const[,u]=s.useState({});return s.useCallback(()=>u({}),[])},De=({channels:u,getWatchValue:t})=>{const o=Ne(),i=s.useCallback(c=>{if(!c)return u.subscribeWatch("values",()=>o()),t();if(typeof c=="string"){const b=L(c);u.subscribeWatch(b,(d,m)=>{d!==m&&o()})}return Array.isArray(c)&&c.forEach(b=>{const d=L(b);u.subscribeWatch(d,(m,l)=>{m!==l&&o()})}),t({name:c})},[]),a=s.useCallback(({name:c,compute:b,setValue:d})=>{if(typeof b=="function")return u.subscribe("values",()=>{const l=t({compute:b});d(l)});if(!c)return u.subscribe("values",()=>{d(t())});if(typeof c=="string"){const m=L(c);return u.subscribe(m,()=>{const R=t({name:c});d(R)})}if(Array.isArray(c)){const m=[];return c.forEach(l=>{const R=L(l),C=u.subscribe(R,()=>{const O=t({name:c});d(O)});m.push(C)}),()=>m.forEach(l=>l())}throw new Error("Parameters of name must be string or array of string or compute must be a function")},[]),f=s.useCallback((c,b)=>{if(!c)return u.subscribe("values",()=>b(t()));if(["onChange","onBlur"].includes(c))return u.subscribe(c,b);if(typeof c=="string"){const d=L(c);return u.subscribe(d,()=>b(t({name:c})))}if(Array.isArray(c)){const d=[];return c.forEach(m=>{const l=L(m),R=u.subscribe(l,()=>{b(t({name:c}))});d.push(R)}),()=>d.forEach(m=>m())}throw new Error("Parameters of name must be string or array of string")},[]);return{watch:i,registerHookWatcher:a,getWatchValue:t,subscribe:f}},xe=({getWatchValue:u})=>{const t=s.useRef(new Map),o=s.useRef(new Map),i=s.useCallback(()=>o.current,[]),a=s.useCallback(()=>t.current,[]),f=s.useCallback(()=>{t.current.clear(),o.current.clear()},[]),c=s.useCallback((l,...R)=>{const C=t.current.get(l),O=o.current.get(l);C&&C.forEach(S=>S(...R)),O&&O.forEach(S=>S(...R)),t.current.forEach((S,v)=>{if(v!==l&&l.startsWith(v)){const F=u({name:v.replace("values.","").replace("values","")});S.forEach(j=>j($(F,l.replace(`${v}.`,""),R[0])))}}),o.current.forEach((S,v)=>{if(v!==l&&l.startsWith(v)){const F=u({name:v.replace("values.","").replace("values","")});S.forEach(j=>j($(F,l.replace(`${v}.`,""),R[0])))}})},[]),b=s.useCallback(l=>{t.current.forEach((R,C)=>{C.startsWith(l)&&R.forEach(O=>O(u({name:C.replace("values.","").replace("values","")})))}),o.current.forEach((R,C)=>{C.startsWith(l)&&R.forEach(O=>O(u({name:C.replace("values.","").replace("values","")})))})},[]),d=s.useCallback((l,R)=>(t.current.has(l)||t.current.set(l,new Set),t.current.get(l).add(R),()=>{var C;return(C=t.current.get(l))==null?void 0:C.delete(R)}),[]),m=s.useCallback((l,R)=>{o.current.has(l)||(o.current.set(l,new Set),o.current.get(l).add(R))},[]);return{reset:f,publish:c,subscribe:d,subscribeWatch:m,getEvents:a,getWatchEvents:i,trigger:b}},$e=({defaultValues:u=_e,shouldUnRegister:t=!1}={})=>{const[o,i]=Pe(),a=s.useRef(),f=s.useRef(new Map),c=s.useRef({}),b=s.useRef({}),d=s.useRef({...Ee}),m=s.useRef({...u}),l=s.useRef({...u}),R=s.useRef(new Set),C=s.useCallback((e,n=l.current)=>Object.keys(n).reduce((E,h)=>{if(typeof n[h]=="object")return{...E,[h]:C(e,n[h])};const w={...e?{}:b.current[h]||{},...ae};return{...E,[h]:w}},{}),[]),O=s.useCallback((e={},{clearCustomFormStates:n=!1,clearCustomFieldStates:E=!1}={})=>{l.current={...l.current,...e},m.current={...l.current},f.current.forEach((h,w)=>{h(P(l.current,w)??"")}),c.current={},f.current=new Map,d.current={...n?{}:d.current,...Ee},b.current=C(E),y.reset(),i()},[]),S=s.useCallback(()=>{let e=Object.fromEntries(new FormData(a.current));f.current.forEach((E,h)=>{e=$(e,h,P(m.current,h))});let n={...m.current,...e};return me(n)},[]),v=s.useCallback(e=>ge(e.target),[]),F=s.useCallback(()=>l.current,[]),j=s.useCallback(()=>f.current,[]),N=s.useCallback((e,n)=>n?Array.isArray(n)?n.reduce((E,h)=>({...E,[h]:P(e,h)}),{}):P(e,n):{...e},[]),M=s.useCallback(e=>N(m.current,e),[N]),U=s.useCallback(e=>N(c.current,e),[N]),oe=s.useCallback(e=>N(b.current,e),[N]),Z=s.useCallback(e=>N({...d.current,lastReset:o},e),[o]),B=s.useCallback(({name:e,compute:n}={})=>!e&&!n||e==="values"?M():typeof n=="function"?n(M()):Array.isArray(e)?e.reduce((E,h)=>({...E,[h]:B({name:h})}),{}):P({...m.current,errors:c.current,fieldStates:b.current,formState:Z()},e),[]),y=xe({getWatchValue:B}),{watch:Q,registerHookWatcher:ue,subscribe:J}=De({getWatchValue:B,channels:y}),K=s.useCallback((e,n)=>{const E=d.current,h=P(E,e);h!==n&&(d.current=$(d.current,e,n),y.publish(`formState.${e}`,n,h))},[]),ee=s.useCallback((e,n)=>{x(e,"error",n)},[]),Y=s.useCallback(e=>{x(e,"error",null)},[]),re=s.useCallback(()=>{d.current.isError&&Object.keys(c.current).forEach(e=>Y(e))},[]),x=s.useCallback((e,n,E)=>{try{if(typeof E=="function"&&(E=E(B({name:e}))),R.current.has(e))if(typeof E=="object"&&E!==null){Object.keys(E).forEach(T=>{x(`${e}.${T}`,n,E[T])});return}else return console.error(`Cannot set primitive value for nested parent field "${e}". Please set value for its children or provide an object.`);const h=P(b.current,e)||{...ae},w=P(h,n);if(w!==E){if(b.current=$(b.current,`${e}.${n}`,E),y.publish(`fieldStates.${e}.${n}`,E,w),n==="error"){const T=P(c.current,e),I=E||null;T!==I&&(c.current=$(c.current,e,I),y.publish(`errors.${e}`,I,T));const V=Se(R.current,c.current);d.current.errorFields=V,y.publish("formState.errorFields",V);const ce=V.length>0;if(d.current.isError!==ce){const pe=d.current.isError;d.current.isError=ce,y.publish("formState.isError",ce,pe)}}if(n==="isDirty"){const T=Re(R.current,b.current);d.current.dirtyFields=T,y.publish("formState.dirtyFields",T);const I=T.length>0;if(d.current.isDirty!==I){const V=d.current.isDirty;d.current.isDirty=I,y.publish("formState.isDirty",I,V)}}if(n==="isTouched"){const T=ve(R.current,b.current);d.current.touchedFields=T,y.publish("formState.touchedFields",T)}}}catch(h){console.log(h)}},[]),te=s.useCallback(e=>{x(e,"isDirty",!1),x(e,"isTouched",!1),x(e,"error",!1)},[]),se=s.useCallback(e=>{const n=ge(e.target);n?ee(e.target.name,n):Y(e.target.name)},[]),z=s.useCallback((e,n,{shouldDirty:E=!0,shouldOnChange:h=!0}={})=>{if(typeof n=="function"&&(n=n(M(e))),R.current.has(e)){typeof n=="object"&&n!==null?Object.keys(n).forEach(V=>{z(`${e}.${V}`,n[V],{shouldDirty:E,shouldOnChange:h})}):console.error(`Cannot set primitive value for nested parent field "${e}". Please set value for its children or provide an object.`);return}const w=P(m.current,e);if(n===w)return;m.current=$(m.current,e,n),y.publish(`values.${e}`,n,w);const T=f.current.get(e);T&&T(n),n&&x(e,"isTouched",!0);const I=n!==(P(l.current,e)||"");E&&x(e,"isDirty",I),h&&y.publish("onChange",e,n,w)},[]),r=s.useCallback(e=>{const n=e.split(".");let E="";n.forEach((h,w)=>{w<n.length-1&&(E=E?`${E}.${h}`:h,R.current.add(E))})},[]),g=s.useCallback((e="",n)=>(f.current.set(e,n),b.current=$(b.current,e,{...ae}),e.includes(".")&&r(e),()=>{t&&f.current.delete(e)}),[]),p=s.useCallback((e,{keepError:n,keepDirty:E,keepTouched:h,defaultValue:w})=>{const T=f.current.get(e);if(!T)throw new Error(`Cannot reset "${e}" because it's uncontrolled field`);l.current=$(l.current,e,w),m.current=$(m.current,e,w),T&&T(w),n||Y(e),E||x(e,"isDirty",!1),h||x(e,"isTouched",!1)},[]),_=s.useCallback(e=>{if(!e)return y.trigger("values");if(!Array.isArray(e)){const n=L(e);y.trigger(n);return}e.forEach(n=>{const E=L(n);y.trigger(E)})},[]),W=s.useCallback((e,n=D.getValues(e))=>{y.publish("onBlur",e,n,D.getValues())},[]),D=s.useMemo(()=>({subscribe:J,reset:O,trigger:_,reload:i,resetField:p,setValue:z,getValues:M,getErrors:U,getFieldStates:oe,getFormState:Z,setError:ee,clearError:Y,clearErrors:re,checkValidity:se,getFieldValidity:v,getDefaultValues:F,setFieldState:x,triggerFieldBlur:W,resetFieldState:te,getControlledFields:j,setFormState:K,getEvents:y.getEvents,getWatchEvents:y.getWatchEvents}),[o]),q=s.useCallback(()=>{[...a.current.querySelectorAll("[name]")].forEach(E=>{const h=E.name||"";h.includes(".")&&r(h),!E.defaultValue&&!f.current.has(h)&&P(l.current,h)&&(E.defaultValue=P(l.current,h))});const n=S();m.current={...n},l.current={...n}},[]),G=s.useMemo(()=>({ref:a,watch:Q,actions:D,initForm:q,registerController:g,registerHookWatcher:ue,lastReloadedAt:o,loadFormValues:S,getWatchValue:B,channels:y}),[o]),A=s.useMemo(()=>({control:G,actions:D,watch:Q}),[o]);return s.useLayoutEffect(()=>{a.current&&q()},[o]),s.useLayoutEffect(()=>{b.current=C(!1)},[]),A};exports.Controller=we;exports.Form=Te;exports.restoreFromDotNotation=me;exports.useController=Ce;exports.useForm=$e;exports.useFormContext=le;exports.useWatch=je;
|