Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch components support RTL #6449

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/components/config-provider/config-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
cbbfcd marked this conversation as resolved.
Show resolved Hide resolved
locale: Locale
children?: ReactNode
}
Expand Down
10 changes: 10 additions & 0 deletions src/components/switch/switch.less
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions src/components/switch/switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const Switch: FC<SwitchProps> = 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,
Expand Down Expand Up @@ -80,7 +80,11 @@ export const Switch: FC<SwitchProps> = p => {
aria-disabled={disabled}
>
<div className={`${classPrefix}-checkbox`}>
<div className={`${classPrefix}-handle`}>
<div
className={classNames(`${classPrefix}-handle`, {
[`${classPrefix}-handle-rtl`]: direction === 'rtl',
})}
>
{(props.loading || changing) && (
<SpinIcon className={`${classPrefix}-spin-icon`} />
)}
Expand Down
13 changes: 13 additions & 0 deletions src/components/switch/tests/switch.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand All @@ -22,6 +24,17 @@ describe('Switch', () => {
expect(screen.getByRole('switch')).toHaveClass(`${classPrefix}-disabled`)
})

test('renders with rtl mode', () => {
render(
<ConfigProvider locale={zhCN} direction='rtl'>
<Switch />
</ConfigProvider>
)
expect(
screen.getByRole('switch').querySelector(`.${classPrefix}-handle-rtl`)
).not.toBeNull()
})

test('controlled mode', async () => {
const App = () => {
const [checked, setChecked] = useState(false)
Expand Down
Loading