Skip to content

Commit baaccd5

Browse files
zkwolftangjinzhou
andauthored
fix(switch): custom checkedChildren unexpected (#4528)
* fix(switch): custom checkedChildren unexpected * Update index.tsx Co-authored-by: tangjinzhou <415800467@qq.com>
1 parent c7abe76 commit baaccd5

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

components/switch/__tests__/index.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,35 @@ describe('Switch', () => {
7070
});
7171
expect(checked.value).toBe(1);
7272
});
73+
74+
it('customize checked value and children should work', async () => {
75+
resetWarned();
76+
const checked = ref(1);
77+
const onUpdate = val => (checked.value = val);
78+
const wrapper = mount({
79+
render() {
80+
return (
81+
<Switch
82+
{...{ 'onUpdate:checked': onUpdate }}
83+
checked={checked.value}
84+
unCheckedValue={1}
85+
checkedValue={2}
86+
checkedChildren="on"
87+
unCheckedChildren="off"
88+
/>
89+
);
90+
},
91+
});
92+
await asyncExpect(() => {
93+
wrapper.find('button').trigger('click');
94+
});
95+
expect(checked.value).toBe(2);
96+
expect(wrapper.find('.ant-switch-inner').text()).toBe('on');
97+
98+
await asyncExpect(() => {
99+
wrapper.find('button').trigger('click');
100+
});
101+
expect(checked.value).toBe(1);
102+
expect(wrapper.find('.ant-switch-inner').text()).toBe('off');
103+
});
73104
});

components/switch/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ const Switch = defineComponent({
134134
[`${prefixCls.value}-disabled`]: props.disabled,
135135
[prefixCls.value]: true,
136136
}));
137+
137138
return () => (
138139
<Wave insertExtraNode>
139140
<button
@@ -160,7 +161,7 @@ const Switch = defineComponent({
160161
>
161162
{props.loading ? <LoadingOutlined class={`${prefixCls.value}-loading-icon`} /> : null}
162163
<span class={`${prefixCls.value}-inner`}>
163-
{checked.value
164+
{checkedStatus.value
164165
? getPropsSlot(slots, props, 'checkedChildren')
165166
: getPropsSlot(slots, props, 'unCheckedChildren')}
166167
</span>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy