Skip to content

Commit 151a6b5

Browse files
committed
add async stepper
1 parent 202ce48 commit 151a6b5

File tree

3 files changed

+78
-15
lines changed

3 files changed

+78
-15
lines changed

src/components/Stepper/index.scss

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
@import '../../styles/typography.scss';
44
@import '../../styles/opacity.scss';
55
@import '../../styles/variables.scss';
6+
@keyframes donut-spin {
7+
0% {
8+
transform: rotate(0deg);
9+
}
10+
11+
100% {
12+
transform: rotate(360deg);
13+
}
14+
}
15+
16+
17+
618

719
$baseClass: 'vant-stepper';
820
.step-container {
@@ -25,20 +37,27 @@ $baseClass: 'vant-stepper';
2537
outline: none;
2638
background-color: rgba(128, 128, 128, 0.137);
2739
border: none;
28-
display: flex;
29-
justify-content: center;
30-
align-items: center;
31-
text-align: center;
3240
font-weight: 100;
3341
}
42+
.loading{
43+
display: inline-block;
44+
border: 4px solid rgba(0, 0, 0, 0.1);
45+
border-left-color: grey;
46+
border-radius: 50%;
47+
width: 65px;
48+
height: 65px;
49+
animation: donut-spin 1.2s linear infinite;
50+
position: absolute;
51+
left: 47%;
52+
top: 35%;
53+
opacity: 0;
54+
}
3455
}
3556

3657
button.#{$baseClass} {
37-
width: 30px;
38-
height: 30px;
39-
display: flex;
40-
justify-content: center;
41-
align-items: center;
58+
width: 50px;
59+
height: 50px;
60+
4261

4362
&__disabled {
4463
cursor: not-allowed;
@@ -50,8 +69,8 @@ button.#{$baseClass} {
5069
}
5170
}
5271
input.#{$baseClass} {
53-
width: 35px;
54-
height: 30px;
72+
width: 55px;
73+
height: 50px;
5574

5675
&__disabled {
5776
cursor: not-allowed;
@@ -60,4 +79,11 @@ input.#{$baseClass} {
6079
&__theme {
6180
border-radius: 50%;
6281
}
82+
83+
6384
}
85+
86+
87+
88+
89+

src/components/Stepper/index.stories.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const RangeStepper = () => (
3333

3434
export const SizeStepper = () => (
3535
<div className='container stepper'>
36-
<Stepper size={50} />
36+
<Stepper size={70} />
3737
</div>
3838
);
3939

@@ -48,3 +48,9 @@ export const DisableInputStepper = () => (
4848
<Stepper disableInput />
4949
</div>
5050
);
51+
52+
export const AsyncStepper = () => (
53+
<div className='container stepper'>
54+
<Stepper loading />
55+
</div>
56+
);

src/components/Stepper/index.tsx

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { useState, useEffect } from 'react';
2+
import ReactDOM from 'react-dom';
23

34
import classnames from '../../utils/classNames';
45

@@ -18,6 +19,7 @@ export interface IProps {
1819
plus?: Boolean;
1920
minus?: Boolean;
2021
size?: number;
22+
loading?: Boolean;
2123
}
2224

2325
export default function Stepper({
@@ -27,12 +29,15 @@ export default function Stepper({
2729
max,
2830
disableInput,
2931
size,
30-
theme
32+
theme,
33+
loading
3134
}: IProps) {
3235
const [value, setValue] = useState(0);
3336
const [minus, setMinus] = useState(false);
3437
const [plus, setPlus] = useState(false);
3538
const [Input, setInput] = useState(false);
39+
const animationDiv = document.getElementById('loading');
40+
3641
const plusBtProps = {
3742
className: classnames(baseClass, [{ disabled }, { theme }]),
3843
style: {}
@@ -47,15 +52,40 @@ export default function Stepper({
4752
};
4853

4954
const handleIncrement = () => {
50-
if (step) {
55+
if (loading) {
56+
ReactDOM.findDOMNode(animationDiv).style.opacity = 1;
57+
const handlePlus = () => {
58+
if (step) {
59+
setValue(value + step);
60+
ReactDOM.findDOMNode(animationDiv).style.opacity = 0;
61+
} else {
62+
setValue(value + 1);
63+
ReactDOM.findDOMNode(animationDiv).style.opacity = 0;
64+
}
65+
};
66+
67+
setTimeout(handlePlus, 2000);
68+
} else if (step) {
5169
setValue(value + step);
5270
} else {
5371
setValue(value + 1);
5472
}
5573
};
5674
const handleMinus = () => {
5775
const canMinus = value - step;
58-
if (step) {
76+
if (loading) {
77+
ReactDOM.findDOMNode(animationDiv).style.opacity = 1;
78+
const handleMinus = () => {
79+
if (step) {
80+
setValue(value - step);
81+
ReactDOM.findDOMNode(animationDiv).style.opacity = 0;
82+
} else {
83+
setValue(value - 1);
84+
ReactDOM.findDOMNode(animationDiv).style.opacity = 0;
85+
}
86+
};
87+
setTimeout(handleMinus, 2000);
88+
} else if (step) {
5989
if (canMinus >= 0) {
6090
setValue(value - step);
6191
}
@@ -167,6 +197,7 @@ export default function Stepper({
167197
<button onClick={handleIncrement} {...plusBtProps} disabled={plus}>
168198
+
169199
</button>
200+
{loading && <div id='loading' className='loading' />}
170201
</div>
171202
);
172203
}

0 commit comments

Comments
 (0)