1.0.1 • Published 4 years ago

@rajeshsmallarc/react-native-creditcard v1.0.1

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

This project is a modified version of this one, https://www.npmjs.com/package/react-native-input-credit-card

React Native CreditCard

Credit Card Input for React Native Project

Code:

<CreditCardInput onChange={this._onChange} />

Features

  • credit-card 💳 (inspired by: card, react-native-input-credit-card)
  • Credit-card input validations & formatting while you're typing
  • Form is fully navigatable using keypad
  • Works on both Android and iOS

Usage

npm i @rajeshsmallarc/react-native-creditcard --save

then add these lines in your react-native codebase

import { CreditCardInput } from "@rajeshsmallarc/react-native-creditcard";

<CreditCardInput onChange={this._onChange} />

And then on your onChange handler:

_onChange => card => console.log(card);

// 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 4242 4242",
  	expiry: "12/25",
  	cvc: "123",
  	type: "visa", // will be one of [null, "visa", "master-card", "american-express", "diners-club", "discover", "jcb", "unionpay", "maestro"]
  	name: "Rajesh",
  	postalCode: "123456",
  },
  status: {  // will be one of ["incomplete", "invalid", and "valid"]
	number: "incomplete",
	expiry: "incomplete",
	cvc: "incomplete",
	name: "incomplete", 
	postalCode: "incomplete",
  },
};

// Notes: 
// You can enable/disable cvc, name using requiresCVC, requiresName, requiresPostalCode)

// Example: 

<CreditCardInput 
	onChange={this._onChange} 
	requiresName
	requiresPostalCode
/>

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")
cardImageBackPropTypes.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
inputContainerStyleViewPropTypes.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
allowScrollPropTypes.boolenables horizontal scrolling on CreditCardInput Defaults to false
cardBrandIconsPropTypes.objectbrand icons for CardView. see ./src/Icons.js for details
additionalInputsPropsPropTypes.objectOf(TextInput.propTypes)An object with Each key of the object corresponding to the name of the field. Allows you to change all props documented in RN TextInput.

Note on additionalInputsProps

additionalInputsProps gives you more control over the inputs in CreditCardInput. An example object is as follows:

addtionalInputsProps = {
  name: {
    defaultValue: 'my name',
    maxLength: 40,
  },
  postalCode: {
    returnKeyType: 'go',
  },
};

The above would set the default value of the name field to my name and limit the input to a maximum of 40 character. In addition, it would set the returnKeyType of the postalcode field to go.

Methods

setValues

Set values into credit card form

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

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 or Send a pull request

TODO

  • Rewrite using hooks aiming performance and simplicity
  • Add unit and integration tests