@@ -6,6 +6,7 @@ import forOwn from 'lodash/forOwn'
6
6
import keys from 'lodash/keys'
7
7
import omit from 'lodash/omit'
8
8
import set from 'lodash/set'
9
+ import { useForm } from 'react-hook-form'
9
10
import { Box , OVERFLOW_AUTO } from '@opentrons/components'
10
11
import { ConfigFormResetButton } from './ConfigFormResetButton'
11
12
import {
@@ -14,13 +15,13 @@ import {
14
15
ConfigQuirkGroup ,
15
16
} from './ConfigFormGroup'
16
17
18
+ import type { FieldError , Resolver } from 'react-hook-form'
17
19
import type { FormValues } from './ConfigFormGroup'
18
20
import type {
19
21
PipetteSettingsField ,
20
22
PipetteSettingsFieldsMap ,
21
23
UpdatePipetteSettingsData ,
22
24
} from '@opentrons/api-client'
23
- import { FieldError , Resolver , useForm } from 'react-hook-form'
24
25
25
26
export interface DisplayFieldProps extends PipetteSettingsField {
26
27
name : string
@@ -46,7 +47,7 @@ const POWER_KEYS = ['plungerCurrent', 'pickUpCurrent', 'dropTipCurrent']
46
47
const TIP_KEYS = [ 'dropTipSpeed' , 'pickUpDistance' ]
47
48
const QUIRK_KEY = 'quirks'
48
49
49
- export const ConfigForm = ( props : ConfigFormProps ) : JSX . Element => {
50
+ export function ConfigForm ( props : ConfigFormProps ) : JSX . Element {
50
51
const {
51
52
updateInProgress,
52
53
formId,
@@ -55,6 +56,44 @@ export const ConfigForm = (props: ConfigFormProps): JSX.Element => {
55
56
groupLabels,
56
57
} = props
57
58
59
+ const getInitialValues : ( ) => FormValues = ( ) => {
60
+ const fields = getVisibleFields ( )
61
+ const initialFieldValues = mapValues <
62
+ PipetteSettingsFieldsMap ,
63
+ string | boolean
64
+ > ( fields , f => {
65
+ if ( f . value === true || f . value === false ) return f . value
66
+ // @ts -expect-error(sa, 2021-05-27): avoiding src code change, use optional chain to access f.value
67
+ return f . value !== f . default ? f . value . toString ( ) : ''
68
+ } )
69
+ const initialQuirkValues = settings [ QUIRK_KEY ]
70
+ const initialValues = Object . assign (
71
+ { } ,
72
+ initialFieldValues ,
73
+ initialQuirkValues
74
+ )
75
+
76
+ return initialValues
77
+ }
78
+ const initialValues = getInitialValues ( )
79
+
80
+ const resolver : Resolver < FormValues > = values => {
81
+ let errors = { }
82
+ errors = validate ( values , errors )
83
+ return { values, errors }
84
+ }
85
+
86
+ const {
87
+ handleSubmit,
88
+ reset,
89
+ getValues,
90
+ control,
91
+ formState : { errors } ,
92
+ } = useForm < FormValues > ( {
93
+ defaultValues : initialValues ,
94
+ resolver : resolver ,
95
+ } )
96
+
58
97
const getFieldsByKey = (
59
98
keys : string [ ] ,
60
99
fields : PipetteSettingsFieldsMap
@@ -166,32 +205,6 @@ export const ConfigForm = (props: ConfigFormProps): JSX.Element => {
166
205
return errors
167
206
}
168
207
169
- const resolver : Resolver < FormValues > = values => {
170
- let errors = { }
171
- errors = validate ( values , errors )
172
- return { values, errors }
173
- }
174
-
175
- const getInitialValues : ( ) => FormValues = ( ) => {
176
- const fields = getVisibleFields ( )
177
- const initialFieldValues = mapValues <
178
- PipetteSettingsFieldsMap ,
179
- string | boolean
180
- > ( fields , f => {
181
- if ( f . value === true || f . value === false ) return f . value
182
- // @ts -expect-error(sa, 2021-05-27): avoiding src code change, use optional chain to access f.value
183
- return f . value !== f . default ? f . value . toString ( ) : ''
184
- } )
185
- const initialQuirkValues = settings [ QUIRK_KEY ]
186
- const initialValues = Object . assign (
187
- { } ,
188
- initialFieldValues ,
189
- initialQuirkValues
190
- )
191
-
192
- return initialValues
193
- }
194
-
195
208
const fields = getVisibleFields ( )
196
209
const UNKNOWN_KEYS = getUnknownKeys ( )
197
210
const plungerFields = getFieldsByKey ( PLUNGER_KEYS , fields )
@@ -200,18 +213,6 @@ export const ConfigForm = (props: ConfigFormProps): JSX.Element => {
200
213
const quirkFields = getKnownQuirks ( )
201
214
const quirksPresent = quirkFields . length > 0
202
215
const unknownFields = getFieldsByKey ( UNKNOWN_KEYS , fields )
203
- const initialValues = getInitialValues ( )
204
-
205
- const {
206
- handleSubmit,
207
- reset,
208
- getValues,
209
- control,
210
- formState : { errors } ,
211
- } = useForm < FormValues > ( {
212
- defaultValues : initialValues ,
213
- resolver : resolver ,
214
- } )
215
216
216
217
const handleReset = ( ) : void => {
217
218
const newValues = mapValues ( getValues ( ) , v => {
0 commit comments