Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AddressNL validation triggers #789

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 37 additions & 17 deletions src/formio/components/AddressNL.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
*/
import {Formik, useFormikContext} from 'formik';
import debounce from 'lodash/debounce';
import {useContext, useEffect} from 'react';
import {useContext, useEffect, useState} from 'react';
import {createRoot} from 'react-dom/client';
import {Formio} from 'react-formio';
import {FormattedMessage, IntlProvider, defineMessages, useIntl} from 'react-intl';
import {z} from 'zod';
import {toFormikValidationSchema} from 'zod-formik-adapter';
import {toFormikValidate} from 'zod-formik-adapter';

import {ConfigContext} from 'Context';
import {get} from 'api';
Expand Down Expand Up @@ -299,6 +299,7 @@

const AddressNLForm = ({initialValues, required, deriveAddress, layout, setFormioValues}) => {
const intl = useIntl();
const [dirty, setDirty] = useState(false);

Check warning on line 302 in src/formio/components/AddressNL.jsx

View check run for this annotation

Codecov / codecov/patch

src/formio/components/AddressNL.jsx#L302

Added line #L302 was not covered by tests

const {
component: {
Expand Down Expand Up @@ -338,6 +339,13 @@
return {message: ctx.defaultError}; // use global schema as fallback
};

const handleFormikAddressDirtyChange = newDirtyState => {

Check warning on line 342 in src/formio/components/AddressNL.jsx

View check run for this annotation

Codecov / codecov/patch

src/formio/components/AddressNL.jsx#L342

Added line #L342 was not covered by tests
// Only set the dirty state from `false` to `true`. Once it's dirty, it will remain dirty.
if (!dirty && newDirtyState) {
setDirty(newDirtyState);

Check warning on line 345 in src/formio/components/AddressNL.jsx

View check run for this annotation

Codecov / codecov/patch

src/formio/components/AddressNL.jsx#L345

Added line #L345 was not covered by tests
}
};

return (
<Formik
initialValues={initialValues}
Expand All @@ -346,35 +354,47 @@
houseNumber: true,
city: true,
}}
validationSchema={toFormikValidationSchema(
addressNLSchema(required, intl, {
postcode: {
pattern: postcodePattern,
errorMessage: postcodeError,
},
city: {
pattern: cityPattern,
errorMessage: cityError,
},
}),
{errorMap}
)}
validateOnChange={false}
validate={values =>

Check warning on line 358 in src/formio/components/AddressNL.jsx

View check run for this annotation

Codecov / codecov/patch

src/formio/components/AddressNL.jsx#L358

Added line #L358 was not covered by tests
dirty
? toFormikValidate(
addressNLSchema(required, intl, {
postcode: {
pattern: postcodePattern,
errorMessage: postcodeError,
},
city: {
pattern: cityPattern,
errorMessage: cityError,
},
}),
{errorMap}
)(values)
: {}
}
>
<FormikAddress
required={required}
setFormioValues={setFormioValues}
deriveAddress={deriveAddress}
layout={layout}
setDirty={handleFormikAddressDirtyChange}
/>
</Formik>
);
};

const FormikAddress = ({required, setFormioValues, deriveAddress, layout}) => {
const {values, isValid, setFieldValue} = useFormikContext();
const FormikAddress = ({required, setFormioValues, deriveAddress, layout, setDirty}) => {
const {values, isValid, setFieldValue, dirty} = useFormikContext();

Check warning on line 388 in src/formio/components/AddressNL.jsx

View check run for this annotation

Codecov / codecov/patch

src/formio/components/AddressNL.jsx#L388

Added line #L388 was not covered by tests
const {baseUrl} = useContext(ConfigContext);
const useColumns = layout === 'doubleColumn';

useEffect(() => {

Check warning on line 392 in src/formio/components/AddressNL.jsx

View check run for this annotation

Codecov / codecov/patch

src/formio/components/AddressNL.jsx#L392

Added line #L392 was not covered by tests
if (dirty) {
setDirty(dirty);

Check warning on line 394 in src/formio/components/AddressNL.jsx

View check run for this annotation

Codecov / codecov/patch

src/formio/components/AddressNL.jsx#L394

Added line #L394 was not covered by tests
}
}, [dirty, setDirty]);

useEffect(() => {
// *always* synchronize the state up, since:
//
Expand Down
Loading