From 717b79b9f67237dff7211f0de7ca1c88d97c4aeb Mon Sep 17 00:00:00 2001 From: wangsijie Date: Wed, 13 Apr 2022 15:10:40 +0800 Subject: [PATCH] feat(console): toggle switch --- .../src/components/Switch/index.module.scss | 61 +++++++++++++++++++ .../console/src/components/Switch/index.tsx | 21 +++++++ .../components/BrandingForm.tsx | 19 +++--- .../SignInExperience/components/TermsForm.tsx | 17 +++--- .../src/pages/SignInExperience/index.tsx | 40 ++++++------ 5 files changed, 119 insertions(+), 39 deletions(-) create mode 100644 packages/console/src/components/Switch/index.module.scss create mode 100644 packages/console/src/components/Switch/index.tsx diff --git a/packages/console/src/components/Switch/index.module.scss b/packages/console/src/components/Switch/index.module.scss new file mode 100644 index 000000000..4b86c59ae --- /dev/null +++ b/packages/console/src/components/Switch/index.module.scss @@ -0,0 +1,61 @@ +@use '@/scss/underscore' as _; + +.switch { + position: relative; + display: inline-block; + width: 40px; + height: 24px; + + input { + opacity: 0%; + width: 0; + height: 0; + } + + .slider { + position: absolute; + cursor: pointer; + top: 0; + left: 0; + right: 0; + bottom: 0; + background-color: var(--color-neutral-90); + transition: 0.4s; + border-radius: 12px; + + &::before { + position: absolute; + content: ''; + height: 20px; + width: 20px; + left: 2px; + bottom: 2px; + background-color: #fff; + transition: 0.4s; + border-radius: 50%; + box-shadow: 0 3px 7px rgba(0, 0, 0, 12%); + } + } + + input:checked + .slider { + background-color: var(--color-success-70); + } + + input:checked + .slider::before { + transform: translateX(16px); + } +} + +.wrapper { + display: flex; + align-items: center; + border: 1px solid var(--color-neutral-90); + border-radius: _.unit(2); + padding: _.unit(4); + + .label { + flex: 1; + margin-right: _.unit(2); + font: var(--font-body-medium); + } +} diff --git a/packages/console/src/components/Switch/index.tsx b/packages/console/src/components/Switch/index.tsx new file mode 100644 index 000000000..198d8fbf5 --- /dev/null +++ b/packages/console/src/components/Switch/index.tsx @@ -0,0 +1,21 @@ +import React, { forwardRef, HTMLProps, ReactNode, Ref } from 'react'; + +import * as styles from './index.module.scss'; + +type Props = HTMLProps & { + label?: ReactNode; +}; + +const Switch = ({ label, ...rest }: Props, ref?: Ref) => { + return ( +
+
{label}
+ +
+ ); +}; + +export default forwardRef(Switch); diff --git a/packages/console/src/pages/SignInExperience/components/BrandingForm.tsx b/packages/console/src/pages/SignInExperience/components/BrandingForm.tsx index 57b15a381..2b56b63eb 100644 --- a/packages/console/src/pages/SignInExperience/components/BrandingForm.tsx +++ b/packages/console/src/pages/SignInExperience/components/BrandingForm.tsx @@ -1,22 +1,18 @@ import { BrandingStyle, SignInExperience } from '@logto/schemas'; import React from 'react'; -import { Control, Controller, UseFormRegister, UseFormWatch } from 'react-hook-form'; +import { Controller, useFormContext } from 'react-hook-form'; import { useTranslation } from 'react-i18next'; import FormField from '@/components/FormField'; import RadioGroup, { Radio } from '@/components/RadioGroup'; +import Switch from '@/components/Switch'; import TextInput from '@/components/TextInput'; import * as styles from './index.module.scss'; -type Props = { - register: UseFormRegister; - control: Control; - watch: UseFormWatch; -}; - -const BrandingForm = ({ register, control, watch }: Props) => { +const BrandingForm = () => { const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' }); + const { watch, register, control } = useFormContext(); const isDarkModeEnabled = watch('branding.isDarkModeEnabled'); const style = watch('branding.style'); @@ -30,9 +26,10 @@ const BrandingForm = ({ register, control, watch }: Props) => { - {/* TODO: LOG-2152 switch */} - -
{t('sign_in_exp.branding.dark_mode_description')}
+
; - watch: UseFormWatch; -}; - -const TermsForm = ({ register, watch }: Props) => { +const TermsForm = () => { const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' }); + const { watch, register } = useFormContext(); const enabled = watch('termsOfUse.enabled'); return ( <>
{t('sign_in_exp.terms_of_use.title')}
- -
{t('sign_in_exp.terms_of_use.description')}
+
diff --git a/packages/console/src/pages/SignInExperience/index.tsx b/packages/console/src/pages/SignInExperience/index.tsx index 897b735d6..cd3b19503 100644 --- a/packages/console/src/pages/SignInExperience/index.tsx +++ b/packages/console/src/pages/SignInExperience/index.tsx @@ -1,6 +1,6 @@ import { SignInExperience as SignInExperienceType } from '@logto/schemas'; import React, { useEffect } from 'react'; -import { useForm } from 'react-hook-form'; +import { FormProvider, useForm } from 'react-hook-form'; import { toast } from 'react-hot-toast'; import { useTranslation } from 'react-i18next'; import { useParams } from 'react-router-dom'; @@ -21,14 +21,12 @@ const SignInExperience = () => { const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' }); const { tab } = useParams(); const { data, error, mutate } = useSWR('/api/sign-in-exp'); + const methods = useForm(); const { reset, - control, handleSubmit, - register, - watch, formState: { isSubmitting }, - } = useForm(); + } = methods; const api = useApi(); useEffect(() => { @@ -72,20 +70,24 @@ const SignInExperience = () => { {!data && !error &&
loading
} {error &&
{`error occurred: ${error.body.message}`}
} {data && ( -
- {tab === 'experience' && ( - - )} - {tab === 'experience' && } -
-
- + +
+ {tab === 'experience' && ( + <> + + + + )} +
+
+ +
)}