Skip to content
This repository was archived by the owner on Jun 15, 2022. It is now read-only.

Commit 21e80f3

Browse files
authored
Merge pull request #16 from react-hook-form/improve-types
import types from core lib
2 parents 1f5ed93 + bded674 commit 21e80f3

File tree

6 files changed

+59
-76
lines changed

6 files changed

+59
-76
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
"prettier": "^2.0.5",
8282
"react": "^16.13.1",
8383
"react-dom": "^16.13.1",
84-
"react-hook-form": "^6.0.0-rc.5",
84+
"react-hook-form": "^6.7.0",
8585
"react-test-renderer": "^16.13.1",
8686
"rimraf": "^3.0.2",
8787
"rollup": "^2.17.0",
@@ -96,7 +96,7 @@
9696
"peerDependencies": {
9797
"react": ">=16.8.0",
9898
"react-dom": ">=16.8.0",
99-
"react-hook-form": ">=6.0.0",
99+
"react-hook-form": ">=6.6.0",
100100
"typescript": ">=3.0.1"
101101
},
102102
"husky": {

src/__snapshots__/useTypedController.test.tsx.snap

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
exports[`useTypedField should render correctly when as is input and name is string 1`] = `
44
<DocumentFragment>
55
<input
6+
name="flat"
67
value="test"
78
/>
89
</DocumentFragment>
910
`;
1011

1112
exports[`useTypedField should render correctly when as is textarea and name is array 1`] = `
1213
<DocumentFragment>
13-
<textarea>
14+
<textarea
15+
name="nested.object.test"
16+
>
1417
test
1518
</textarea>
1619
</DocumentFragment>
@@ -19,14 +22,17 @@ exports[`useTypedField should render correctly when as is textarea and name is a
1922
exports[`useTypedField should render correctly when name is array and render is input 1`] = `
2023
<DocumentFragment>
2124
<input
25+
name="nested.array[0].test"
2226
value="test"
2327
/>
2428
</DocumentFragment>
2529
`;
2630

2731
exports[`useTypedField should render correctly when name is string and render is textarea 1`] = `
2832
<DocumentFragment>
29-
<textarea>
33+
<textarea
34+
name="flat"
35+
>
3036
test
3137
</textarea>
3238
</DocumentFragment>

src/types.ts

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
import { NestedValue, Control } from 'react-hook-form';
1+
import {
2+
FieldValues,
3+
Control,
4+
Message,
5+
ValidationRule,
6+
ValidateResult,
7+
} from 'react-hook-form';
28

39
export type ArrayWithLength<N extends number> = { [K in N]: any };
410

5-
export interface DeepPathArray<TValues extends Record<string, any>, TPath>
11+
export interface DeepPathArray<TValues extends FieldValues, TPath>
612
extends ReadonlyArray<string | number> {
713
['0']: keyof TValues;
814
['1']?: TPath extends {
@@ -73,7 +79,7 @@ export interface DeepPathArray<TValues extends Record<string, any>, TPath>
7379
}
7480

7581
export type DeepPathArrayValue<
76-
TValues extends Record<string, any>,
82+
TValues extends FieldValues,
7783
TPath extends DeepPathArray<TValues, TPath>
7884
> = TPath extends ArrayWithLength<0 | 1 | 2 | 3 | 4 | 5 | 6>
7985
? any
@@ -91,54 +97,23 @@ export type DeepPathArrayValue<
9197
? TValues[TPath[0]]
9298
: never;
9399

94-
export type DeepPath<TValues extends Record<string, any>, TPath> =
100+
export type DeepPath<TValues extends FieldValues, TPath> =
95101
| DeepPathArray<TValues, TPath>
96102
| keyof TValues;
97103

98104
export type DeepPathValue<
99-
TValues extends Record<string, any>,
105+
TValues extends FieldValues,
100106
TPath extends DeepPath<TValues, TPath>
101107
> = TPath extends DeepPathArray<TValues, TPath>
102108
? DeepPathArrayValue<TValues, TPath>
103109
: TPath extends keyof TValues
104110
? TValues[TPath]
105111
: any;
106112

107-
export type NonUndefined<T> = T extends undefined ? never : T;
108-
109-
export type UnpackNestedValue<T> = NonUndefined<T> extends NestedValue<infer U>
110-
? U
111-
: NonUndefined<T> extends Date | FileList
112-
? T
113-
: NonUndefined<T> extends object
114-
? { [K in keyof T]: UnpackNestedValue<T[K]> }
115-
: T;
116-
117-
export type FieldValuesFromControl<
118-
TControl extends Control
119-
> = TControl extends Control<infer TFieldValues> ? TFieldValues : never;
120-
121113
export type Options<TControl extends Control> = {
122114
control?: TControl;
123115
};
124116

125-
export type Message = string;
126-
127-
export type ValidationValue = boolean | number | string | RegExp;
128-
129-
export type ValidationRule<
130-
TValidationValue extends ValidationValue = ValidationValue
131-
> = TValidationValue | ValidationValueMessage<TValidationValue>;
132-
133-
export type ValidationValueMessage<
134-
TValidationValue extends ValidationValue = ValidationValue
135-
> = {
136-
value: TValidationValue;
137-
message: Message;
138-
};
139-
140-
export type ValidateResult = Message | Message[] | boolean | undefined;
141-
142117
export type Validate<TFieldValue> = (
143118
data: TFieldValue,
144119
) => ValidateResult | Promise<ValidateResult>;
@@ -157,7 +132,7 @@ export type Assign<TValues extends object, U extends object> = TValues &
157132
Omit<U, keyof TValues>;
158133

159134
export type ControllerProps<
160-
TFieldValues extends Record<string, any>,
135+
TFieldValues extends FieldValues,
161136
TFieldName extends DeepPath<TFieldValues, TFieldName>,
162137
TAs extends 'input' | 'select' | 'textarea'
163138
> = Assign<

src/useTypedController.test.tsx

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import { render } from '@testing-library/react';
44
import { renderHook } from '@testing-library/react-hooks';
55
import { useTypedController } from './useTypedController';
66

7-
const reconfigureControl = (
7+
export const reconfigureControl = (
88
controlOverrides: Partial<Control> = {},
99
): Control => ({
10+
unmountFieldsStateRef: {
11+
current: {},
12+
},
1013
defaultValuesRef: {
1114
current: {},
1215
},
@@ -25,38 +28,42 @@ const reconfigureControl = (
2528
watchFieldsRef: {
2629
current: new Set(),
2730
},
28-
dirtyFieldsRef: {
29-
current: {},
30-
},
3131
watchFieldsHookRef: {
3232
current: {},
3333
},
3434
watchFieldsHookRenderRef: {
3535
current: {},
3636
},
3737
watchInternal: jest.fn(),
38-
validateSchemaIsValid: jest.fn(),
39-
reRender: jest.fn(),
38+
validateResolver: jest.fn(),
4039
setValue: jest.fn(),
40+
getValues: jest.fn(),
4141
register: jest.fn(),
4242
unregister: jest.fn(),
4343
trigger: jest.fn(),
4444
removeFieldEventListener: jest.fn(),
45-
errorsRef: { current: {} },
46-
touchedFieldsRef: { current: {} },
47-
mode: { isOnSubmit: false, isOnBlur: false, isOnChange: false },
45+
mode: {
46+
isOnSubmit: false,
47+
isOnBlur: false,
48+
isOnChange: false,
49+
isOnTouch: false,
50+
isOnAll: false,
51+
},
4852
reValidateMode: {
4953
isReValidateOnBlur: false,
50-
isReValidateOnSubmit: false,
54+
isReValidateOnChange: false,
5155
},
52-
formState: {
53-
isDirty: false,
54-
isSubmitted: false,
55-
dirtyFields: {},
56-
submitCount: 0,
57-
touched: {},
58-
isSubmitting: false,
59-
isValid: false,
56+
formStateRef: {
57+
current: {
58+
errors: {},
59+
isDirty: false,
60+
isSubmitted: false,
61+
dirtyFields: {},
62+
submitCount: 0,
63+
touched: {},
64+
isSubmitting: false,
65+
isValid: false,
66+
},
6067
},
6168
fieldsRef: {
6269
current: {},
@@ -67,15 +74,12 @@ const reconfigureControl = (
6774
fieldArrayNamesRef: {
6875
current: new Set<string>(),
6976
},
70-
isDirtyRef: {
71-
current: false,
72-
},
73-
isSubmittedRef: {
74-
current: false,
75-
},
77+
// eslint-disable-next-line @typescript-eslint/no-empty-function
78+
updateFormState: () => {},
7679
readFormStateRef: {
7780
current: {
7881
isDirty: true,
82+
errors: true,
7983
isSubmitted: false,
8084
submitCount: false,
8185
touched: false,
@@ -86,7 +90,6 @@ const reconfigureControl = (
8690
},
8791
// eslint-disable-next-line @typescript-eslint/no-empty-function
8892
renderWatchedInputs: () => {},
89-
unmountFieldsStateRef: { current: {} },
9093
...controlOverrides,
9194
});
9295

src/useTypedController.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import React from 'react';
2-
import { Controller, Control } from 'react-hook-form';
3-
import { formatName } from './logic/formatName';
42
import {
5-
DeepPath,
3+
Controller,
64
UnpackNestedValue,
75
FieldValuesFromControl,
8-
Options,
9-
ControllerProps,
10-
} from './types';
6+
Control,
7+
} from 'react-hook-form';
8+
import { formatName } from './logic/formatName';
9+
import { DeepPath, Options, ControllerProps } from './types';
1110

1211
export const useTypedController = <
1312
TFieldValues extends UnpackNestedValue<FieldValuesFromControl<TControl>>,

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6742,10 +6742,10 @@ react-dom@^16.13.1:
67426742
prop-types "^15.6.2"
67436743
scheduler "^0.19.1"
67446744

6745-
react-hook-form@^6.0.0-rc.5:
6746-
version "6.0.0-rc.5"
6747-
resolved "https://registry.yarnpkg.com/react-hook-form/-/react-hook-form-6.0.0-rc.5.tgz#f3965f66a3160fa37fcd44a88c54d8d7c65106d0"
6748-
integrity sha512-u+9po9D2gV6LjzCAdLsIHghG2mpE3+g49HZMWEl222NKqZ50b9g76f0rmw9jkBwRnS3cqxWcrmqlBD6YptPw5g==
6745+
react-hook-form@^6.7.0:
6746+
version "6.7.0"
6747+
resolved "https://registry.yarnpkg.com/react-hook-form/-/react-hook-form-6.7.0.tgz#84702b6a56c6c81d7c43cbfbd343e4884dd1e6be"
6748+
integrity sha512-2ySZ/lyp8xinKUMv/yoSo3NcNknfrHGlJ1pIV2U80H0WlFSGaGZ0wvAHmgfl1L5ZhoQND6mawcv09wuM8OGVPw==
67496749

67506750
react-is@^16.12.0, react-is@^16.8.1, react-is@^16.8.6:
67516751
version "16.13.1"

0 commit comments

Comments
 (0)