diff --git a/packages/console/src/components/Checkbox/index.tsx b/packages/console/src/components/Checkbox/index.tsx index 2a0dbe0ca..74e66983f 100644 --- a/packages/console/src/components/Checkbox/index.tsx +++ b/packages/console/src/components/Checkbox/index.tsx @@ -1,5 +1,5 @@ import { nanoid } from 'nanoid'; -import React, { InputHTMLAttributes, ReactNode, useState } from 'react'; +import React, { forwardRef, InputHTMLAttributes, ReactNode, Ref, useState } from 'react'; import Icon from './Icon'; import * as styles from './index.module.scss'; @@ -8,16 +8,16 @@ type Props = Omit, 'type'> & { label?: ReactNode; }; -const Checkbox = ({ label, disabled, ...rest }: Props) => { +const Checkbox = ({ label, disabled, ...rest }: Props, ref: Ref) => { const [id] = useState(nanoid()); return (
- + {label && }
); }; -export default Checkbox; +export default forwardRef(Checkbox); diff --git a/packages/console/src/pages/SignInExperience/components/SignInMethodsForm.tsx b/packages/console/src/pages/SignInExperience/components/SignInMethodsForm.tsx index 5a6ad5a55..7cc37dc8b 100644 --- a/packages/console/src/pages/SignInExperience/components/SignInMethodsForm.tsx +++ b/packages/console/src/pages/SignInExperience/components/SignInMethodsForm.tsx @@ -1,5 +1,5 @@ import { SignInMethodKey } from '@logto/schemas'; -import React from 'react'; +import React, { useEffect, useMemo } from 'react'; import { Controller, useFormContext } from 'react-hook-form'; import { useTranslation } from 'react-i18next'; @@ -16,8 +16,44 @@ const signInMethods = Object.values(SignInMethodKey); const SignInMethodsForm = () => { const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' }); - const { register, watch, control } = useFormContext(); + const { register, watch, control, setValue } = useFormContext(); const primaryMethod = watch('signInMethods.primary'); + const enableSecondary = watch('signInMethods.enableSecondary'); + const social = watch('signInMethods.social'); + + useEffect(() => { + if (primaryMethod) { + // When one of the sign-in methods has been primary, it should not be able to be secondary simultaneously. + setValue(`signInMethods.${primaryMethod}`, false); + } + }, [primaryMethod, setValue]); + + const secondaryMethodsFields = useMemo( + () => + signInMethods.map((method) => { + const label = ( + <> + {t('sign_in_exp.sign_in_methods.methods', { context: method })} + {primaryMethod === method && ( + + {t('sign_in_exp.sign_in_methods.methods_primary_tag')} + + )} + + ); + + return ( +
+ +
+ ); + }), + [primaryMethod, register, t] + ); return ( <> @@ -38,39 +74,39 @@ const SignInMethodsForm = () => { )} /> + {primaryMethod === SignInMethodKey.Social && ( +
+ ( + + )} + /> +
+ )} - {signInMethods.map((method) => ( -
- - {t('sign_in_exp.sign_in_methods.methods', { context: method })} - {primaryMethod === method && ( - - {t('sign_in_exp.sign_in_methods.methods_primary_tag')} - - )} - - } - disabled={primaryMethod === method} - {...register(`signInMethods.${method}`)} - /> -
- ))}
- - ( - + {enableSecondary && ( + <> + {secondaryMethodsFields} + {social && ( + + ( + + )} + /> + )} - /> - + + )} ); }; diff --git a/packages/console/src/pages/SignInExperience/components/index.module.scss b/packages/console/src/pages/SignInExperience/components/index.module.scss index da96d6db7..b7e181356 100644 --- a/packages/console/src/pages/SignInExperience/components/index.module.scss +++ b/packages/console/src/pages/SignInExperience/components/index.module.scss @@ -13,3 +13,7 @@ .method { margin-top: _.unit(2); } + +.primarySocial { + margin-top: _.unit(2); +}