question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[v2] Any way to use a custom Formik Field?

See original GitHub issue

We wrap the Formik Field with our own to provide additional functionality:

  1. We have a <FieldSet/> component that we use with nested fields so you can do:
<FieldSet name='a.b.c.d'>
  <Field name='z' />
</FieldSet>
  1. We have format and normalize props for our custom <Field/> that converts data from/to API calls like <Field format={timeToDate} normalize={dateToTime} .../>

Any idea on if we can still pull this off with Formik-material-ui v2 since you no longer support using <Field>?

Here is what our custom Field looks like:

import { FastField as FormikField, isInputEvent } from 'formik';
import * as PropTypes from 'prop-types';
import React from 'react';
import { FieldSetConsumer, formatFieldName } from './FieldSet';

const Field = ({ component: Component, name, format, normalize, useContext, onChange, validate, ...otherProps }) => (
  <FieldSetConsumer>
    {context => {
      const fieldName = formatFieldName(useContext ? context : '', name);
      return (
        <FormikField name={fieldName} normalize={normalize} validate={validate}>
          {({ field, form }) => {
            const { value } = field;

            const handleChange = eventOrValue => {
              const event = isInputEvent(eventOrValue);
              let newValue;
              if (event) {
                newValue =
                  eventOrValue.target.type === 'checkbox' ? eventOrValue.target.checked : eventOrValue.target.value;
              } else {
                newValue = eventOrValue;
              }
              newValue = normalize ? normalize(newValue) : newValue;
              const oldValue = value;

              field.onChange(eventOrValue);
              form.setFieldValue(fieldName, newValue);
              if (onChange) {
                onChange({ event: eventOrValue, field, form, newValue, oldValue });
              }
            };

            const { children, ...props } = otherProps;
            return (
              <Component
                field={{
                  ...field,
                  onChange: handleChange,
                  value: format ? format(value) : value
                }}
                form={form}
                {...props}
              >
                {typeof children === 'function' ? children({ field, form }) : children}
              </Component>
            );
          }}
        </FormikField>
      );
    }}
  </FieldSetConsumer>
);

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11

github_iconTop GitHub Comments

1reaction
mycroescommented, Feb 3, 2020

Can’t you just replace Field with TextField (import {TextField} from 'formik-material-ui';)? From what I saw in the docs that’s how it’s supposed to work now…

0reactions
cliedemancommented, Feb 17, 2020

@MikeSuiter that is an oversight. It should be possible to override onChange.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[v2] Any way to use a custom Formik Field? · Issue #129 - GitHub
My custom Formik FastField works with all the standard Formik-MUI fields but the format/normalize doesn't work with Formik-MUI pickers. I ...
Read more >
<Field /> | Formik
Custom React components will be passed onChange , onBlur , name , and value plus any other props passed to directly to <Field>...
Read more >
How to use custom Input with Formik in React? - Stack Overflow
You can use e.target.value on setFieldValue's second prop, depends on your custom input design. Thanks to Brandon. Share.
Read more >
Managing nested forms gracefully with Formik - Rajesh Naroth
With the same concept as a custom field, we'll create forms that are essentially Formik fields themselves. The secret lies in the setFieldValue ......
Read more >
Using Formik to Handle Forms in React - CSS-Tricks
withFormik is a higher-order component and be used that way if that's your thing. Write the form, then expose it through Formik. A...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found