-
Notifications
You must be signed in to change notification settings - Fork 178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(js): update Formik to v2 #5190
Conversation
@@ -195,18 +196,19 @@ export class ConfigForm extends React.Component<ConfigFormProps> { | |||
initialValues={initialValues} | |||
validate={this.validate} | |||
validateOnChange={false} | |||
render={formProps => { | |||
> | |||
{(formProps: FormikProps<FormValues>) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just changed from render
prop to child fn
setValues: FormValues => mixed | ||
): mixed { | ||
setValues: FormValues => mixed, | ||
resetForm: ({| values: FormValues |}) => mixed, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the shape of the args for setValues
vs resetForm
Formik fns used to match, now in v2 they don't. So now I pass both fns into handleSecurityChange
and decide which to call inside here (instead of choosing which of the 2 fns to pass into handleSecurityChange
from outside)
Codecov Report
@@ Coverage Diff @@
## edge #5190 +/- ##
==========================================
+ Coverage 60.29% 60.43% +0.13%
==========================================
Files 1025 1025
Lines 29029 29230 +201
==========================================
+ Hits 17502 17664 +162
- Misses 11527 11566 +39
Continue to review full report at Codecov.
|
This is looking good to me so far, and would be quite helpful for the rewrite of ConnectForm we're trying to land as part of the Wi-Fi disconnect push. Would be great if we could validate and get this in asap |
const handleReset = () => { | ||
const newValues = mapValues(values, v => { | ||
if (typeof v === 'boolean') { | ||
// NOTE: checkbox fields don't have defaults from the API b/c they come in from `quirks` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this isn't a change, I just added a note b/c I got confused about what was intended here 🙃
@@ -107,13 +109,13 @@ export function ConfigCheckbox(props: ConfigCheckboxProps) { | |||
const id = makeId(name) | |||
return ( | |||
<ConfigFormRow label={displayName} labelFor={id}> | |||
<Field name={name}> | |||
<Field name={name} type="checkbox"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type="checkbox"
is required for fieldProps.field.checked
to work
components/src/forms/SelectField.js
Outdated
@@ -70,7 +83,7 @@ export function SelectField(props: SelectFieldProps) { | |||
menuPosition={menuPosition} | |||
formatOptionLabel={formatOptionLabel} | |||
onChange={opt => onValueChange && onValueChange(name, opt?.value || '')} | |||
onBlur={() => onLoseFocus && onLoseFocus(name)} | |||
onBlur={handleOnBlur} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mcous do you know why we didn't do something like this earlier? Formik needs that event to handle onBlur
, otherwise it will throw an error at runtime (eg in labware-library/src/labware-creator/components/Dropdown.js
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SelectField
is an old component, and was always designed to use setFieldValue
and setFieldTouched
rather than onChange
and onBlur
(see SelectField.md
). The reasons behind this were:
onBlur
is attached to the<input type="text">
that<ReactSelect>
sometimes (maybe? It's pretty hard to tell from the react-select docs so this is a combo of code reading and docs inference) renders depending on what props you pass inonChange
in<ReactSelect>
is not "standard", so it felt weird to have a "vanila" onBlur but a custom onChange
Accordingly, it looks lile labware-library/src/labware-creator/components/Dropdown.js
was written incorrectly for how SelectField
is designed to be used:
// is now
onLoseFocus={() => {
reportFieldEdit({ value: field.value, name: field.name })
field.onBlur()
}}
// "should" be
onLoseFocus={name => {
reportFieldEdit({ value: field.value, name })
form.setFieldTouched(name)
}}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wi-Fi connect and pipette settings forms in the app look like they're behaving
- Responded to why
SelectField
doesn't have anonBlur
prop. If you feel strongly about it, we can add it, but it looks like the stuff in LC that was usingSelectField
was actually not using itsonLoseFocus
prop correctly asdsafsd: *
is deprecated and should be avoiding in favor of simply omitting the type annotation- Premature
useMemo
components/src/forms/SelectField.js
Outdated
@@ -70,7 +83,7 @@ export function SelectField(props: SelectFieldProps) { | |||
menuPosition={menuPosition} | |||
formatOptionLabel={formatOptionLabel} | |||
onChange={opt => onValueChange && onValueChange(name, opt?.value || '')} | |||
onBlur={() => onLoseFocus && onLoseFocus(name)} | |||
onBlur={handleOnBlur} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SelectField
is an old component, and was always designed to use setFieldValue
and setFieldTouched
rather than onChange
and onBlur
(see SelectField.md
). The reasons behind this were:
onBlur
is attached to the<input type="text">
that<ReactSelect>
sometimes (maybe? It's pretty hard to tell from the react-select docs so this is a combo of code reading and docs inference) renders depending on what props you pass inonChange
in<ReactSelect>
is not "standard", so it felt weird to have a "vanila" onBlur but a custom onChange
Accordingly, it looks lile labware-library/src/labware-creator/components/Dropdown.js
was written incorrectly for how SelectField
is designed to be used:
// is now
onLoseFocus={() => {
reportFieldEdit({ value: field.value, name: field.name })
field.onBlur()
}}
// "should" be
onLoseFocus={name => {
reportFieldEdit({ value: field.value, name })
form.setFieldTouched(name)
}}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🍴
overview
Serves as its own ticket
We use Formik in several projects and it had a major v1 -> v2 release in October. The breaking changes are minimal: https://jaredpalmer.com/formik/docs/migrating-v2
What do we get? Some nice stuff
For the migration to v2, I found 2 changes that affected us:
resetForm
args changed. This PR changesresetForm(stuff)
toresetForm({values: stuff})
render
prop, this is deprecated. In this PR, I used a child function instead. (Unfortunately they still don't provide a hook interface, you need to use<Formik>
orwithFormik()
. Maybe you could do<Formik><MyFormComponent /></Formik>
and inMyFormComponent
you could useuseFormikContext
hook? See docs)changelog
review requests
Run App
app/src/components/RobotSettings/SelectNetwork/ConnectForm.js
app/src/components/AppSettings/AddManualIp/ManualIpForm.js
app/src/components/ConfigurePipette/ConfigForm.js
Labware Creator
It's just a big Formik form!
Protocol Designer