Skip to content

Commit a4f7d8a

Browse files
committed
feat: add wrapper root
1 parent 86b7162 commit a4f7d8a

4 files changed

Lines changed: 49 additions & 41 deletions

File tree

assets/index.less

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
@switchPrefixCls: rc-switch;
22

3-
@duration: .3s;
3+
@duration: 0.3s;
4+
5+
.@{switchPrefixCls}-wrapper {
6+
display: flex;
7+
align-items: center;
8+
}
49

510
.@{switchPrefixCls} {
611
position: relative;
@@ -20,7 +25,7 @@
2025

2126
&-inner-checked,
2227
&-inner-unchecked {
23-
color:#fff;
28+
color: #fff;
2429
font-size: 12px;
2530
position: absolute;
2631
top: 0;
@@ -35,15 +40,15 @@
3540
left: 24px;
3641
}
3742

38-
&:after{
43+
&:after {
3944
position: absolute;
4045
width: 18px;
4146
height: 18px;
4247
left: 2px;
4348
top: 1px;
4449
border-radius: 50% 50%;
4550
background-color: #fff;
46-
content: " ";
51+
content: ' ';
4752
cursor: pointer;
4853
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.26);
4954
transform: scale(1);
@@ -75,23 +80,23 @@
7580
left: 44px;
7681
}
7782

78-
&:after{
83+
&:after {
7984
left: 22px;
8085
}
8186
}
8287

83-
&-disabled{
88+
&-disabled {
8489
cursor: no-drop;
8590
background: #ccc;
8691
border-color: #ccc;
8792

88-
&:after{
93+
&:after {
8994
background: #9e9e9e;
9095
animation-name: none;
9196
cursor: no-drop;
9297
}
9398

94-
&:hover:after{
99+
&:hover:after {
95100
transform: scale(1);
96101
animation-name: none;
97102
}

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@
4343
]
4444
},
4545
"dependencies": {
46-
"classnames": "^2.2.1",
47-
"@rc-component/util": "^1.2.0"
46+
"@rc-component/util": "^1.2.0",
47+
"classnames": "^2.2.1"
4848
},
4949
"devDependencies": {
5050
"@rc-component/father-plugin": "^2.0.0",
51+
"@rc-component/np": "^1.0.3",
5152
"@types/classnames": "^2.2.10",
5253
"@types/jest": "^29.4.0",
54+
"@types/react": "^19.1.8",
5355
"@umijs/fabric": "^3.0.0",
5456
"cheerio": "1.0.0-rc.12",
5557
"dumi": "^2.0.0",
@@ -64,7 +66,6 @@
6466
"husky": "^8.0.1",
6567
"less": "^4.1.3",
6668
"lint-staged": "^15.1.0",
67-
"@rc-component/np": "^1.0.3",
6869
"prettier": "^3.1.0",
6970
"react": "^16.0.0",
7071
"react-dom": "^16.0.0",

src/index.tsx

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -90,33 +90,35 @@ const Switch = React.forwardRef<HTMLButtonElement, SwitchProps>(
9090
});
9191

9292
return (
93-
<button
94-
{...restProps}
95-
type="button"
96-
role="switch"
97-
aria-checked={innerChecked}
98-
disabled={disabled}
99-
className={switchClassName}
100-
ref={ref}
101-
onKeyDown={onInternalKeyDown}
102-
onClick={onInternalClick}
103-
>
104-
{loadingIcon}
105-
<span className={`${prefixCls}-inner`}>
106-
<span
107-
className={classNames(`${prefixCls}-inner-checked`, switchClassNames?.content)}
108-
style={styles?.content}
109-
>
110-
{checkedChildren}
93+
<label className={`${prefixCls}-wrapper`}>
94+
<button
95+
{...restProps}
96+
type="button"
97+
role="switch"
98+
aria-checked={innerChecked}
99+
disabled={disabled}
100+
className={switchClassName}
101+
ref={ref}
102+
onKeyDown={onInternalKeyDown}
103+
onClick={onInternalClick}
104+
>
105+
{loadingIcon}
106+
<span className={`${prefixCls}-inner`}>
107+
<span
108+
className={classNames(`${prefixCls}-inner-checked`, switchClassNames?.content)}
109+
style={styles?.content}
110+
>
111+
{checkedChildren}
112+
</span>
113+
<span
114+
className={classNames(`${prefixCls}-inner-unchecked`, switchClassNames?.content)}
115+
style={styles?.content}
116+
>
117+
{unCheckedChildren}
118+
</span>
111119
</span>
112-
<span
113-
className={classNames(`${prefixCls}-inner-unchecked`, switchClassNames?.content)}
114-
style={styles?.content}
115-
>
116-
{unCheckedChildren}
117-
</span>
118-
</span>
119-
</button>
120+
</button>
121+
</label>
120122
);
121123
},
122124
);

tests/index.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,18 @@ describe('rc-switch', () => {
3434
const onChange = jest.fn();
3535
const wrapper = createSwitch({ defaultChecked: true, onChange });
3636
expect(wrapper.exists('.checked')).toBeTruthy();
37-
wrapper.simulate('click');
37+
wrapper.find('.rc-switch').simulate('click');
3838
expect(wrapper.exists('.unchecked')).toBeTruthy();
3939
expect(onChange.mock.calls.length).toBe(1);
4040
});
4141

4242
it('should support onClick', () => {
4343
const onClick = jest.fn();
4444
const wrapper = createSwitch({ onClick });
45-
wrapper.simulate('click');
45+
wrapper.find('.rc-switch').simulate('click');
4646
expect(onClick).toHaveBeenCalledWith(true, expect.objectContaining({ type: 'click' }));
4747
expect(onClick.mock.calls.length).toBe(1);
48-
wrapper.simulate('click');
48+
wrapper.find('.rc-switch').simulate('click');
4949
expect(onClick).toHaveBeenCalledWith(false, expect.objectContaining({ type: 'click' }));
5050
expect(onClick.mock.calls.length).toBe(2);
5151
});
@@ -117,7 +117,7 @@ describe('rc-switch', () => {
117117
it('onMouseUp', () => {
118118
const onMouseUp = jest.fn();
119119
const wrapper = createSwitch({ onMouseUp });
120-
wrapper.simulate('mouseup');
120+
wrapper.find('.rc-switch').simulate('mouseup');
121121
expect(onMouseUp).toHaveBeenCalled();
122122
});
123123

0 commit comments

Comments
 (0)