-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #181 from appfolio/feature/credit-card-number-field
Credit Card Validation Components
- Loading branch information
Showing
17 changed files
with
465 additions
and
6,175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ coverage | |
node_modules | ||
dist | ||
*.log | ||
yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import React, { Component, PropTypes } from 'react'; | ||
import { Col, Row } from 'reactstrap'; | ||
import { Select } from '../'; | ||
|
||
const today = new Date(); | ||
const MONTHS = [ | ||
'January', 'February', 'March', 'April', 'May', 'June', | ||
'July', 'August', 'September', 'October', 'November', 'December', | ||
]; | ||
const YEARS = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | ||
.map(offset => today.getFullYear() + offset); | ||
|
||
// eslint-disable-next-line arrow-body-style | ||
const monthOptions = MONTHS.map((label, index) => ({ label, value: index + 1 })); | ||
// eslint-disable-next-line arrow-body-style | ||
const yearsOptions = YEARS.map(year => ({ label: year, value: year })); | ||
|
||
export default class CreditCardExpiration extends Component { | ||
onMonthSelection = (option) => { | ||
const month = option && option.value || CreditCardExpiration.defaultProps.month; | ||
this.props.onChange({ month, year: this.props.year }); | ||
} | ||
onYearSelection = (option) => { | ||
const year = option && option.value || CreditCardExpiration.defaultProps.year; | ||
this.props.onChange({ year, month: this.props.month }); | ||
} | ||
|
||
render() { | ||
return ( | ||
<Row className="no-gutters"> | ||
<Col xs={7} sm={8}> | ||
<Select | ||
name={this.props.monthName} | ||
placeholder="Month" | ||
value={this.props.month} | ||
options={monthOptions} | ||
onChange={this.onMonthSelection} | ||
className="pr-3" | ||
/> | ||
</Col> | ||
<Col xs={5} sm={4}> | ||
<Select | ||
name={this.props.yearName} | ||
placeholder="Year" value={this.props.year} | ||
options={yearsOptions} onChange={this.onYearSelection} | ||
/> | ||
</Col> | ||
</Row> | ||
); | ||
} | ||
} | ||
|
||
CreditCardExpiration.defaultProps = { | ||
month: null, | ||
monthName: 'month', | ||
year: null, | ||
yearName: 'year', | ||
|
||
onChange: () => true, | ||
}; | ||
|
||
CreditCardExpiration.propTypes = { | ||
month: PropTypes.number, | ||
monthName: PropTypes.string, | ||
year: PropTypes.number, | ||
yearName: PropTypes.string, | ||
|
||
onChange: PropTypes.func, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import React, { Component, PropTypes } from 'react'; | ||
import { | ||
Row, Col, PatternInput, ValidatedFormGroup, | ||
CreditCardExpiration, CreditCardNumber, | ||
} from '../'; | ||
|
||
export default class CreditCardInput extends Component { | ||
handleCardCVVChange = (event, { value, isValid }) => { | ||
this.props.onChange({ cardCVV: value, cardCVVIsValid: isValid }); | ||
} | ||
handleCardExpirationChange = ({ month, year }) => { | ||
const TODAY = new Date(); | ||
const expirationIsValid = new Date(year, month) >= TODAY; | ||
this.props.onChange({ expirationMonth: month, expirationYear: year, expirationIsValid }); | ||
} | ||
handleCardNumberChange = (cardNumber, cardNumberIsValid) => { | ||
this.props.onChange({ cardNumber, cardNumberIsValid }); | ||
} | ||
|
||
render() { | ||
const { | ||
cardNumber, cardCVV, errors, | ||
expirationMonth: month, expirationYear: year, | ||
} = this.props; | ||
|
||
return ( | ||
<Row> | ||
<Col xs={12} sm={6}> | ||
<Row className="no-gutters"> | ||
<Col xs={9} sm={10}> | ||
<ValidatedFormGroup className="pr-3" error={errors.cardNumber}> | ||
<CreditCardNumber | ||
value={cardNumber} | ||
placeholder="Card Number" | ||
onChange={this.handleCardNumberChange} | ||
/> | ||
</ValidatedFormGroup> | ||
</Col> | ||
<Col xs={3} sm={2}> | ||
<ValidatedFormGroup error={errors.cardCVV}> | ||
<PatternInput | ||
name="cardCVV" | ||
placeholder="CVV" | ||
type="text" | ||
value={cardCVV} | ||
pattern={/^[0-9]{0,5}$/} | ||
onChange={this.handleCardCVVChange} | ||
/> | ||
</ValidatedFormGroup> | ||
</Col> | ||
</Row> | ||
</Col> | ||
<Col xs={12} sm={6}> | ||
<ValidatedFormGroup error={errors.expiration}> | ||
<CreditCardExpiration | ||
month={month} | ||
monthName="expiration" | ||
year={year} | ||
yearName="expiration" | ||
onChange={this.handleCardExpirationChange} | ||
/> | ||
</ValidatedFormGroup> | ||
</Col> | ||
</Row> | ||
); | ||
} | ||
} | ||
|
||
export const creditCardPropTypes = { | ||
cardNumber: PropTypes.string, | ||
cardCVV: PropTypes.string, | ||
errors: PropTypes.object, | ||
|
||
expirationMonth: (props, propName) => { | ||
const value = props[propName]; | ||
if (value === null) return null; | ||
return typeof value === 'number' && value >= 1 && value <= 12 | ||
? null : new Error('Expiration Month must be a number between 1 and 12'); | ||
}, | ||
expirationYear: (props, propName) => { | ||
const value = props[propName]; | ||
if (value === null) return null; | ||
|
||
const TODAY = new Date(); | ||
return typeof value === 'number' && value >= TODAY.getFullYear() | ||
? null : new Error('Expiration Year must be this year or later'); | ||
}, | ||
|
||
onChange: PropTypes.func, | ||
}; | ||
|
||
|
||
CreditCardInput.propTypes = creditCardPropTypes; | ||
CreditCardInput.defaultProps = { | ||
cardNumber: '', | ||
cardCVV: '', | ||
errors: {}, | ||
expirationMonth: null, | ||
expirationYear: null, | ||
|
||
onChange: () => {}, | ||
}; |
Oops, something went wrong.