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' ...@@ -4,7 +4,13 @@ import { Field } from 'formik'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import styled from 'styled-components' import styled from 'styled-components'
import { th } from '@pubsweet/ui-toolkit' import { th } from '@pubsweet/ui-toolkit'
import { compose, withHandlers, withProps } from 'recompose' import {
compose,
withProps,
withHandlers,
shouldUpdate,
setDisplayName,
} from 'recompose'
const WrappedComponent = compose( const WrappedComponent = compose(
withHandlers({ withHandlers({
...@@ -23,6 +29,11 @@ const WrappedComponent = compose( ...@@ -23,6 +29,11 @@ const WrappedComponent = compose(
error: hasError && get(errors, name), error: hasError && get(errors, name),
validationStatus: hasError ? 'error' : 'default', validationStatus: hasError ? 'error' : 'default',
})), })),
shouldUpdate(
(prev, next) =>
get(prev, 'field.value') !== get(next, 'field.value') ||
get(prev, 'error') !== get(next, 'error'),
),
)( )(
({ ({
error, error,
...@@ -70,10 +81,14 @@ ValidatedFormField.propTypes = { ...@@ -70,10 +81,14 @@ ValidatedFormField.propTypes = {
]).isRequired, ]).isRequired,
} }
export default withProps(({ validate }) => ({ export default compose(
validateFn: value => setDisplayName('ValidatedFormikField'),
validate.filter(fn => fn(value)).reduce((acc, fn) => fn(value), ''), withProps(({ validate = [] }) => ({
}))(ValidatedFormField) validateFn: (value = '') =>
validate.reduce((acc, fn) => acc || fn(value), ''),
})),
shouldUpdate(() => false),
)(ValidatedFormField)
// #region styles // #region styles
const MessageWrapper = styled.div` const MessageWrapper = styled.div`
......
...@@ -11,13 +11,14 @@ import { ...@@ -11,13 +11,14 @@ import {
Text, Text,
Label, Label,
IconButton, IconButton,
MenuCountry,
RowOverrideAlert, RowOverrideAlert,
ItemOverrideAlert, ItemOverrideAlert,
ValidatedFormField, ValidatedFormField,
withRoles, withRoles,
withFetching, withFetching,
withCountries, withCountries,
MenuCountry, validators,
} from 'pubsweet-component-faraday-ui' } from 'pubsweet-component-faraday-ui'
// #region helpers // #region helpers
...@@ -87,7 +88,7 @@ const FormModal = ({ ...@@ -87,7 +88,7 @@ const FormModal = ({
component={TextField} component={TextField}
inline inline
name="email" name="email"
validate={[min3, max6]} validate={[validators.emailValidator]}
/> />
</ItemOverrideAlert> </ItemOverrideAlert>
...@@ -195,10 +196,9 @@ export default compose( ...@@ -195,10 +196,9 @@ export default compose(
})), })),
withHandlers({ withHandlers({
onSubmit: ({ onSubmit, ...props }) => (values, formProps) => { onSubmit: ({ onSubmit, ...props }) => (values, formProps) => {
console.log('the values', values) if (typeof onSubmit === 'function') {
// if (typeof onSubmit === 'function') { onSubmit(values, { ...formProps, ...props })
// onSubmit(values, { ...formProps, ...props }) }
// }
}, },
onClose: ({ onCancel, ...props }) => () => { onClose: ({ onCancel, ...props }) => () => {
if (typeof onCancel === 'function') { 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