From a2bb4016b377f2bc0f07ded0864aff9ca8f006d1 Mon Sep 17 00:00:00 2001 From: bobihuang Date: Thu, 23 Nov 2023 11:42:55 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20switch=20=E6=94=AF=E6=8C=81=20RTL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/config-provider/config-provider.tsx | 3 +++ src/components/switch/switch.less | 10 ++++++++++ src/components/switch/switch.tsx | 8 ++++++-- src/components/switch/tests/switch.test.tsx | 13 +++++++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/components/config-provider/config-provider.tsx b/src/components/config-provider/config-provider.tsx index 7b4d75f3f6..9ebfda086e 100644 --- a/src/components/config-provider/config-provider.tsx +++ b/src/components/config-provider/config-provider.tsx @@ -3,7 +3,10 @@ import type { FC, ReactNode } from 'react' import { Locale } from '../../locales/base' import zhCN from '../../locales/zh-CN' +type DirectionType = 'ltr' | 'rtl' | undefined + type Config = { + direction?: DirectionType locale: Locale children?: ReactNode } diff --git a/src/components/switch/switch.less b/src/components/switch/switch.less index a3451f0288..2ac8255d7a 100644 --- a/src/components/switch/switch.less +++ b/src/components/switch/switch.less @@ -66,6 +66,11 @@ -1px 2px 2px 0 rgba(0, 0, 0, 0.1); } + &-handle-rtl { + left: auto; + right: var(--border-width); + } + &-inner { position: relative; z-index: 1; @@ -91,6 +96,11 @@ left: calc(100% - (var(--height) - var(--border-width))); } + .@{class-prefix-switch}-handle-rtl { + left: auto; + right: calc(100% - (var(--height) - var(--border-width))); + } + .@{class-prefix-switch}-inner { margin: 0 calc(var(--height) - var(--border-width) + 5px) 0 8px; color: var(--adm-color-text-light-solid); diff --git a/src/components/switch/switch.tsx b/src/components/switch/switch.tsx index dfad26a3fb..a0f214d7d6 100644 --- a/src/components/switch/switch.tsx +++ b/src/components/switch/switch.tsx @@ -30,7 +30,7 @@ export const Switch: FC = p => { const props = mergeProps(defaultProps, p) const disabled = props.disabled || props.loading || false const [changing, setChanging] = useState(false) - const { locale } = useConfig() + const { locale, direction } = useConfig() const [checked, setChecked] = usePropsValue({ value: props.checked, @@ -80,7 +80,11 @@ export const Switch: FC = p => { aria-disabled={disabled} >
-
+
{(props.loading || changing) && ( )} diff --git a/src/components/switch/tests/switch.test.tsx b/src/components/switch/tests/switch.test.tsx index 23d29c3be4..18193857bd 100644 --- a/src/components/switch/tests/switch.test.tsx +++ b/src/components/switch/tests/switch.test.tsx @@ -9,6 +9,8 @@ import { act, } from 'testing' import Switch from '..' +import ConfigProvider from '../../config-provider' +import zhCN from '../../../locales/zh-CN' const classPrefix = `adm-switch` @@ -22,6 +24,17 @@ describe('Switch', () => { expect(screen.getByRole('switch')).toHaveClass(`${classPrefix}-disabled`) }) + test('renders with rtl mode', () => { + render( + + + + ) + expect( + screen.getByRole('switch').querySelector(`.${classPrefix}-handle-rtl`) + ).not.toBeNull() + }) + test('controlled mode', async () => { const App = () => { const [checked, setChecked] = useState(false) From a7448d0ebcbbd9ae675a095a4a79d54172cfd1ea Mon Sep 17 00:00:00 2001 From: bobihuang Date: Thu, 23 Nov 2023 17:27:48 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=E7=B1=BB=E5=9E=8B=E5=A3=B0?= =?UTF-8?q?=E6=98=8E=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/config-provider/config-provider.tsx | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/components/config-provider/config-provider.tsx b/src/components/config-provider/config-provider.tsx index 9ebfda086e..3f2fb8b52d 100644 --- a/src/components/config-provider/config-provider.tsx +++ b/src/components/config-provider/config-provider.tsx @@ -3,10 +3,8 @@ import type { FC, ReactNode } from 'react' import { Locale } from '../../locales/base' import zhCN from '../../locales/zh-CN' -type DirectionType = 'ltr' | 'rtl' | undefined - type Config = { - direction?: DirectionType + direction?: 'ltr' | 'rtl' locale: Locale children?: ReactNode }