Skip to content
Snippets Groups Projects
Commit 6df41016 authored by Alexandru Munteanu's avatar Alexandru Munteanu
Browse files

feat(adminUserForm): add validate array to each field and fix wasteful rerenders

parent c52341fa
No related branches found
No related tags found
3 merge requests!233S26 updates,!230S26 Updates,!223Admin formik
......@@ -4,7 +4,13 @@ import { Field } from 'formik'
import PropTypes from 'prop-types'
import styled from 'styled-components'
import { th } from '@pubsweet/ui-toolkit'
import { compose, withHandlers, withProps } from 'recompose'
import {
compose,
withProps,
withHandlers,
shouldUpdate,
setDisplayName,
} from 'recompose'
const WrappedComponent = compose(
withHandlers({
......@@ -23,6 +29,11 @@ const WrappedComponent = compose(
error: hasError && get(errors, name),
validationStatus: hasError ? 'error' : 'default',
})),
shouldUpdate(
(prev, next) =>
get(prev, 'field.value') !== get(next, 'field.value') ||
get(prev, 'error') !== get(next, 'error'),
),
)(
({
error,
......@@ -70,10 +81,14 @@ ValidatedFormField.propTypes = {
]).isRequired,
}
export default withProps(({ validate }) => ({
validateFn: value =>
validate.filter(fn => fn(value)).reduce((acc, fn) => fn(value), ''),
}))(ValidatedFormField)
export default compose(
setDisplayName('ValidatedFormikField'),
withProps(({ validate = [] }) => ({
validateFn: (value = '') =>
validate.reduce((acc, fn) => acc || fn(value), ''),
})),
shouldUpdate(() => false),
)(ValidatedFormField)
// #region styles
const MessageWrapper = styled.div`
......
......@@ -11,13 +11,14 @@ import {
Text,
Label,
IconButton,
MenuCountry,
RowOverrideAlert,
ItemOverrideAlert,
ValidatedFormField,
withRoles,
withFetching,
withCountries,
MenuCountry,
validators,
} from 'pubsweet-component-faraday-ui'
// #region helpers
......@@ -87,7 +88,7 @@ const FormModal = ({
component={TextField}
inline
name="email"
validate={[min3, max6]}
validate={[validators.emailValidator]}
/>
</ItemOverrideAlert>
......@@ -195,10 +196,9 @@ export default compose(
})),
withHandlers({
onSubmit: ({ onSubmit, ...props }) => (values, formProps) => {
console.log('the values', values)
// if (typeof onSubmit === 'function') {
// onSubmit(values, { ...formProps, ...props })
// }
if (typeof onSubmit === 'function') {
onSubmit(values, { ...formProps, ...props })
}
},
onClose: ({ onCancel, ...props }) => () => {
if (typeof onCancel === 'function') {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment