Skip to content

Commit 24bf706

Browse files
committed
feat toggle
1 parent 899db40 commit 24bf706

File tree

3 files changed

+96
-13
lines changed

3 files changed

+96
-13
lines changed
Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
1+
import { Meta, StoryObj } from '@storybook/react-vite'
2+
13
import { Toggle } from './index'
24

3-
export default {
5+
type Story = StoryObj<typeof meta>
6+
7+
const meta: Meta<typeof Toggle> = {
48
title: 'Devfive/Toggle',
59
component: Toggle,
6-
tags: ['autodocs'],
10+
decorators: [
11+
(Story) => (
12+
<div style={{ padding: '10px' }}>
13+
<Story />
14+
</div>
15+
),
16+
],
717
}
818

9-
export const Default = {
19+
export const Default: Story = {
1020
args: {
11-
disabled: false,
12-
variant: 'default',
1321
defaultValue: false,
14-
offText: 'OFF',
15-
onText: 'ON',
16-
color: 'var(--primary)',
22+
variant: 'default',
23+
disabled: false,
1724
},
1825
}
26+
27+
export default meta
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { act, render } from '@testing-library/react'
2+
3+
import { Toggle } from '../index'
4+
5+
vi.mock('react', async (originImport: any) => {
6+
const origin = await originImport()
7+
return {
8+
...origin,
9+
cache: vi.fn((arg) => arg),
10+
}
11+
})
12+
describe('Toggle', () => {
13+
it('should Toggle snapshot', () => {
14+
expect(render(<Toggle />).container).toMatchSnapshot()
15+
expect(render(<Toggle disabled />).container).toMatchSnapshot()
16+
expect(render(<Toggle defaultValue={true} />).container).toMatchSnapshot()
17+
expect(render(<Toggle value={true} />).container).toMatchSnapshot()
18+
expect(render(<Toggle variant="switch" />).container).toMatchSnapshot()
19+
expect(
20+
render(<Toggle disabled variant="switch" />).container,
21+
).toMatchSnapshot()
22+
expect(
23+
render(<Toggle defaultValue={true} variant="switch" />).container,
24+
).toMatchSnapshot()
25+
expect(
26+
render(<Toggle value={true} variant="switch" />).container,
27+
).toMatchSnapshot()
28+
expect(
29+
render(
30+
<Toggle
31+
colors={{
32+
primary: 'blue',
33+
bg: 'blue',
34+
hoverBg: 'blue',
35+
primaryHoverBg: 'blue',
36+
disabledBg: 'blue',
37+
switchHoverOutline: 'blue',
38+
switchShadow: 'blue',
39+
}}
40+
/>,
41+
).container,
42+
).toMatchSnapshot()
43+
expect(
44+
render(
45+
<Toggle
46+
colors={{
47+
primary: 'blue',
48+
bg: 'blue',
49+
hoverBg: 'blue',
50+
primaryHoverBg: 'blue',
51+
disabledBg: 'blue',
52+
switchHoverOutline: 'blue',
53+
switchShadow: 'blue',
54+
}}
55+
variant="switch"
56+
/>,
57+
).container,
58+
).toMatchSnapshot()
59+
})
60+
61+
it('should change value when use onChange prop', async () => {
62+
const onChange = vi.fn()
63+
const { container } = render(<Toggle onChange={onChange} />)
64+
const toggleButton = container.querySelector(`.toggleSwitch`)
65+
const input = container.querySelector('input')
66+
toggleButton &&
67+
(await act(async () => {
68+
await userEvent.click(toggleButton)
69+
}))
70+
expect(input).toHaveAttribute('value', 'true')
71+
})
72+
})

packages/components/src/components/Toggle/index.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export function Toggle({
6363
}
6464
borderRadius="500px"
6565
boxSizing="border-box"
66-
className="toggleSwitch"
66+
className={'toggle-switch ' + className}
6767
cursor="pointer"
6868
h={isDefault ? '28px' : '8px'}
6969
justifyContent={resultValue ? 'flex-end' : undefined}
@@ -105,10 +105,12 @@ export function Toggle({
105105
outline="4px"
106106
pos="absolute"
107107
selectors={{
108-
'.toggleSwitch:hover[aria-disabled=false] > &': {
109-
outline: '4px solid',
110-
outlineColor: `var(--switchHoverOutline, light-dark(color-mix(in srgb, var(--primary) 20%, transparent), color-mix(in srgb, var(--primary) 50%, transparent)))`,
111-
},
108+
'.toggle-switch:not([aria-disabled=true]):hover > &': isDefault
109+
? {}
110+
: {
111+
outline: '4px solid',
112+
outlineColor: `var(--switchHoverOutline, light-dark(color-mix(in srgb, var(--primary) 20%, transparent), color-mix(in srgb, var(--primary) 50%, transparent)))`,
113+
},
112114
}}
113115
style={styles?.toggle}
114116
styleVars={{

0 commit comments

Comments
 (0)