diff --git a/src/components/config-provider/config-provider.tsx b/src/components/config-provider/config-provider.tsx index 7b4d75f3f6..3f2fb8b52d 100644 --- a/src/components/config-provider/config-provider.tsx +++ b/src/components/config-provider/config-provider.tsx @@ -4,6 +4,7 @@ import { Locale } from '../../locales/base' import zhCN from '../../locales/zh-CN' type Config = { + direction?: 'ltr' | 'rtl' 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)