[v2] Any way to use a custom Formik Field?
See original GitHub issueWe wrap the Formik Field with our own to provide additional functionality:
- 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>
- 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:
- Created 4 years ago
- Comments:11
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Can’t you just replace
Field
withTextField
(import {TextField} from 'formik-material-ui';
)? From what I saw in the docs that’s how it’s supposed to work now…@MikeSuiter that is an oversight. It should be possible to override onChange.