@@ -6,13 +6,6 @@ class ReactCreditCards extends React.Component {
6
6
constructor ( props ) {
7
7
super ( props ) ;
8
8
9
- this . state = {
10
- type : {
11
- name : 'unknown' ,
12
- maxLength : 16 ,
13
- } ,
14
- } ;
15
-
16
9
this . setCards ( ) ;
17
10
}
18
11
@@ -54,41 +47,31 @@ class ReactCreditCards extends React.Component {
54
47
preview : false ,
55
48
} ;
56
49
57
- componentDidMount ( ) {
58
- const { number } = this . props ;
59
-
60
- this . updateType ( number ) ;
61
- }
62
-
63
- componentWillReceiveProps ( nextProps ) {
64
- const { acceptedCards, number } = this . props ;
65
-
66
- const {
67
- acceptedCards : nextAcceptedCards ,
68
- number : nextNumber ,
69
- } = nextProps ;
50
+ componentDidUpdate ( prevProps ) {
51
+ const { acceptedCards, callback, number } = this . props ;
70
52
71
- if ( number !== nextNumber ) {
72
- this . updateType ( nextNumber ) ;
53
+ if ( prevProps . number !== number ) {
54
+ /* istanbul ignore else */
55
+ if ( typeof callback === 'function' ) {
56
+ callback ( this . options , Payment . fns . validateCardNumber ( number ) ) ;
57
+ }
73
58
}
74
59
75
- if ( acceptedCards . toString ( ) !== nextAcceptedCards . toString ( ) ) {
76
- this . setCards ( nextProps ) ;
60
+ if ( prevProps . acceptedCards . toString ( ) !== acceptedCards . toString ( ) ) {
61
+ this . setCards ( ) ;
77
62
}
78
63
}
79
64
80
65
get issuer ( ) {
81
- const { type } = this . state ;
82
66
const { issuer, preview } = this . props ;
83
67
84
- return preview && issuer ? issuer . toLowerCase ( ) : type . issuer ;
68
+ return preview && issuer ? issuer . toLowerCase ( ) : this . options . issuer ;
85
69
}
86
70
87
71
get number ( ) {
88
- const { type } = this . state ;
89
72
const { number, preview } = this . props ;
90
73
91
- let maxLength = preview ? 19 : type . maxLength ;
74
+ let maxLength = preview ? 19 : this . options . maxLength ;
92
75
let nextNumber = typeof number === 'number' ? number . toString ( ) : number . replace ( / [ A - Z a - z ] | / g, '' ) ;
93
76
94
77
if ( isNaN ( parseInt ( nextNumber , 10 ) ) && ! preview ) {
@@ -156,55 +139,45 @@ class ReactCreditCards extends React.Component {
156
139
return `${ month } /${ year } ` ;
157
140
}
158
141
159
- setCards ( props = this . props ) {
160
- const { acceptedCards } = props ;
161
- let newCardArray = [ ] ;
162
-
163
- if ( acceptedCards . length ) {
164
- Payment . getCardArray ( )
165
- . forEach ( d => {
166
- if ( acceptedCards . includes ( d . type ) ) {
167
- newCardArray . push ( d ) ;
168
- }
169
- } ) ;
170
- }
171
- else {
172
- newCardArray = newCardArray . concat ( Payment . getCardArray ( ) ) ;
173
- }
174
-
175
- Payment . setCardArray ( newCardArray ) ;
176
- }
177
-
178
- updateType ( number ) {
179
- const { callback } = this . props ;
180
- const type = Payment . fns . cardType ( number ) || 'unknown' ;
142
+ get options ( ) {
143
+ const { number } = this . props ;
144
+ const issuer = Payment . fns . cardType ( number ) || 'unknown' ;
181
145
182
146
let maxLength = 16 ;
183
147
184
- if ( type === 'amex' ) {
148
+ if ( issuer === 'amex' ) {
185
149
maxLength = 15 ;
186
150
}
187
- else if ( type === 'dinersclub' ) {
151
+ else if ( issuer === 'dinersclub' ) {
188
152
maxLength = 14 ;
189
153
}
190
- else if ( [ 'hipercard' , 'mastercard' , 'visa' ] . includes ( type ) ) {
154
+ else if ( [ 'hipercard' , 'mastercard' , 'visa' ] . includes ( issuer ) ) {
191
155
maxLength = 19 ;
192
156
}
193
157
194
- const typeState = {
195
- issuer : type ,
158
+ return {
159
+ issuer,
196
160
maxLength,
197
161
} ;
198
- const isValid = Payment . fns . validateCardNumber ( number ) ;
162
+ }
199
163
200
- this . setState ( {
201
- type : typeState ,
202
- } ) ;
164
+ setCards ( ) {
165
+ const { acceptedCards } = this . props ;
166
+ let newCardArray = [ ] ;
203
167
204
- /* istanbul ignore else */
205
- if ( typeof callback === 'function' ) {
206
- callback ( typeState , isValid ) ;
168
+ if ( acceptedCards . length ) {
169
+ Payment . getCardArray ( )
170
+ . forEach ( d => {
171
+ if ( acceptedCards . includes ( d . type ) ) {
172
+ newCardArray . push ( d ) ;
173
+ }
174
+ } ) ;
207
175
}
176
+ else {
177
+ newCardArray = newCardArray . concat ( Payment . getCardArray ( ) ) ;
178
+ }
179
+
180
+ Payment . setCardArray ( newCardArray ) ;
208
181
}
209
182
210
183
render ( ) {
0 commit comments