@e-group/material-formik
Version:
Custom react components for integrate redux-form.
29 lines (27 loc) • 997 B
JavaScript
import * as React from 'react';
import { getIn } from 'formik';
export default function useFieldStatus(field, form, disabledProp) {
const isSubmitting = form.isSubmitting,
touchedProp = form.touched,
errors = form.errors,
submitCount = form.submitCount,
setFieldTouched = form.setFieldTouched;
const fieldError = getIn(errors, field.name);
const touched = getIn(touchedProp, field.name);
const showError = touched && !!fieldError;
const disabled = disabledProp !== null && disabledProp !== void 0 ? disabledProp : isSubmitting; // Equals to field.value !== undefined && field.value !== null
const hasValue = !(field.value == null); // Need this to solve touched issue.
// https://github.com/formium/formik/issues/445
React.useEffect(() => {
if (submitCount > 0) {
setFieldTouched(field.name);
}
}, [field.name, setFieldTouched, submitCount]);
return {
touched,
fieldError,
showError,
disabled,
hasValue
};
}