Skip to content

Commit b14537c

Browse files
committed
Remove state and deprecated lifecycles
1 parent 6b289b3 commit b14537c

File tree

1 file changed

+35
-62
lines changed

1 file changed

+35
-62
lines changed

src/index.js

+35-62
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@ class ReactCreditCards extends React.Component {
66
constructor(props) {
77
super(props);
88

9-
this.state = {
10-
type: {
11-
name: 'unknown',
12-
maxLength: 16,
13-
},
14-
};
15-
169
this.setCards();
1710
}
1811

@@ -54,41 +47,31 @@ class ReactCreditCards extends React.Component {
5447
preview: false,
5548
};
5649

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;
7052

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+
}
7358
}
7459

75-
if (acceptedCards.toString() !== nextAcceptedCards.toString()) {
76-
this.setCards(nextProps);
60+
if (prevProps.acceptedCards.toString() !== acceptedCards.toString()) {
61+
this.setCards();
7762
}
7863
}
7964

8065
get issuer() {
81-
const { type } = this.state;
8266
const { issuer, preview } = this.props;
8367

84-
return preview && issuer ? issuer.toLowerCase() : type.issuer;
68+
return preview && issuer ? issuer.toLowerCase() : this.options.issuer;
8569
}
8670

8771
get number() {
88-
const { type } = this.state;
8972
const { number, preview } = this.props;
9073

91-
let maxLength = preview ? 19 : type.maxLength;
74+
let maxLength = preview ? 19 : this.options.maxLength;
9275
let nextNumber = typeof number === 'number' ? number.toString() : number.replace(/[A-Za-z]| /g, '');
9376

9477
if (isNaN(parseInt(nextNumber, 10)) && !preview) {
@@ -156,55 +139,45 @@ class ReactCreditCards extends React.Component {
156139
return `${month}/${year}`;
157140
}
158141

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';
181145

182146
let maxLength = 16;
183147

184-
if (type === 'amex') {
148+
if (issuer === 'amex') {
185149
maxLength = 15;
186150
}
187-
else if (type === 'dinersclub') {
151+
else if (issuer === 'dinersclub') {
188152
maxLength = 14;
189153
}
190-
else if (['hipercard', 'mastercard', 'visa'].includes(type)) {
154+
else if (['hipercard', 'mastercard', 'visa'].includes(issuer)) {
191155
maxLength = 19;
192156
}
193157

194-
const typeState = {
195-
issuer: type,
158+
return {
159+
issuer,
196160
maxLength,
197161
};
198-
const isValid = Payment.fns.validateCardNumber(number);
162+
}
199163

200-
this.setState({
201-
type: typeState,
202-
});
164+
setCards() {
165+
const { acceptedCards } = this.props;
166+
let newCardArray = [];
203167

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+
});
207175
}
176+
else {
177+
newCardArray = newCardArray.concat(Payment.getCardArray());
178+
}
179+
180+
Payment.setCardArray(newCardArray);
208181
}
209182

210183
render() {

0 commit comments

Comments
 (0)