1.0.0 • Published 4 years ago

react-native-credit-card-fullpage-form v1.0.0

Weekly downloads
6
License
MIT
Repository
github
Last release
4 years ago

Credit Card Fullpage Form

A Component to add credit card form to your react native application. For Android & iOS.

Bifurcated from the original design for error correction

Original project: https://github.com/lexor90/react-native-credit-card-input

Code:

// fullpage with credit card flipcard
<CreditCardInput onChange={this._onChange} />
// single-line with horizontal scrolling
<LiteCreditCardInput onChange={this._onChange} /> 

Features

  • iOS 10 form design
  • Customizable credit card that flips as you type to show credit card fields (inspired by: card, react-native-credit-card)
  • Scalable credit card
  • Lite version for smaller screens
  • Credit-card input validations & formatting as you type
  • Integrated with card-validator by braintree to display credit card type
  • Auto-focus next field as you complete correctly one
  • iOS native form design (customizable if you want to)

Usage

npm install react-native-credit-card-fullpage-form
yarn add react-native-credit-card-fullpage-form

then add these lines in your react-native codebase

import { CreditCardInput, LiteCreditCardInput } from "react-native-credit-card-input";

<CreditCardInput onChange={this._onChange} />
// or
<LiteCreditCardInput onChange={this._onChange} />

And then on your onChange handler:

onChange => form => console.log(form);

// will print:
{
  valid: true, // will be true once all fields are "valid" (time to enable the submit button)
  values: { // will be in the sanitized and formatted form
  	number: "4242 4242",
  	expiry: "06/19",
  	cvc: "300",
  	type: "visa", // will be one of [null, "visa", "master-card", "american-express", "diners-club", "discover", "jcb", "unionpay", "maestro"]
  	name: "Sam",
  	postalCode: "34567",
  },
  status: {  // will be one of ["incomplete", "invalid", and "valid"]
	number: "incomplete",
	expiry: "incomplete",
	cvc: "incomplete",
	name: "incomplete", 
	postalCode: "incomplete",
  },
};

// Notes: 
// cvc, name, & postalCode will only be available when the respective props is enabled (e.g. requiresName, requiresCVC)

Props

LiteCreditCardInput

PropertyTypeDescription
autoFocusPropTypes.boolAutomatically focus Card Number field on render
onChangePropTypes.funcReceives a formData object every time the form changes
onFocusPropTypes.funcReceives the name of currently focused field
placeholdersPropTypes.objectDefaults to { number: "1234 5678 1234 5678", expiry: "MM/YY", cvc: "CVC" }
inputStyleText.propTypes.styleStyle for credit-card form's textInput
validColorPropTypes.stringColor that will be applied for valid text input. Defaults to: "{inputStyle.color}"
invalidColorPropTypes.stringColor that will be applied for invalid text input. Defaults to: "red"
placeholderColorPropTypes.stringColor that will be applied for text input placeholder. Defaults to: "gray"

NOTES

LiteCreditCardInput does not support requiresName, requiresCVC, and requiresPostalCode at the moment, PRs are welcome :party:

CreditCardInput

PropertyTypeDescription
autoFocusPropTypes.boolAutomatically focus Card Number field on render
onChangePropTypes.funcReceives a formData object every time the form changes
onFocusPropTypes.funcReceives the name of currently focused field
labelsPropTypes.objectDefaults to { number: "CARD NUMBER", expiry: "EXPIRY", cvc: "CVC/CCV" }
placeholdersPropTypes.objectDefaults to { number: "1234 5678 1234 5678", expiry: "MM/YY", cvc: "CVC" }
cardScalePropTypes.numberScales the credit-card view.Defaults to 1, which translates to { width: 300, height: 190 }
cardFontFamilyPropTypes.stringFont family for the CreditCardView, works best with monospace fonts. Defaults to Courier (iOS) or monospace (android)
cardImageFrontPropTypes.numberImage for the credit-card view e.g. require("./card.png")
cardImageFrontPropTypes.numberImage for the credit-card view e.g. require("./card.png")
labelStyleText.propTypes.styleStyle for credit-card form's labels
inputStyleText.propTypes.styleStyle for credit-card form's textInput
inputContainerStyleView.propTypes.styleStyle for textInput's container Defaults to: { borderBottomWidth: 1, borderBottomColor: "black" }
validColorPropTypes.stringColor that will be applied for valid text input. Defaults to: "{inputStyle.color}"
invalidColorPropTypes.stringColor that will be applied for invalid text input. Defaults to: "red"
placeholderColorPropTypes.stringColor that will be applied for text input placeholder. Defaults to: "gray"
requiresNamePropTypes.boolShows cardholder's name field Default to false
requiresCVCPropTypes.boolShows CVC field Default to true
requiresPostalCodePropTypes.boolShows postalCode field Default to false
validatePostalCodePropTypes.funcFunction to validate postalCode, expects incomplete, valid, or invalid as return values

Methods

setValues

Set values into credit card form

	// sets 4242 on credit card number field
	// other fields will stay unchanged
	this.refs.CCInput.setValues({ number: "4242" });

Known issues: clearing a field e.g. setValues({ expiry: "" }) will trigger the logic to move to previous field and trigger other kind of weird side effects. PR plz

Focus

focus on to specified field

	// focus to expiry field
	this.refs.CCInput.focus("expiry");

Missing Something? Something is not working?

  • Open a GitHub issue,
  • Send a pull request,