Skip to content

Commit 325f3d9

Browse files
authored
feat: Switch support classnames and styles (#157)
1 parent 4c3be22 commit 325f3d9

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/index.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ interface SwitchProps
2525
loadingIcon?: React.ReactNode;
2626
style?: React.CSSProperties;
2727
title?: string;
28+
styles?: { content: React.CSSProperties };
29+
classNames?: { content: string };
2830
}
2931

3032
const Switch = React.forwardRef<HTMLButtonElement, SwitchProps>(
@@ -41,6 +43,8 @@ const Switch = React.forwardRef<HTMLButtonElement, SwitchProps>(
4143
onClick,
4244
onChange,
4345
onKeyDown,
46+
styles,
47+
classNames: switchClassNames,
4448
...restProps
4549
},
4650
ref,
@@ -99,8 +103,18 @@ const Switch = React.forwardRef<HTMLButtonElement, SwitchProps>(
99103
>
100104
{loadingIcon}
101105
<span className={`${prefixCls}-inner`}>
102-
<span className={`${prefixCls}-inner-checked`}>{checkedChildren}</span>
103-
<span className={`${prefixCls}-inner-unchecked`}>{unCheckedChildren}</span>
106+
<span
107+
className={classNames(`${prefixCls}-inner-checked`, switchClassNames?.content)}
108+
style={styles?.content}
109+
>
110+
{checkedChildren}
111+
</span>
112+
<span
113+
className={classNames(`${prefixCls}-inner-unchecked`, switchClassNames?.content)}
114+
style={styles?.content}
115+
>
116+
{unCheckedChildren}
117+
</span>
104118
</span>
105119
</button>
106120
);

tests/index.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,13 @@ describe('rc-switch', () => {
129129
wrapper.simulate('click');
130130
expect(onChange).not.toHaveBeenCalled();
131131
});
132+
it('support classnames and styles', () => {
133+
const wrapper = createSwitch({
134+
classNames: { content: 'custom-content' },
135+
styles: { content: { background: 'red' } },
136+
});
137+
const contentElement = wrapper.find('.custom-content').at(0);
138+
expect(contentElement.exists()).toBe(true);
139+
expect(contentElement.prop('style')).toEqual({ background: 'red' });
140+
});
132141
});

0 commit comments

Comments
 (0)