1
- import React , { useState , useEffect } from 'react' ;
2
- import ReactDOM from 'react-dom' ;
1
+ import React , { useState , useEffect , ReactElement , createRef } from 'react' ;
3
2
4
3
import classnames from '../../utils/classNames' ;
5
4
@@ -20,6 +19,9 @@ export interface IProps {
20
19
minus ?: Boolean ;
21
20
size ?: number ;
22
21
loading ?: Boolean ;
22
+ tag ?: ReactElement ;
23
+ onChange : Function ;
24
+ asyncChange ?: Function | any ;
23
25
}
24
26
25
27
export default function Stepper ( {
@@ -30,17 +32,20 @@ export default function Stepper({
30
32
disableInput,
31
33
size,
32
34
theme,
33
- loading
35
+ loading,
36
+ onChange,
37
+ asyncChange
34
38
} : IProps ) {
35
39
const [ value , setValue ] = useState ( 0 ) ;
36
40
const [ isMinus , setIsMinus ] = useState ( false ) ;
37
41
const [ isPlus , setIsPlus ] = useState ( false ) ;
38
42
const [ isInput , setIsInput ] = useState ( false ) ;
39
- const animationDiv = document . getElementById ( 'loading' ) ;
40
- const animationBackground = document . getElementById ( 'loading-background' ) ;
43
+
41
44
const [ minusBt , setMinusBt ] = useState ( { } ) ;
42
45
const [ plusBt , setPlusBt ] = useState ( { } ) ;
43
46
const [ inputBt , setInputBt ] = useState ( { } ) ;
47
+ const animationDiv = createRef < HTMLDivElement > ( ) ;
48
+ const animationBackgroundDiv = createRef < HTMLDivElement > ( ) ;
44
49
45
50
const handleIncrementBtProps = {
46
51
className : classnames ( baseClass , [ { disabled } , { theme } ] ) ,
@@ -57,68 +62,82 @@ export default function Stepper({
57
62
58
63
const handleIncrement = ( ) => {
59
64
if ( loading ) {
60
- ReactDOM . findDOMNode ( animationDiv ) . style . opacity = 1 ;
61
- ReactDOM . findDOMNode ( animationBackground ) . style . opacity = 1 ;
65
+ const aniNode = animationDiv . current ;
66
+ const aniBgNode = animationBackgroundDiv . current ;
67
+ if ( aniNode && aniBgNode ) {
68
+ aniNode . style . opacity = '1' ;
69
+ aniBgNode . style . opacity = '1' ;
70
+ }
71
+
62
72
const handlePlus = ( ) => {
63
- if ( step ) {
64
- setValue ( value + step ) ;
65
- ReactDOM . findDOMNode ( animationDiv ) . style . opacity = 0 ;
66
- ReactDOM . findDOMNode ( animationBackground ) . style . opacity = 0 ;
67
- } else {
68
- setValue ( value + 1 ) ;
69
- ReactDOM . findDOMNode ( animationDiv ) . style . opacity = 0 ;
70
- ReactDOM . findDOMNode ( animationBackground ) . style . opacity = 0 ;
73
+ const nextValue = value + ( step || 1 ) ;
74
+ setValue ( nextValue ) ;
75
+
76
+ if ( aniNode && aniBgNode ) {
77
+ aniNode . style . opacity = '0' ;
78
+ aniBgNode . style . opacity = '0' ;
71
79
}
80
+ onChange ( nextValue ) ;
81
+ asyncChange ( ) ;
72
82
} ;
73
-
74
- setTimeout ( handlePlus , 2000 ) ;
75
- } else if ( step ) {
76
- setValue ( value + step ) ;
83
+ setTimeout ( handlePlus , 1000 ) ;
77
84
} else {
78
- setValue ( value + 1 ) ;
85
+ const nextValue = value + ( step || 1 ) ;
86
+ setValue ( nextValue ) ;
87
+ onChange ( nextValue ) ;
79
88
}
80
89
} ;
90
+
81
91
const handleDecrement = ( ) => {
82
- const canMinus = value - step ;
92
+ setIsPlus ( false ) ;
83
93
if ( loading ) {
84
- ReactDOM . findDOMNode ( animationDiv ) . style . opacity = 1 ;
85
- ReactDOM . findDOMNode ( animationBackground ) . style . opacity = 1 ;
86
-
87
- const Decrement = ( ) => {
88
- if ( step ) {
89
- setValue ( value - step ) ;
90
- ReactDOM . findDOMNode ( animationDiv ) . style . opacity = 0 ;
91
- ReactDOM . findDOMNode ( animationBackground ) . style . opacity = 0 ;
92
- } else {
93
- setValue ( value - 1 ) ;
94
- ReactDOM . findDOMNode ( animationDiv ) . style . opacity = 0 ;
95
- ReactDOM . findDOMNode ( animationBackground ) . style . opacity = 0 ;
94
+ const aniNode = animationDiv . current ;
95
+ const aniBgNode = animationBackgroundDiv . current ;
96
+ if ( aniNode && aniBgNode ) {
97
+ aniNode . style . opacity = '1' ;
98
+ aniBgNode . style . opacity = '1' ;
99
+ }
100
+
101
+ const decrement = ( ) => {
102
+ const nextValue = value - ( step || 1 ) ;
103
+ setValue ( nextValue ) ;
104
+ onChange ( nextValue ) ;
105
+ if ( aniNode && aniBgNode ) {
106
+ aniNode . style . opacity = '0' ;
107
+ aniBgNode . style . opacity = '0' ;
96
108
}
97
109
} ;
98
- setTimeout ( Decrement , 2000 ) ;
99
- } else if ( step ) {
100
- if ( canMinus >= 0 ) {
101
- setValue ( value - step ) ;
102
- }
110
+ setTimeout ( decrement , 1000 ) ;
103
111
} else {
104
- if ( value > 0 ) {
105
- setValue ( value - 1 ) ;
112
+ const nextValue = value - ( step || 1 ) ;
113
+ if ( nextValue >= 0 ) {
114
+ setValue ( nextValue ) ;
115
+ onChange ( nextValue ) ;
106
116
}
107
117
}
108
118
} ;
119
+
109
120
const handleInputChange = ( e ) => {
110
121
const result = e . target . value ;
111
122
if ( loading ) {
112
- ReactDOM . findDOMNode ( animationDiv ) . style . opacity = 1 ;
113
- ReactDOM . findDOMNode ( animationBackground ) . style . opacity = 1 ;
123
+ const aniNode = animationDiv . current ;
124
+ const aniBgNode = animationBackgroundDiv . current ;
125
+ if ( aniNode && aniBgNode ) {
126
+ aniNode . style . opacity = '1' ;
127
+ aniBgNode . style . opacity = '1' ;
128
+ }
114
129
const changeInput = ( ) => {
115
130
setValue ( Number ( result ) ) ;
116
- ReactDOM . findDOMNode ( animationDiv ) . style . opacity = 0 ;
117
- ReactDOM . findDOMNode ( animationBackground ) . style . opacity = 0 ;
131
+ onChange ( Number ( result ) ) ;
132
+ if ( aniNode && aniBgNode ) {
133
+ aniNode . style . opacity = '0' ;
134
+ aniBgNode . style . opacity = '0' ;
135
+ }
118
136
} ;
119
137
setTimeout ( changeInput , 2000 ) ;
120
138
} else {
121
139
setValue ( Number ( e . target . value ) ) ;
140
+ onChange ( Number ( e . target . value ) ) ;
122
141
}
123
142
} ;
124
143
@@ -162,6 +181,7 @@ export default function Stepper({
162
181
opacity : '0.2'
163
182
} ;
164
183
setPlusBt ( btStyle ) ;
184
+ setIsPlus ( true ) ;
165
185
} else {
166
186
const btnStyle = {
167
187
cursor : 'pointer' ,
@@ -181,6 +201,7 @@ export default function Stepper({
181
201
} else if ( value === max ) {
182
202
const btStyle = { cursor : 'not-allowed' , opacity : '0.2' } ;
183
203
setPlusBt ( btStyle ) ;
204
+ setIsPlus ( true ) ;
184
205
} else {
185
206
const btnStyle = {
186
207
cursor : 'pointer'
@@ -197,7 +218,6 @@ export default function Stepper({
197
218
Object . assign ( inputProps , { disabled } ) ;
198
219
}
199
220
} , [ disableInput ] ) ;
200
-
201
221
return (
202
222
< div className = 'step-container' >
203
223
< button
@@ -217,8 +237,8 @@ export default function Stepper({
217
237
style = { inputBt }
218
238
/>
219
239
{ loading && (
220
- < div id = 'loading-background' className = 'load' >
221
- < div id = 'loading' className = 'load-background' />
240
+ < div ref = { animationBackgroundDiv } className = 'load' >
241
+ < div ref = { animationDiv } className = 'load-background' />
222
242
</ div >
223
243
) }
224
244
< button
0 commit comments