Skip to content

Commit

Permalink
Enforce strict string check on form values, now onChange function for…
Browse files Browse the repository at this point in the history
… fields only needs value, no need to supply name or id
  • Loading branch information
amitbadala committed Oct 26, 2023
1 parent 2023dce commit 7dc66b8
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 15 deletions.
45 changes: 38 additions & 7 deletions lib/build/emailpassword-shared7.js

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

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

17 changes: 15 additions & 2 deletions lib/ts/recipe/emailpassword/components/library/formBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,19 @@ 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 Expand Up @@ -219,7 +232,7 @@ export const FormBase: React.FC<FormBaseProps<any>> = (props) => {
autofocus={field.autofocus}
onInputFocus={onInputFocus}
onInputBlur={onInputBlur}
onChange={onInputChange}
onChange={(value) => onInputChange({ id: field.id, value: value })}
hasError={fstate.error !== undefined}
/>
) : (
Expand All @@ -232,7 +245,7 @@ export const FormBase: React.FC<FormBaseProps<any>> = (props) => {
autoComplete={field.autoComplete}
onInputFocus={onInputFocus}
onInputBlur={onInputBlur}
onChange={onInputChange}
onChange={(value) => onInputChange({ id: field.id, value: value })}
autofocus={field.autofocus}
hasError={fstate.error !== undefined}
/>
Expand Down
7 changes: 2 additions & 5 deletions lib/ts/recipe/emailpassword/components/library/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export type InputProps = {
value: string;
onInputBlur?: (field: APIFormField) => void;
onInputFocus?: (field: APIFormField) => void;
onChange?: (field: APIFormField) => void;
onChange?: (value: string) => void;
};

const Input: React.FC<InputProps> = ({
Expand Down Expand Up @@ -77,10 +77,7 @@ const Input: React.FC<InputProps> = ({

function handleChange(event: ChangeEvent<HTMLInputElement>) {
if (onChange) {
onChange({
id: name,
value: event.target.value,
});
onChange(event.target.value);
}
}

Expand Down

0 comments on commit 7dc66b8

Please sign in to comment.