Skip to content

feat: add wrapper root #159

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 13 additions & 8 deletions assets/index.less
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
@switchPrefixCls: rc-switch;

@duration: .3s;
@duration: 0.3s;

.@{switchPrefixCls}-wrapper {
display: flex;
align-items: center;
}

.@{switchPrefixCls} {
position: relative;
Expand All @@ -20,7 +25,7 @@

&-inner-checked,
&-inner-unchecked {
color:#fff;
color: #fff;
font-size: 12px;
position: absolute;
top: 0;
Expand All @@ -35,15 +40,15 @@
left: 24px;
}

&:after{
&:after {
position: absolute;
width: 18px;
height: 18px;
left: 2px;
top: 1px;
border-radius: 50% 50%;
background-color: #fff;
content: " ";
content: ' ';
cursor: pointer;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.26);
transform: scale(1);
Expand Down Expand Up @@ -75,23 +80,23 @@
left: 44px;
}

&:after{
&:after {
left: 22px;
}
}

&-disabled{
&-disabled {
cursor: no-drop;
background: #ccc;
border-color: #ccc;

&:after{
&:after {
background: #9e9e9e;
animation-name: none;
cursor: no-drop;
}

&:hover:after{
&:hover:after {
transform: scale(1);
animation-name: none;
}
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@
]
},
"dependencies": {
"classnames": "^2.2.1",
"@rc-component/util": "^1.2.0"
"@rc-component/util": "^1.2.0",
"classnames": "^2.2.1"
},
"devDependencies": {
"@rc-component/father-plugin": "^2.0.0",
"@rc-component/np": "^1.0.3",
"@types/classnames": "^2.2.10",
"@types/jest": "^29.4.0",
"@types/react": "^19.1.8",
"@umijs/fabric": "^3.0.0",
"cheerio": "1.0.0-rc.12",
"dumi": "^2.0.0",
Expand All @@ -64,7 +66,6 @@
"husky": "^8.0.1",
"less": "^4.1.3",
"lint-staged": "^15.1.0",
"@rc-component/np": "^1.0.3",
"prettier": "^3.1.0",
"react": "^16.0.0",
"react-dom": "^16.0.0",
Expand Down
54 changes: 28 additions & 26 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,33 +90,35 @@ const Switch = React.forwardRef<HTMLButtonElement, SwitchProps>(
});

return (
<button
{...restProps}
type="button"
role="switch"
aria-checked={innerChecked}
disabled={disabled}
className={switchClassName}
ref={ref}
onKeyDown={onInternalKeyDown}
onClick={onInternalClick}
>
{loadingIcon}
<span className={`${prefixCls}-inner`}>
<span
className={classNames(`${prefixCls}-inner-checked`, switchClassNames?.content)}
style={styles?.content}
>
{checkedChildren}
<span className={`${prefixCls}-wrapper`}>
<button
{...restProps}
type="button"
role="switch"
aria-checked={innerChecked}
disabled={disabled}
className={switchClassName}
ref={ref}
onKeyDown={onInternalKeyDown}
onClick={onInternalClick}
>
{loadingIcon}
<span className={`${prefixCls}-inner`}>
<span
className={classNames(`${prefixCls}-inner-checked`, switchClassNames?.content)}
style={styles?.content}
>
{checkedChildren}
</span>
<span
className={classNames(`${prefixCls}-inner-unchecked`, switchClassNames?.content)}
style={styles?.content}
>
{unCheckedChildren}
</span>
</span>
<span
className={classNames(`${prefixCls}-inner-unchecked`, switchClassNames?.content)}
style={styles?.content}
>
{unCheckedChildren}
</span>
</span>
</button>
</button>
</span>
);
},
);
Expand Down
44 changes: 23 additions & 21 deletions tests/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,50 @@ describe('rc-switch', () => {
);
}

const findSwitch = (wrapper) => wrapper.find('.rc-switch');

it('works', () => {
const wrapper = createSwitch();
expect(wrapper.exists('.unchecked')).toBeTruthy();
wrapper.simulate('click');
expect(wrapper.exists('.checked')).toBeTruthy();
expect(findSwitch(wrapper).exists('.unchecked')).toBeTruthy();
findSwitch(wrapper).simulate('click');
expect(findSwitch(wrapper).exists('.checked')).toBeTruthy();
});

it('should be checked upon right key and unchecked on left key', () => {
const wrapper = createSwitch();
expect(wrapper.exists('.unchecked')).toBeTruthy();
wrapper.simulate('keydown', { which: KeyCode.RIGHT });
expect(wrapper.exists('.checked')).toBeTruthy();
wrapper.simulate('keydown', { which: KeyCode.LEFT });
expect(wrapper.exists('.unchecked')).toBeTruthy();
expect(findSwitch(wrapper).exists('.unchecked')).toBeTruthy();
findSwitch(wrapper).simulate('keydown', { which: KeyCode.RIGHT });
expect(findSwitch(wrapper).exists('.checked')).toBeTruthy();
findSwitch(wrapper).simulate('keydown', { which: KeyCode.LEFT });
expect(findSwitch(wrapper).exists('.unchecked')).toBeTruthy();
});

it('should change from an initial checked state of true to false on click', () => {
const onChange = jest.fn();
const wrapper = createSwitch({ defaultChecked: true, onChange });
expect(wrapper.exists('.checked')).toBeTruthy();
wrapper.simulate('click');
expect(wrapper.exists('.unchecked')).toBeTruthy();
expect(findSwitch(wrapper).exists('.checked')).toBeTruthy();
findSwitch(wrapper).simulate('click');
expect(findSwitch(wrapper).exists('.unchecked')).toBeTruthy();
expect(onChange.mock.calls.length).toBe(1);
});

it('should support onClick', () => {
const onClick = jest.fn();
const wrapper = createSwitch({ onClick });
wrapper.simulate('click');
findSwitch(wrapper).simulate('click');
expect(onClick).toHaveBeenCalledWith(true, expect.objectContaining({ type: 'click' }));
expect(onClick.mock.calls.length).toBe(1);
wrapper.simulate('click');
findSwitch(wrapper).simulate('click');
expect(onClick).toHaveBeenCalledWith(false, expect.objectContaining({ type: 'click' }));
expect(onClick.mock.calls.length).toBe(2);
});

it('should not toggle when clicked in a disabled state', () => {
const onChange = jest.fn();
const wrapper = createSwitch({ disabled: true, checked: true, onChange });
expect(wrapper.exists('.checked')).toBeTruthy();
wrapper.simulate('click');
expect(wrapper.exists('.checked')).toBeTruthy();
expect(findSwitch(wrapper).exists('.checked')).toBeTruthy();
findSwitch(wrapper).simulate('click');
expect(findSwitch(wrapper).exists('.checked')).toBeTruthy();
expect(onChange.mock.calls.length).toBe(0);
});

Expand Down Expand Up @@ -109,15 +111,15 @@ describe('rc-switch', () => {

it('disabled', () => {
const wrapper = createSwitch({ disabled: true });
expect(wrapper.exists('.unchecked')).toBeTruthy();
wrapper.simulate('keydown', { keyCode: 39 });
expect(wrapper.exists('.unchecked')).toBeTruthy();
expect(findSwitch(wrapper).exists('.unchecked')).toBeTruthy();
findSwitch(wrapper).simulate('keydown', { keyCode: 39 });
expect(findSwitch(wrapper).exists('.unchecked')).toBeTruthy();
});

it('onMouseUp', () => {
const onMouseUp = jest.fn();
const wrapper = createSwitch({ onMouseUp });
wrapper.simulate('mouseup');
findSwitch(wrapper).simulate('mouseup');
expect(onMouseUp).toHaveBeenCalled();
});

Expand All @@ -126,7 +128,7 @@ describe('rc-switch', () => {
const onClick = jest.fn();
const wrapper = createSwitch({ disabled: true, onChange, onClick, checked: true });

wrapper.simulate('click');
findSwitch(wrapper).simulate('click');
expect(onChange).not.toHaveBeenCalled();
});
it('support classnames and styles', () => {
Expand Down