Skip to content

Commit f8dc76e

Browse files
committed
close #69
close #70
1 parent 389f8cb commit f8dc76e

File tree

5 files changed

+222
-99
lines changed

5 files changed

+222
-99
lines changed

app/src/App.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@ const App = () => {
88
firstName: string;
99
lastName: string;
1010
custom: string;
11+
ha: {
12+
test: string;
13+
};
1114
}>({
1215
mode: 'onChange',
1316
defaultValues: {
1417
firstName: '',
1518
lastName: '',
19+
ha: {
20+
test: '',
21+
},
1622
},
1723
});
1824

@@ -34,6 +40,7 @@ const App = () => {
3440
</p>
3541
<label>First Name</label>
3642
<input {...register('firstName', { required: true })} />
43+
<input {...register('ha.test', { required: true })} />
3744

3845
<label>Last Name</label>
3946
<input {...register('lastName', { required: true })} />

app/src/devTool/panel.tsx

Lines changed: 104 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,99 @@ import FormStateTable from './formStateTable';
88
import { Button, Input } from './styled';
99
import { setCollapse } from './settingAction';
1010

11-
export default ({ control, control: { fieldsRef } }: { control: Control }) => {
11+
let childIndex = 0;
12+
13+
function PanelChildren<T, K, L, M, G>({
14+
fields,
15+
searchTerm,
16+
touchedFields,
17+
errors,
18+
dirtyFields,
19+
state,
20+
fieldsValues,
21+
}: {
22+
fields: T;
23+
fieldsValues: K;
24+
state: {
25+
isCollapse: boolean;
26+
};
27+
searchTerm: string;
28+
touchedFields: M;
29+
errors: L;
30+
dirtyFields: G;
31+
}) {
32+
return (
33+
<>
34+
{Object.entries(fields)
35+
.filter(
36+
([name]) =>
37+
((name &&
38+
name.toLowerCase &&
39+
name.toLowerCase().includes(searchTerm)) ||
40+
(!name && !searchTerm) ||
41+
searchTerm === '') &&
42+
name,
43+
)
44+
.map(([name, value], index) => {
45+
childIndex++;
46+
47+
if (!value._f) {
48+
return (
49+
<PanelChildren
50+
key={name + childIndex}
51+
{...{
52+
fields: value,
53+
searchTerm,
54+
touchedFields,
55+
errors,
56+
dirtyFields,
57+
state,
58+
fieldsValues,
59+
}}
60+
/>
61+
);
62+
} else {
63+
const error = get(errors, value._f.name);
64+
const errorMessage = get(error, 'message', undefined);
65+
const errorType = get(error, 'type', undefined);
66+
const type = get(value, 'ref.type', undefined);
67+
const isTouched = !!get(touchedFields, name);
68+
const isNative = !!(value && value._f.ref.type);
69+
const isDirty = !!get(dirtyFields, name);
70+
const hasError = !!error;
71+
const ref = get(value, '_f.ref');
72+
73+
return (
74+
<section
75+
key={value?._f.name + childIndex}
76+
style={{
77+
borderBottom: `1px dashed ${colors.secondary}`,
78+
margin: 0,
79+
}}
80+
>
81+
<PanelTable
82+
refObject={ref}
83+
index={index}
84+
collapseAll={state.isCollapse}
85+
name={value?._f.name}
86+
isTouched={isTouched}
87+
type={type}
88+
hasError={hasError}
89+
isNative={isNative}
90+
errorMessage={errorMessage}
91+
errorType={errorType}
92+
isDirty={isDirty}
93+
fieldsValues={fieldsValues}
94+
/>
95+
</section>
96+
);
97+
}
98+
})}
99+
</>
100+
);
101+
}
102+
103+
const Panel = ({ control, control: { fieldsRef } }: { control: Control }) => {
12104
const formState = useFormState({
13105
control,
14106
});
@@ -98,52 +190,15 @@ export default ({ control, control: { fieldsRef } }: { control: Control }) => {
98190
overflow: 'auto',
99191
}}
100192
>
101-
{Object.entries(fieldsRef.current)
102-
.filter(
103-
([name]) =>
104-
((name &&
105-
name.toLowerCase &&
106-
name.toLowerCase().includes(searchTerm)) ||
107-
(!name && !searchTerm) ||
108-
searchTerm === '') &&
109-
name,
110-
)
111-
.map(([name, value], index) => {
112-
const error = get(errors, name);
113-
const errorMessage = get(error, 'message', undefined);
114-
const errorType = get(error, 'type', undefined);
115-
const type = get(value, 'ref.type', undefined);
116-
const isTouched = !!get(touchedFields, name);
117-
const isNative = !!(value && value._f.ref.type);
118-
const isDirty = !!get(dirtyFields, name);
119-
const hasError = !!error;
120-
const ref = get(value, '_f.ref');
121-
122-
return (
123-
<section
124-
key={`${name}${index}`}
125-
style={{
126-
borderBottom: `1px dashed ${colors.secondary}`,
127-
margin: 0,
128-
}}
129-
>
130-
<PanelTable
131-
refObject={ref}
132-
index={index}
133-
collapseAll={state.isCollapse}
134-
name={name}
135-
isTouched={isTouched}
136-
type={type}
137-
hasError={hasError}
138-
isNative={isNative}
139-
errorMessage={errorMessage}
140-
errorType={errorType}
141-
isDirty={isDirty}
142-
fieldsValues={fieldsValues}
143-
/>
144-
</section>
145-
);
146-
})}
193+
<PanelChildren
194+
fields={fieldsRef.current}
195+
searchTerm={searchTerm}
196+
errors={errors}
197+
touchedFields={touchedFields}
198+
dirtyFields={dirtyFields}
199+
fieldsValues={fieldsValues}
200+
state={state}
201+
/>
147202
</div>
148203

149204
<FormStateTable
@@ -154,3 +209,5 @@ export default ({ control, control: { fieldsRef } }: { control: Control }) => {
154209
</div>
155210
);
156211
};
212+
213+
export default Panel;

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
"prettier": "^2.0.5",
8585
"react": "^16.13.1",
8686
"react-dom": "^16.13.1",
87-
"react-hook-form": "^7.0.0-beta.2",
87+
"react-hook-form": "7.0.0-rc.0",
8888
"rimraf": "^3.0.2",
8989
"rollup": "^2.10.7",
9090
"rollup-plugin-peer-deps-external": "^2.2.2",
@@ -97,7 +97,7 @@
9797
"peerDependencies": {
9898
"react": ">=16.8.0",
9999
"react-dom": ">=16.8.0",
100-
"react-hook-form": ">=6.5.0"
100+
"react-hook-form": "^7.0.0-rc.0"
101101
},
102102
"husky": {
103103
"hooks": {

src/devTool.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22
import { StateMachineProvider, createStore } from 'little-state-machine';
3-
import { Control } from 'react-hook-form';
3+
import { Control, useFormContext } from 'react-hook-form';
44
import { DevToolUI } from './devToolUI';
55

66
if (typeof window !== 'undefined') {
@@ -18,10 +18,12 @@ if (typeof window !== 'undefined') {
1818
);
1919
}
2020

21-
export const DevTool = ({ control }: { control: Control<any> }) => {
21+
export const DevTool = (props?: { control: Control<any> }) => {
22+
const methods = useFormContext();
23+
2224
return (
2325
<StateMachineProvider>
24-
<DevToolUI control={control} />
26+
<DevToolUI control={methods.control || (props && props.control)} />
2527
</StateMachineProvider>
2628
);
2729
};

0 commit comments

Comments
 (0)