Skip to content

Commit

Permalink
Handle if getDefaultValue is not a function
Browse files Browse the repository at this point in the history
  • Loading branch information
amitbadala committed Oct 26, 2023
1 parent a13ddc1 commit 2be7cfa
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 21 deletions.
39 changes: 28 additions & 11 deletions lib/build/emailpassword-shared7.js

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

38 changes: 28 additions & 10 deletions lib/ts/recipe/emailpassword/components/library/formBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ type FieldState = {
value: string;
};

const fetchDefaultValue = (field: FormFieldThemeProps): string => {
if (field && field.getDefaultValue) {
try {
if (typeof field.getDefaultValue !== "function") {
throw new Error(`getDefaultValue for ${field.id} must be a function`);
}
const defaultValue = field.getDefaultValue();
if (typeof defaultValue !== "string") {
throw new Error(`getDefaultValue for ${field.id} must return a string`);
} else {
return defaultValue;
}
} catch (error) {
console.error(error);
}
}
return "";
};

export const FormBase: React.FC<FormBaseProps<any>> = (props) => {
const { footer, buttonLabel, showLabels, validateOnBlur, formFields } = props;

Expand All @@ -49,18 +68,17 @@ export const FormBase: React.FC<FormBaseProps<any>> = (props) => {
};
}, [unmounting]);

const fetchDefaultValue = (field: FormFieldThemeProps) => {
if (field && field.getDefaultValue) {
return field.getDefaultValue();
}
return "";
};

const [fieldStates, setFieldStates] = useState<FieldState[]>(
props.formFields.map((f) => ({ id: f.id, value: fetchDefaultValue(f) }))
);
const [fieldStates, setFieldStates] = useState<FieldState[]>([]);
const [isLoading, setIsLoading] = useState(false);

useEffect(() => {
const initialValues = props.formFields.map((f) => ({
id: f.id,
value: fetchDefaultValue(f),
}));
setFieldStates(initialValues);
}, []);

const updateFieldState = useCallback(
(id: string, update: (os: FieldState) => FieldState) => {
setFieldStates((os) => {
Expand Down

0 comments on commit 2be7cfa

Please sign in to comment.