@octanejs/hook-form 0.1.0
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/LICENSE +21 -0
- package/README.md +62 -0
- package/package.json +46 -0
- package/src/FormProvider.tsrx +76 -0
- package/src/FormProvider.tsrx.d.ts +10 -0
- package/src/constants.ts +31 -0
- package/src/controller.tsrx +8 -0
- package/src/controller.tsrx.d.ts +52 -0
- package/src/form.tsrx +119 -0
- package/src/form.tsrx.d.ts +6 -0
- package/src/formStateSubscribe.tsrx +10 -0
- package/src/formStateSubscribe.tsrx.d.ts +16 -0
- package/src/index.ts +19 -0
- package/src/logic/appendErrors.ts +19 -0
- package/src/logic/createFormControl.ts +1841 -0
- package/src/logic/generateId.ts +14 -0
- package/src/logic/generateWatchOutput.ts +27 -0
- package/src/logic/getCheckboxValue.ts +36 -0
- package/src/logic/getDirtyFields.ts +120 -0
- package/src/logic/getEventValue.ts +12 -0
- package/src/logic/getFieldValue.ts +33 -0
- package/src/logic/getFieldValueAs.ts +22 -0
- package/src/logic/getFocusFieldName.ts +13 -0
- package/src/logic/getNodeParentName.ts +4 -0
- package/src/logic/getProxyFormState.ts +33 -0
- package/src/logic/getRadioValue.ts +24 -0
- package/src/logic/getResolverOptions.ts +33 -0
- package/src/logic/getRuleValue.ts +16 -0
- package/src/logic/getValidateError.ts +22 -0
- package/src/logic/getValidationModes.ts +11 -0
- package/src/logic/getValueAndMessage.ts +12 -0
- package/src/logic/hasPromiseValidation.ts +27 -0
- package/src/logic/hasValidation.ts +12 -0
- package/src/logic/index.ts +3 -0
- package/src/logic/isNameInFieldArray.ts +7 -0
- package/src/logic/isWatched.ts +11 -0
- package/src/logic/iterateFieldsByAction.ts +37 -0
- package/src/logic/schemaErrorLookup.ts +54 -0
- package/src/logic/shouldRenderFormState.ts +25 -0
- package/src/logic/shouldSubscribeByName.ts +18 -0
- package/src/logic/skipValidation.ts +21 -0
- package/src/logic/unsetEmptyArray.ts +6 -0
- package/src/logic/updateFieldArrayRootError.ts +17 -0
- package/src/logic/validateField.ts +273 -0
- package/src/types/controller.ts +98 -0
- package/src/types/errors.ts +56 -0
- package/src/types/events.ts +26 -0
- package/src/types/fieldArray.ts +288 -0
- package/src/types/fields.ts +46 -0
- package/src/types/form.ts +961 -0
- package/src/types/index.ts +12 -0
- package/src/types/path/common.ts +391 -0
- package/src/types/path/eager.ts +224 -0
- package/src/types/path/index.ts +16 -0
- package/src/types/resolvers.ts +38 -0
- package/src/types/utils.ts +125 -0
- package/src/types/validator.ts +94 -0
- package/src/types/watch.ts +66 -0
- package/src/useController.ts +268 -0
- package/src/useFieldArray.ts +490 -0
- package/src/useForm.ts +182 -0
- package/src/useFormContext.ts +47 -0
- package/src/useFormControlContext.ts +25 -0
- package/src/useFormState.ts +86 -0
- package/src/useIsomorphicLayoutEffect.ts +6 -0
- package/src/useWatch.ts +356 -0
- package/src/utils/append.ts +4 -0
- package/src/utils/cloneObject.ts +32 -0
- package/src/utils/compact.ts +2 -0
- package/src/utils/convertToArrayPayload.ts +2 -0
- package/src/utils/createSubject.ts +48 -0
- package/src/utils/deepEqual.ts +81 -0
- package/src/utils/deepMerge.ts +27 -0
- package/src/utils/extractFormValues.ts +28 -0
- package/src/utils/fillEmptyArray.ts +3 -0
- package/src/utils/flatten.ts +23 -0
- package/src/utils/get.ts +29 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/insert.ts +8 -0
- package/src/utils/isBoolean.ts +2 -0
- package/src/utils/isCheckBoxInput.ts +4 -0
- package/src/utils/isDateObject.ts +2 -0
- package/src/utils/isEmptyObject.ts +7 -0
- package/src/utils/isFileInput.ts +4 -0
- package/src/utils/isFunction.ts +2 -0
- package/src/utils/isHTMLElement.ts +13 -0
- package/src/utils/isKey.ts +4 -0
- package/src/utils/isMultipleSelect.ts +5 -0
- package/src/utils/isNullOrUndefined.ts +2 -0
- package/src/utils/isObject.ts +8 -0
- package/src/utils/isPlainObject.ts +8 -0
- package/src/utils/isPrimitive.ts +8 -0
- package/src/utils/isRadioInput.ts +4 -0
- package/src/utils/isRadioOrCheckbox.ts +8 -0
- package/src/utils/isRegex.ts +2 -0
- package/src/utils/isString.ts +2 -0
- package/src/utils/isUndefined.ts +2 -0
- package/src/utils/isWeb.ts +4 -0
- package/src/utils/live.ts +6 -0
- package/src/utils/move.ts +15 -0
- package/src/utils/noop.ts +2 -0
- package/src/utils/objectHasFunction.ts +11 -0
- package/src/utils/prepend.ts +7 -0
- package/src/utils/remove.ts +24 -0
- package/src/utils/set.ts +36 -0
- package/src/utils/sleep.ts +2 -0
- package/src/utils/stringToPath.ts +4 -0
- package/src/utils/swap.ts +4 -0
- package/src/utils/unset.ts +65 -0
- package/src/utils/update.ts +5 -0
- package/src/watch.tsrx +7 -0
- package/src/watch.tsrx.d.ts +42 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Vendored from react-hook-form@7.81.0 src/utils/deepMerge.ts (octane port).
|
|
2
|
+
import isObject from './isObject';
|
|
3
|
+
import isPrimitive from './isPrimitive';
|
|
4
|
+
|
|
5
|
+
export default function deepMerge<T extends Record<keyof T, any>, U extends Record<keyof U, any>>(
|
|
6
|
+
target: T,
|
|
7
|
+
source: U,
|
|
8
|
+
): T & U {
|
|
9
|
+
if (isPrimitive(target) || isPrimitive(source)) {
|
|
10
|
+
return source;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
for (const key in source) {
|
|
14
|
+
const targetValue = target[key];
|
|
15
|
+
const sourceValue = source[key];
|
|
16
|
+
|
|
17
|
+
try {
|
|
18
|
+
target[key] =
|
|
19
|
+
(isObject(targetValue) && isObject(sourceValue)) ||
|
|
20
|
+
(Array.isArray(targetValue) && Array.isArray(sourceValue))
|
|
21
|
+
? deepMerge(targetValue, sourceValue)
|
|
22
|
+
: sourceValue;
|
|
23
|
+
} catch {}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return target;
|
|
27
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Vendored from react-hook-form@7.81.0 src/utils/extractFormValues.ts (octane port).
|
|
2
|
+
import isObject from './isObject';
|
|
3
|
+
|
|
4
|
+
export default function extractFormValues<T extends object, K extends Record<string, unknown>>(
|
|
5
|
+
fieldsState: T,
|
|
6
|
+
formValues: K,
|
|
7
|
+
) {
|
|
8
|
+
const values: Record<string, unknown> = {};
|
|
9
|
+
|
|
10
|
+
for (const key in fieldsState) {
|
|
11
|
+
if (fieldsState.hasOwnProperty(key)) {
|
|
12
|
+
const fieldState = fieldsState[key];
|
|
13
|
+
const fieldValue = formValues[key];
|
|
14
|
+
|
|
15
|
+
if (fieldState && isObject(fieldState) && fieldValue) {
|
|
16
|
+
const nestedFieldsState = extractFormValues(fieldState, fieldValue as K);
|
|
17
|
+
|
|
18
|
+
if (isObject(nestedFieldsState)) {
|
|
19
|
+
values[key] = nestedFieldsState;
|
|
20
|
+
}
|
|
21
|
+
} else if (fieldsState[key]) {
|
|
22
|
+
values[key] = fieldValue;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return values;
|
|
28
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Vendored from react-hook-form@7.81.0 src/utils/flatten.ts (octane port).
|
|
2
|
+
import type { FieldValues } from '../types';
|
|
3
|
+
|
|
4
|
+
import isDateObject from './isDateObject';
|
|
5
|
+
import { isObjectType } from './isObject';
|
|
6
|
+
|
|
7
|
+
export const flatten = (obj: FieldValues) => {
|
|
8
|
+
const output: FieldValues = {};
|
|
9
|
+
|
|
10
|
+
for (const key of Object.keys(obj)) {
|
|
11
|
+
if (isObjectType(obj[key]) && obj[key] !== null && !isDateObject(obj[key])) {
|
|
12
|
+
const nested = flatten(obj[key]);
|
|
13
|
+
|
|
14
|
+
for (const nestedKey of Object.keys(nested)) {
|
|
15
|
+
output[`${key}.${nestedKey}`] = nested[nestedKey];
|
|
16
|
+
}
|
|
17
|
+
} else {
|
|
18
|
+
output[key] = obj[key];
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return output;
|
|
23
|
+
};
|
package/src/utils/get.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Vendored from react-hook-form@7.81.0 src/utils/get.ts (octane port).
|
|
2
|
+
import { PROTOTYPE_KEYWORDS } from '../constants';
|
|
3
|
+
|
|
4
|
+
import isKey from './isKey';
|
|
5
|
+
import isNullOrUndefined from './isNullOrUndefined';
|
|
6
|
+
import isObject from './isObject';
|
|
7
|
+
import isUndefined from './isUndefined';
|
|
8
|
+
import stringToPath from './stringToPath';
|
|
9
|
+
|
|
10
|
+
export default <T>(object: T, path?: string | null, defaultValue?: unknown): any => {
|
|
11
|
+
if (!path || !isObject(object)) {
|
|
12
|
+
return defaultValue;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const paths = isKey(path) ? [path] : stringToPath(path);
|
|
16
|
+
if (paths.some((key) => PROTOTYPE_KEYWORDS.includes(key))) {
|
|
17
|
+
return defaultValue;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const result = paths.reduce<any>((result, key) => {
|
|
21
|
+
return isNullOrUndefined(result) ? undefined : result[key];
|
|
22
|
+
}, object);
|
|
23
|
+
|
|
24
|
+
return isUndefined(result) || result === object
|
|
25
|
+
? isUndefined(object[path as keyof T])
|
|
26
|
+
? defaultValue
|
|
27
|
+
: object[path as keyof T]
|
|
28
|
+
: result;
|
|
29
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Vendored from react-hook-form@7.81.0 src/utils/insert.ts (octane port).
|
|
2
|
+
import convertToArrayPayload from './convertToArrayPayload';
|
|
3
|
+
|
|
4
|
+
export default function insert<T>(data: T[], index: number): (T | undefined)[];
|
|
5
|
+
export default function insert<T>(data: T[], index: number, value: T | T[]): T[];
|
|
6
|
+
export default function insert<T>(data: T[], index: number, value?: T | T[]): (T | undefined)[] {
|
|
7
|
+
return [...data.slice(0, index), ...convertToArrayPayload(value), ...data.slice(index)];
|
|
8
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// Vendored from react-hook-form@7.81.0 src/utils/isEmptyObject.ts (octane port).
|
|
2
|
+
import type { EmptyObject } from '../types';
|
|
3
|
+
|
|
4
|
+
import isObject from './isObject';
|
|
5
|
+
|
|
6
|
+
export default (value: unknown): value is EmptyObject =>
|
|
7
|
+
isObject(value) && !Object.keys(value).length;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Vendored from react-hook-form@7.81.0 src/utils/isHTMLElement.ts (octane port).
|
|
2
|
+
import isWeb from './isWeb';
|
|
3
|
+
|
|
4
|
+
export default (value: unknown): value is HTMLElement => {
|
|
5
|
+
if (!isWeb) {
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const owner = value ? ((value as HTMLElement).ownerDocument as Document) : 0;
|
|
10
|
+
return (
|
|
11
|
+
value instanceof (owner && owner.defaultView ? owner.defaultView.HTMLElement : HTMLElement)
|
|
12
|
+
);
|
|
13
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Vendored from react-hook-form@7.81.0 src/utils/isObject.ts (octane port).
|
|
2
|
+
import isDateObject from './isDateObject';
|
|
3
|
+
import isNullOrUndefined from './isNullOrUndefined';
|
|
4
|
+
|
|
5
|
+
export const isObjectType = (value: unknown): value is object => typeof value === 'object';
|
|
6
|
+
|
|
7
|
+
export default <T extends object>(value: unknown): value is T =>
|
|
8
|
+
!isNullOrUndefined(value) && !Array.isArray(value) && isObjectType(value) && !isDateObject(value);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Vendored from react-hook-form@7.81.0 src/utils/isPlainObject.ts (octane port).
|
|
2
|
+
import isObject from './isObject';
|
|
3
|
+
|
|
4
|
+
export default (tempObject: object) => {
|
|
5
|
+
const prototypeCopy = tempObject.constructor && tempObject.constructor.prototype;
|
|
6
|
+
|
|
7
|
+
return isObject(prototypeCopy) && prototypeCopy.hasOwnProperty('isPrototypeOf');
|
|
8
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Vendored from react-hook-form@7.81.0 src/utils/isPrimitive.ts (octane port).
|
|
2
|
+
import type { Primitive } from '../types';
|
|
3
|
+
|
|
4
|
+
import isNullOrUndefined from './isNullOrUndefined';
|
|
5
|
+
import { isObjectType } from './isObject';
|
|
6
|
+
|
|
7
|
+
export default (value: unknown): value is Primitive =>
|
|
8
|
+
isNullOrUndefined(value) || !isObjectType(value);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Vendored from react-hook-form@7.81.0 src/utils/isRadioOrCheckbox.ts (octane port).
|
|
2
|
+
import type { FieldElement } from '../types';
|
|
3
|
+
|
|
4
|
+
import isCheckBoxInput from './isCheckBoxInput';
|
|
5
|
+
import isRadioInput from './isRadioInput';
|
|
6
|
+
|
|
7
|
+
export default (ref: FieldElement): ref is HTMLInputElement =>
|
|
8
|
+
isRadioInput(ref) || isCheckBoxInput(ref);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Vendored from react-hook-form@7.81.0 src/utils/move.ts (octane port).
|
|
2
|
+
import isUndefined from './isUndefined';
|
|
3
|
+
|
|
4
|
+
export default <T>(data: (T | undefined)[], from: number, to: number): (T | undefined)[] => {
|
|
5
|
+
if (!Array.isArray(data)) {
|
|
6
|
+
return [];
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
if (isUndefined(data[to])) {
|
|
10
|
+
data[to] = undefined;
|
|
11
|
+
}
|
|
12
|
+
data.splice(to, 0, data.splice(from, 1)[0]);
|
|
13
|
+
|
|
14
|
+
return data;
|
|
15
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Vendored from react-hook-form@7.81.0 src/utils/objectHasFunction.ts (octane port).
|
|
2
|
+
import isFunction from './isFunction';
|
|
3
|
+
|
|
4
|
+
export default <T>(data: T): boolean => {
|
|
5
|
+
for (const key in data) {
|
|
6
|
+
if (isFunction(data[key])) {
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
return false;
|
|
11
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// Vendored from react-hook-form@7.81.0 src/utils/prepend.ts (octane port).
|
|
2
|
+
import convertToArrayPayload from './convertToArrayPayload';
|
|
3
|
+
|
|
4
|
+
export default <T>(data: T[], value: T | T[]): T[] => [
|
|
5
|
+
...convertToArrayPayload(value),
|
|
6
|
+
...convertToArrayPayload(data),
|
|
7
|
+
];
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Vendored from react-hook-form@7.81.0 src/utils/remove.ts (octane port).
|
|
2
|
+
import compact from './compact';
|
|
3
|
+
import convertToArrayPayload from './convertToArrayPayload';
|
|
4
|
+
import isUndefined from './isUndefined';
|
|
5
|
+
|
|
6
|
+
function removeAtIndexes<T>(data: T[], indexes: number[]): T[] {
|
|
7
|
+
let i = 0;
|
|
8
|
+
const temp = [...data];
|
|
9
|
+
|
|
10
|
+
for (const index of indexes) {
|
|
11
|
+
temp.splice(index - i, 1);
|
|
12
|
+
i++;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return compact(temp).length ? temp : [];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export default <T>(data: T[], index?: number | number[]): T[] =>
|
|
19
|
+
isUndefined(index)
|
|
20
|
+
? []
|
|
21
|
+
: removeAtIndexes(
|
|
22
|
+
data,
|
|
23
|
+
(convertToArrayPayload(index) as number[]).sort((a, b) => a - b),
|
|
24
|
+
);
|
package/src/utils/set.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Vendored from react-hook-form@7.81.0 src/utils/set.ts (octane port).
|
|
2
|
+
import { PROTOTYPE_KEYWORDS } from '../constants';
|
|
3
|
+
import type { FieldPath, FieldValues } from '../types';
|
|
4
|
+
|
|
5
|
+
import isKey from './isKey';
|
|
6
|
+
import isObject from './isObject';
|
|
7
|
+
import stringToPath from './stringToPath';
|
|
8
|
+
|
|
9
|
+
export default (object: FieldValues, path: FieldPath<FieldValues>, value?: unknown) => {
|
|
10
|
+
let index = -1;
|
|
11
|
+
const tempPath = isKey(path) ? [path] : stringToPath(path);
|
|
12
|
+
const length = tempPath.length;
|
|
13
|
+
const lastIndex = length - 1;
|
|
14
|
+
|
|
15
|
+
while (++index < length) {
|
|
16
|
+
const key = tempPath[index];
|
|
17
|
+
let newValue = value;
|
|
18
|
+
|
|
19
|
+
if (index !== lastIndex) {
|
|
20
|
+
const objValue = object[key];
|
|
21
|
+
newValue =
|
|
22
|
+
isObject(objValue) || Array.isArray(objValue)
|
|
23
|
+
? objValue
|
|
24
|
+
: !isNaN(+tempPath[index + 1])
|
|
25
|
+
? []
|
|
26
|
+
: {};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (PROTOTYPE_KEYWORDS.includes(key)) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
object[key] = newValue;
|
|
34
|
+
object = object[key];
|
|
35
|
+
}
|
|
36
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// Vendored from react-hook-form@7.81.0 src/utils/unset.ts (octane port).
|
|
2
|
+
import { PROTOTYPE_KEYWORDS } from '../constants';
|
|
3
|
+
|
|
4
|
+
import isEmptyObject from './isEmptyObject';
|
|
5
|
+
import isKey from './isKey';
|
|
6
|
+
import isNullOrUndefined from './isNullOrUndefined';
|
|
7
|
+
import isObject from './isObject';
|
|
8
|
+
import isString from './isString';
|
|
9
|
+
import isUndefined from './isUndefined';
|
|
10
|
+
import stringToPath from './stringToPath';
|
|
11
|
+
|
|
12
|
+
function baseGet(object: any, updatePath: (string | number)[]) {
|
|
13
|
+
const length = updatePath.slice(0, -1).length;
|
|
14
|
+
let index = 0;
|
|
15
|
+
|
|
16
|
+
while (index < length) {
|
|
17
|
+
if (isNullOrUndefined(object)) {
|
|
18
|
+
object = undefined;
|
|
19
|
+
break;
|
|
20
|
+
}
|
|
21
|
+
object = object[updatePath[index]];
|
|
22
|
+
index++;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return object;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function isEmptyArray(obj: unknown[]) {
|
|
29
|
+
for (const key in obj) {
|
|
30
|
+
if (obj.hasOwnProperty(key) && !isUndefined(obj[key])) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export default function unset(object: any, path: string | (string | number)[]) {
|
|
38
|
+
if (isString(path) && Object.prototype.hasOwnProperty.call(object, path)) {
|
|
39
|
+
delete object[path];
|
|
40
|
+
return object;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const paths = Array.isArray(path) ? path : isKey(path) ? [path] : stringToPath(path);
|
|
44
|
+
if (paths.some((segment) => PROTOTYPE_KEYWORDS.includes(String(segment)))) {
|
|
45
|
+
return object;
|
|
46
|
+
}
|
|
47
|
+
const childObject = paths.length === 1 ? object : baseGet(object, paths);
|
|
48
|
+
|
|
49
|
+
const index = paths.length - 1;
|
|
50
|
+
const key = paths[index];
|
|
51
|
+
|
|
52
|
+
if (childObject) {
|
|
53
|
+
delete childObject[key];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (
|
|
57
|
+
index !== 0 &&
|
|
58
|
+
((isObject(childObject) && isEmptyObject(childObject)) ||
|
|
59
|
+
(Array.isArray(childObject) && isEmptyArray(childObject)))
|
|
60
|
+
) {
|
|
61
|
+
unset(object, paths.slice(0, -1));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return object;
|
|
65
|
+
}
|
package/src/watch.tsrx
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// Vendored from react-hook-form@7.81.0 src/watch.tsx (octane port).
|
|
2
|
+
// Watch component — subscribes to form field changes and re-renders when the
|
|
3
|
+
// watched fields update. Authored as .tsrx (full compile) so the useWatch call
|
|
4
|
+
// is slot-wrapped (see controller.tsrx note).
|
|
5
|
+
import { useWatch } from './useWatch.ts';
|
|
6
|
+
|
|
7
|
+
export const Watch = (props) => props.render(useWatch({ name: props.names, ...props }));
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// Type declaration for the .tsrx component (resolved by relative path).
|
|
2
|
+
import type { FieldPath, FieldValues, WatchProps } from './types';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Watch component that subscribes to form field changes and re-renders when watched fields update.
|
|
6
|
+
*
|
|
7
|
+
* @param control - The form control object from useForm
|
|
8
|
+
* @param name - Can be field name, array of field names, or undefined to watch the entire form
|
|
9
|
+
* @param disabled - Disable subscription
|
|
10
|
+
* @param exact - Whether to watch exact field names or not
|
|
11
|
+
* @param defaultValue - The default value to use if the field is not yet set
|
|
12
|
+
* @param compute - Function to compute derived values from watched fields
|
|
13
|
+
* @param render - The function that receives watched values and returns a renderable
|
|
14
|
+
* @returns The result of calling render function with watched values
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* The `Watch` component only re-renders when the values of `foo`, `bar`, and `baz.qux` change.
|
|
18
|
+
*
|
|
19
|
+
* ```tsx
|
|
20
|
+
* const { control } = useForm();
|
|
21
|
+
*
|
|
22
|
+
* <Watch
|
|
23
|
+
* control={control}
|
|
24
|
+
* names={['foo', 'bar', 'baz.qux']}
|
|
25
|
+
* render={([foo, bar, baz_qux]) => <div>{foo}{bar}{baz_qux}</div>}
|
|
26
|
+
* />
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare const Watch: <
|
|
30
|
+
TFieldValues extends FieldValues = FieldValues,
|
|
31
|
+
TFieldName extends
|
|
32
|
+
| FieldPath<TFieldValues>
|
|
33
|
+
| readonly [FieldPath<TFieldValues>, ...FieldPath<TFieldValues>[]]
|
|
34
|
+
| FieldPath<TFieldValues>[]
|
|
35
|
+
| readonly FieldPath<TFieldValues>[]
|
|
36
|
+
| undefined = undefined,
|
|
37
|
+
TContext = any,
|
|
38
|
+
TTransformedValues = TFieldValues,
|
|
39
|
+
TComputeValue = undefined,
|
|
40
|
+
>(
|
|
41
|
+
props: WatchProps<TFieldName, TFieldValues, TContext, TTransformedValues, TComputeValue>,
|
|
42
|
+
) => unknown;
|