Skip to content

Commit

Permalink
instead of form submit apply type test within onChange function itself
Browse files Browse the repository at this point in the history
  • Loading branch information
amitbadala committed Oct 26, 2023
1 parent 2be7cfa commit 2480ebd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 44 deletions.
40 changes: 9 additions & 31 deletions lib/build/emailpassword-shared7.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 8 additions & 13 deletions lib/ts/recipe/emailpassword/components/library/formBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ export const FormBase: React.FC<FormBaseProps<any>> = (props) => {

const onInputChange = useCallback(
(field: APIFormField) => {
try {
if (typeof field.value !== "string") {
throw new Error(`${field.id} value must be a string`);
}
} catch (error) {
console.error(error);
return props.onError("SOMETHING_WENT_WRONG_ERROR");
}
updateFieldState(field.id, (os) => ({ ...os, value: field.value, error: undefined }));
props.clearError();
},
Expand All @@ -145,19 +153,6 @@ export const FormBase: React.FC<FormBaseProps<any>> = (props) => {
};
});

// field.value must be a string
try {
const fieldsWithIncorrectValues = apiFields.filter((field) => typeof field.value !== "string");
if (fieldsWithIncorrectValues.length > 0) {
const errorFields = fieldsWithIncorrectValues.map(({ id }) => id).join(", ");
throw new Error(`${errorFields} value must be a string`);
}
} catch (error) {
console.error(error);
setIsLoading(false);
return props.onError("SOMETHING_WENT_WRONG_ERROR");
}

const fieldUpdates: FieldState[] = [];
// Call API.
try {
Expand Down

0 comments on commit 2480ebd

Please sign in to comment.