Skip to content

Commit d42520d

Browse files
committed
feat(switch): 异步加载展示loading
1 parent 9a903a0 commit d42520d

File tree

9 files changed

+72
-48
lines changed

9 files changed

+72
-48
lines changed

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

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

7-
const onChangeAsync = (value: boolean, event: any) => {
7+
const mockRequest = (): Promise<void> => {
8+
return new Promise((resolve) => {
9+
setTimeout(() => {
10+
resolve()
11+
}, 2000)
12+
})
13+
}
14+
15+
const onChangeAsync = async (value: boolean) => {
816
Toast.show(`2秒后异步触发 ${value}`)
9-
setTimeout(() => {
10-
setCheckedAsync(value)
11-
}, 2000)
17+
await mockRequest()
18+
setCheckedAsync(value)
1219
}
1320
return (
1421
<Cell>
1522
<Switch
1623
checked={checkedAsync}
17-
onChange={(value, event) => onChangeAsync(value, event)}
24+
onChange={(value) => onChangeAsync(value)}
1825
/>
1926
</Cell>
2027
)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const Demo2 = () => {
66
const [value, setValue] = useState(false)
77
const [showToast, setShowToast] = useState(false)
88

9-
const onChangeAsync = (value: boolean, event: any) => {
9+
const onChangeAsync = (value: boolean) => {
1010
setValue(value)
1111
setShowToast(true)
1212
setTimeout(() => {
@@ -18,7 +18,7 @@ const Demo2 = () => {
1818
<Cell>
1919
<Switch
2020
checked={checkedAsync}
21-
onChange={(value, event) => onChangeAsync(value, event)}
21+
onChange={(value) => onChangeAsync(value)}
2222
/>
2323
</Cell>
2424
<Toast

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,14 @@ import { Cell, Switch, Toast } from '@nutui/nutui-react-taro'
44
const Demo6 = () => {
55
const [value, setValue] = useState(false)
66
const [showToast, setShowToast] = useState(false)
7-
const onChange = (
8-
value: boolean,
9-
event: React.MouseEvent<Element, MouseEvent>
10-
) => {
7+
const onChange = (value: boolean) => {
118
setValue(value)
129
setShowToast(true)
1310
}
1411
return (
1512
<>
1613
<Cell>
17-
<Switch
18-
defaultChecked
19-
onChange={(value, event) => onChange(value, event)}
20-
/>
14+
<Switch defaultChecked onChange={(value) => onChange(value)} />
2115
</Cell>
2216
<Toast
2317
content={`触发了onChange事件,开关状态:${value}`}

src/packages/switch/switch.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
left: calc(100% - $switch-height + $switch-border-width);
4141
}
4242
}
43+
.nut-icon {
44+
width: calc(($switch-height - $switch-border-width * 2) / 2);
45+
height: calc(($switch-height - $switch-border-width * 2) / 2);
46+
color: $switch-active-disabled-background-color;
47+
}
4348
}
4449

4550
&-close {

src/packages/switch/switch.taro.tsx

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import React, { FunctionComponent } from 'react'
1+
import React, { FunctionComponent, useEffect, useState } from 'react'
22

3-
import { ITouchEvent, View } from '@tarojs/components'
3+
import { View } from '@tarojs/components'
44
import classNames from 'classnames'
5+
import { Loading1 } from '@nutui/icons-react-taro'
56
import { ComponentDefaults } from '@/utils/typings'
67
import { usePropsValue } from '@/hooks/use-props-value'
78
import { useRtl } from '@/packages/configprovider/index.taro'
@@ -37,6 +38,12 @@ export const Switch: FunctionComponent<Partial<TaroSwitchProps>> = (props) => {
3738
defaultValue: defaultChecked,
3839
})
3940

41+
useEffect(() => {
42+
changing && setChanging(false)
43+
}, [value])
44+
45+
const [changing, setChanging] = useState(false)
46+
4047
const classes = () => {
4148
return classNames([
4249
classPrefix,
@@ -49,20 +56,16 @@ export const Switch: FunctionComponent<Partial<TaroSwitchProps>> = (props) => {
4956
])
5057
}
5158

52-
const onClick = (
53-
event: React.MouseEvent<Element, MouseEvent> | ITouchEvent
54-
) => {
55-
if (disabled) return
56-
onChange && onChange(!value, event)
59+
const onClick = () => {
60+
if (disabled || changing) return
61+
if (props.onChange) {
62+
setChanging(true)
63+
props.onChange(!value)
64+
}
5765
setValue(!value)
5866
}
5967
return (
60-
<View
61-
className={classes()}
62-
onClick={(e) => onClick(e)}
63-
style={style}
64-
{...rest}
65-
>
68+
<View className={classes()} onClick={onClick} style={style} {...rest}>
6669
<View
6770
className={classNames([
6871
[`${classPrefix}-button`],
@@ -77,8 +80,14 @@ export const Switch: FunctionComponent<Partial<TaroSwitchProps>> = (props) => {
7780
},
7881
])}
7982
>
80-
{!value && !activeText && (
81-
<View className={`${classPrefix}-close-line`} />
83+
{changing ? (
84+
<Loading1 />
85+
) : (
86+
<>
87+
{!value && !activeText && (
88+
<View className={`${classPrefix}-close-line`} />
89+
)}
90+
</>
8291
)}
8392
</View>
8493
{activeText && (

src/packages/switch/switch.tsx

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import React, { FunctionComponent } from 'react'
1+
import React, { FunctionComponent, useEffect, useState } from 'react'
22
import classNames from 'classnames'
3+
import { Loading1 } from '@nutui/icons-react'
34
import { ComponentDefaults } from '@/utils/typings'
45
import { usePropsValue } from '@/hooks/use-props-value'
56
import { useRtl } from '@/packages/configprovider'
@@ -35,6 +36,12 @@ export const Switch: FunctionComponent<Partial<WebSwitchProps>> = (props) => {
3536
defaultValue: defaultChecked,
3637
})
3738

39+
useEffect(() => {
40+
changing && setChanging(false)
41+
}, [value])
42+
43+
const [changing, setChanging] = useState(false)
44+
3845
const classes = () => {
3946
return classNames([
4047
classPrefix,
@@ -47,18 +54,16 @@ export const Switch: FunctionComponent<Partial<WebSwitchProps>> = (props) => {
4754
])
4855
}
4956

50-
const onClick = (event: React.MouseEvent<Element, MouseEvent>) => {
51-
if (disabled) return
52-
onChange && onChange(!value, event)
57+
const onClick = () => {
58+
if (disabled || changing) return
59+
if (props.onChange) {
60+
setChanging(true)
61+
props.onChange(!value)
62+
}
5363
setValue(!value)
5464
}
5565
return (
56-
<div
57-
className={classes()}
58-
onClick={(e) => onClick(e)}
59-
style={style}
60-
{...rest}
61-
>
66+
<div className={classes()} onClick={onClick} style={style} {...rest}>
6267
<div
6368
className={classNames([
6469
[`${classPrefix}-button`],
@@ -73,8 +78,14 @@ export const Switch: FunctionComponent<Partial<WebSwitchProps>> = (props) => {
7378
},
7479
])}
7580
>
76-
{!value && !activeText && (
77-
<div className={`${classPrefix}-close-line`} />
81+
{changing ? (
82+
<Loading1 />
83+
) : (
84+
<>
85+
{!value && !activeText && (
86+
<div className={`${classPrefix}-close-line`} />
87+
)}
88+
</>
7889
)}
7990
</div>
8091
{activeText && (

src/types/spec/switch/base.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { ReactNode } from 'react'
22
import { BaseProps } from '../../base/props'
33

4-
export interface BaseSwitch<EVENT = any> extends BaseProps {
4+
export interface BaseSwitch extends BaseProps {
55
checked: boolean
66
defaultChecked: boolean
77
disabled: boolean
88
activeText: ReactNode
99
inactiveText: ReactNode
10-
onChange: (val: boolean, event: EVENT) => void
10+
onChange: (val: boolean) => void
1111
}

src/types/spec/switch/h5.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import { BaseSwitch } from '../switch/base'
22

3-
export interface WebSwitchProps extends BaseSwitch<React.MouseEvent> {}
3+
export interface WebSwitchProps extends BaseSwitch {}

src/types/spec/switch/taro.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { ITouchEvent } from '@tarojs/components'
21
import { BaseSwitch } from './base'
32

4-
type UnionEvent = React.MouseEvent<Element, MouseEvent> | ITouchEvent
5-
export interface TaroSwitchProps extends BaseSwitch<UnionEvent> {}
3+
export interface TaroSwitchProps extends BaseSwitch {}

0 commit comments

Comments
 (0)