Skip to content

Commit 53f286c

Browse files
authored
fix(switch): 异步操作中抛错可中断 loading 态 (#3143)
* feat: 控制加载状态的图标,传入空值时禁用 loading 态 * fix: modify cr * fix: remove console * feat: 增加demo使用描述 * feat: update test * fix: 还原doc * fix: 还原doc * fix: update descr
1 parent a7f79dd commit 53f286c

5 files changed

Lines changed: 39 additions & 26 deletions

File tree

src/packages/switch/__test__/switch.spec.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,13 @@ test('disabled test', async () => {
6363
})
6464

6565
test('loadingIcon test', async () => {
66-
const testFn = vi.fn()
6766
const { container } = render(
68-
<Switch loadingIcon={<Loading1 />} onChange={testFn} />
67+
<Switch
68+
loadingIcon={<Loading1 />}
69+
onChange={() => {
70+
throw new Error('Function not implemented.')
71+
}}
72+
/>
6973
)
7074
const el: Element | null = container.querySelector('.nut-switch-button')
7175
if (el) {

src/packages/switch/demos/h5/demo2.tsx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@ import { Cell, Switch, Toast } from '@nutui/nutui-react'
44
const Demo2 = () => {
55
const [checkedAsync, setCheckedAsync] = useState(true)
66

7-
const mockRequest = (): Promise<void> => {
8-
return new Promise((resolve) => {
7+
const onChangeAsync = async (value: boolean) => {
8+
Toast.show(`2秒后异步触发 ${value}`)
9+
const res = await new Promise((resolve) => {
910
setTimeout(() => {
10-
resolve()
11+
resolve(true)
1112
}, 2000)
1213
})
13-
}
14-
15-
const onChangeAsync = async (value: boolean) => {
16-
Toast.show(`2秒后异步触发 ${value}`)
17-
await mockRequest()
18-
setCheckedAsync(value)
14+
if (!res) {
15+
// 主动抛出一个错误对象,用于中断组件 loading 态
16+
throw new Error()
17+
} else {
18+
setCheckedAsync(value)
19+
}
1920
}
2021
return (
2122
<Cell>

src/packages/switch/demos/taro/demo2.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@ const Demo2 = () => {
55
const [checkedAsync, setCheckedAsync] = useState(true)
66
const [value, setValue] = useState(false)
77
const [showToast, setShowToast] = useState(false)
8-
const mockRequest = (): Promise<void> => {
9-
return new Promise((resolve) => {
10-
setTimeout(() => {
11-
resolve()
12-
}, 2000)
13-
})
14-
}
158

169
const onChangeAsync = async (value: boolean) => {
1710
setValue(value)
1811
setShowToast(true)
19-
await mockRequest()
20-
setCheckedAsync(value)
12+
const res = await new Promise((resolve) => {
13+
setTimeout(() => {
14+
resolve(true)
15+
}, 2000)
16+
})
17+
if (!res) {
18+
// 主动抛出一个错误对象,用于中断组件 loading 态
19+
throw new Error()
20+
} else {
21+
setCheckedAsync(value)
22+
}
2123
}
2224
return (
2325
<>

src/packages/switch/switch.taro.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ export const Switch: FunctionComponent<Partial<TaroSwitchProps>> = (props) => {
3232
...props,
3333
}
3434
const classPrefix = 'nut-switch'
35-
3635
const rtl = useRtl()
37-
3836
const [value, setValue] = usePropsValue<boolean>({
3937
value: checked,
4038
defaultValue: defaultChecked,
@@ -58,11 +56,15 @@ export const Switch: FunctionComponent<Partial<TaroSwitchProps>> = (props) => {
5856
])
5957
}
6058

61-
const onClick = () => {
59+
const onClick = async () => {
6260
if (disabled || changing) return
6361
if (onChange) {
6462
setChanging(true)
65-
onChange(!value)
63+
try {
64+
await onChange(!value)
65+
} catch (e) {
66+
setChanging(false)
67+
}
6668
}
6769
setValue(!value)
6870
}

src/packages/switch/switch.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,15 @@ export const Switch: FunctionComponent<Partial<WebSwitchProps>> = (props) => {
5656
])
5757
}
5858

59-
const onClick = () => {
59+
const onClick = async () => {
6060
if (disabled || changing) return
6161
if (onChange) {
6262
setChanging(true)
63-
onChange(!value)
63+
try {
64+
await onChange(!value)
65+
} catch (e) {
66+
setChanging(false)
67+
}
6468
}
6569
setValue(!value)
6670
}

0 commit comments

Comments
 (0)