Skip to content

Commit

Permalink
fix(useForm): update form handling for missing configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
aprendendofelipe committed Nov 7, 2024
1 parent 566a28f commit 1f3ce99
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/form/components/Checkout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function Checkout({ fields, product, store }) {

<FormField {...getFieldProps('fullName')} />
<ResponsiveFormRow>
<FormField {...getFieldProps('email')} sx={{ flex: 1, minWidth: '240px' }} />
<FormField {...getFieldProps('emailConfirmable')} sx={{ flex: 1, minWidth: '240px' }} />
<FormField {...getFieldProps('emailConfirmation')} sx={{ flex: 1, minWidth: '240px' }} />
</ResponsiveFormRow>
<ResponsiveFormRow>
Expand Down
2 changes: 1 addition & 1 deletion examples/form/form-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const installmentField = {

export const checkoutFields = {
fullName,
email,
emailConfirmable,
emailConfirmation,
document: brDocs,
phone,
Expand Down
13 changes: 11 additions & 2 deletions packages/forms/src/useForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ export function useForm(initialConfig, { submitDisabled = true, submitHidden = f
const errors = {};
let hasErrors = false;

for (const [fieldName, fieldState] of Object.entries(state)) {
const { validateOnBlurAndSubmit, prepare } = processors[fieldName];
for (const [fieldName, processor] of Object.entries(processors)) {
const { validateOnBlurAndSubmit, prepare } = processor;
const fieldState = state[fieldName];

if (!submitDisabled && fieldState.disabled === true) {
continue;
Expand Down Expand Up @@ -61,6 +62,14 @@ export function useForm(initialConfig, { submitDisabled = true, submitHidden = f

const getFieldProps = useCallback(
(name) => {
if (!processors[name]) {
if (process.env.NODE_ENV !== 'production') {
throw new Error(`Field "${name}" not found in config.`);
} else {
return { hidden: true };
}
}

const fieldState = state[name];
const { format, prepare, onValidChange, validateOnBlurAndSubmit, validateOnChange } = processors[name];
let onBlurError = null;
Expand Down
17 changes: 17 additions & 0 deletions packages/forms/src/useForm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ describe('useForm', () => {
const empty = renderHook(() => useForm({})).result.current;
expect(empty).toStrictEqual(expectedResult);
});

describe('Missing configuration', () => {
it('should throw new Error("Field not found in config.")', () => {
const { result } = renderHook(() => useForm(initialConfig));
expect(() => result.current.getFieldProps('notFound')).toThrowError('Field "notFound" not found in config.');
});

it('should return hidden field', () => {
const originalEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'production';
const { result } = renderHook(() => useForm(initialConfig));
const hiddenField = result.current.getFieldProps('notFound');
process.env.NODE_ENV = originalEnv;

expect(hiddenField).toStrictEqual({ hidden: true });
});
});
});

describe('update state', () => {
Expand Down

0 comments on commit 1f3ce99

Please sign in to comment.