diff --git a/lib/build/emailpassword-shared7.js b/lib/build/emailpassword-shared7.js index a2672b8b5..a4f63ad4c 100644 --- a/lib/build/emailpassword-shared7.js +++ b/lib/build/emailpassword-shared7.js @@ -436,24 +436,24 @@ function Label(_a) { ); } -// 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 ""; -// }; +var fetchDefaultValue = function (field) { + if (field && field.getDefaultValue) { + try { + if (typeof field.getDefaultValue !== "function") { + throw new Error("getDefaultValue for ".concat(field.id, " must be a function")); + } + var defaultValue = field.getDefaultValue(); + if (typeof defaultValue !== "string") { + throw new Error("getDefaultValue for ".concat(field.id, " must return a string")); + } else { + return defaultValue; + } + } catch (error) { + console.error(error); + } + } + return ""; +}; var FormBase = function (props) { var footer = props.footer, buttonLabel = props.buttonLabel, @@ -473,7 +473,7 @@ var FormBase = function (props) { ); var _a = React.useState( props.formFields.map(function (f) { - return { id: f.id, value: "" }; + return { id: f.id, value: fetchDefaultValue(f) }; }) ), fieldStates = _a[0], @@ -481,13 +481,6 @@ var FormBase = function (props) { var _b = React.useState(false), isLoading = _b[0], setIsLoading = _b[1]; - // useEffect(() => { - // const initialValues = props.formFields.map((f) => ({ - // id: f.id, - // value: fetchDefaultValue(f), - // })); - // setFieldStates(initialValues); - // }, []); var updateFieldState = React.useCallback( function (id, update) { setFieldStates(function (os) { diff --git a/lib/ts/recipe/emailpassword/components/library/formBase.tsx b/lib/ts/recipe/emailpassword/components/library/formBase.tsx index 56977fa7b..3418d68d5 100644 --- a/lib/ts/recipe/emailpassword/components/library/formBase.tsx +++ b/lib/ts/recipe/emailpassword/components/library/formBase.tsx @@ -25,7 +25,7 @@ import STGeneralError from "supertokens-web-js/utils/error"; import { MANDATORY_FORM_FIELDS_ID_ARRAY } from "../../constants"; import type { APIFormField } from "../../../../types"; -import type { FormBaseProps } from "../../types"; +import type { FormBaseProps, FormFieldThemeProps } from "../../types"; import type { FormEvent } from "react"; import { Button, FormRow, Input, InputError, Label } from "."; @@ -37,24 +37,24 @@ 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 ""; -// }; +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> = (props) => { const { footer, buttonLabel, showLabels, validateOnBlur, formFields } = props; @@ -69,18 +69,10 @@ export const FormBase: React.FC> = (props) => { }, [unmounting]); const [fieldStates, setFieldStates] = useState( - props.formFields.map((f) => ({ id: f.id, value: "" })) + props.formFields.map((f) => ({ id: f.id, value: fetchDefaultValue(f) })) ); 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) => {