1.1.5 • Published 5 years ago

react-credit-card-input v1.1.5

Weekly downloads
5,004
License
MIT
Repository
github
Last release
5 years ago

React Credit Card Input

A credit/debit card input field for React

Example

Click here for an interactive demo

npm.io

Install

$ npm install --save react-credit-card-input styled-components

Usage

import CreditCardInput from 'react-credit-card-input';

<CreditCardInput
  cardNumberInputProps={{ value: cardNumber, onChange: this.handleCardNumberChange }}
  cardExpiryInputProps={{ value: expiry, onChange: this.handleCardExpiryChange }}
  cardCVCInputProps={{ value: cvc, onChange: this.handleCardCVCChange }}
  fieldClassName="input"
/>

Available props

PropTypeDefault valueDescription
cardNumberInputPropsobject (optional){}Card number input element props
(e.g. { value: cardNumber, onChange: this.handleCardNumberChange, onBlur: this.handleCardNumberBlur, onError: this.handleCardNumberError } )
cardExpiryInputPropsobject (optional){}Card expiry date input element props
(e.g. { value: expiry, onChange: this.handleCardExpiryChange, onBlur: this.handleCardExpiryBlur, onError: this.handleCardExpiryError })
cardCVCInputPropsobject (optional){}Card CVC input element props
(e.g. { value: cvc, onChange: this.handleCardCVCChange, onBlur: this.handleCardCVCBlur, onError: this.handleCardCVCError })
cardNumberInputRendererFunction (view input renderer props below)Card number input renderer
cardExpiryInputRendererFunction (view input renderer props below)Card expiry date input renderer
cardCVCInputRendererFunction (view input renderer props below)Card CVC input renderer
onErrorFunction (optional)Invokes on field errors. Recieves errorMessage argument.
cardImageClassNamestring (optional)''Class name for the card type image
cardImageStyleobject (optional){}Style for the card type image
containerClassNamestring (optional)''Class name for the field container
containerStyleobject (optional){}Style for the field container
dangerTextClassNamestring (optional)''Class name for the danger text
dangerTextStyleobject (optional){}Style for the danger text container
fieldClassNamestring (optional)''Class name for the field
fieldStyleobject (optional){}Style for the field
inputClassNamestring (optional)''Class name for the inputs
inputStyleobject (optional){}Style for the inputs
invalidClassNamestring (optional)'is-invalid'Class name for the invalid field
invalidStyleobject (optional){}Style for the invalid field
inputComponentstring, function, class (optional)'input'Input component for the card number, expiry and CVC input
customTextLabelsobject (optional){}Object that defines custom label values.

Input renderer props

PropTypeDescription
handleCardNumberChangeFunctionHandle card number change.
handleCardNumberBlurFunctionHandle card number blur.
handleCardExpiryChangeFunctionHandle card expiry change.
handleCardExpiryBlurFunctionHandle card expiry blur.
handleCardCVCChangeFunctionHandle card CVC change.
handleCardCVCBlurFunctionHandle card CVC blur.
propsObjectInput component props

Error handling

<CreditCardInput
  onError={({ inputName, err }) => console.log(`credit card input error: ${err}`)}
  cardCVCInputProps={{
    onBlur: e => console.log('cvc blur', e),
    onChange: e => console.log('cvc change', e),
    onError: err => console.log(`cvc error: ${err}`)
  }}
  cardExpiryInputProps={{
    onBlur: e => console.log('expiry blur', e),
    onChange: e => console.log('expiry change', e),
    onError: err => console.log(`expiry error: ${err}`)
  }}
  cardNumberInputProps={{
    onBlur: e => console.log('number blur', e),
    onChange: e => console.log('number change', e),
    onError: err => console.log(`number error: ${err}`)
  }}
/>
/>

Custom input renderer usage

<CreditCardInput
  cardCVCInputRenderer={({ handleCardCVCChange, props }) => (
    <input
      {...props}
      onChange={handleCardCVCChange(e => console.log('cvc change', e))}
    />
  )}
  cardExpiryInputRenderer={({ handleCardExpiryChange, props }) => (
    <input
      {...props}
      onChange={handleCardExpiryChange(e =>
        console.log('expiry change', e)
      )}
    />
  )}
  cardNumberInputRenderer={({ handleCardNumberChange, props }) => (
    <input
      {...props}
      onChange={handleCardNumberChange(e =>
        console.log('number change', e)
      )}
    />
  )}
/>

Custom text labels usage

<CreditCardInput
  customTextLabels={{
    invalidCardNumber: 'El número de la tarjeta es inválido',
    expiryError: {
      invalidExpiryDate: 'La fecha de expiración es inválida',
      monthOutOfRange: 'El mes de expiración debe estar entre 01 y 12',
      yearOutOfRange: 'El año de expiración no puede estar en el pasado',
      dateOutOfRange: 'La fecha de expiración no puede estar en el pasado'
    },
    invalidCvc: 'El código de seguridad es inválido',
    invalidZipCode: 'El código postal es inválido',
    cardNumberPlaceholder: 'Número de tarjeta',
    expiryPlaceholder: 'MM/AA',
    cvcPlaceholder: 'COD',
    zipPlaceholder: 'C.P.'
  }}
/>

Contributing

Contributing to react-credit-card-input is easy! With four simple steps:

Create a branch

  1. Fork the repository
  2. git clone <your-repo-url> to clone your GitHub repo to your local one
  3. git pull origin master to pull the latest code
  4. npm install to install the project's dependencies
  5. git checkout -b the-name-of-my-branch to create a branch (use something short and comprehensible, such as: fix-card-number-issue).
  6. git remote add upstream https://github.com/medipass/react-credit-card-input.git and git pull upstream master to update your fork from this source.

Make the change

Note: You can run npm run storybook, and then navigate to http://localhost:9001/ to interactively develop your changes. If you are developing a new feature, make sure to add a story for it!

Test the change

  1. Run npm run fix from the project root (This will run Prettier and ESLint and automatically fix any issues).

Push the change!

  1. git add -A && git commit -m "My message (#issue-number/pr-number)" (replacing My message (#issue-number/pr-number) with a commit message, such as Fixed card number issue (#43)) to stage and commit your changes
  2. git push my-fork-name the-name-of-my-branch

License

MIT © Medipass Solutions

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.15

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0-canary.0

6 years ago

1.0.0

6 years ago

0.6.1

6 years ago

0.6.0

6 years ago

0.5.0

6 years ago

0.4.8

6 years ago

0.4.7

6 years ago

0.4.6

6 years ago

0.4.5

6 years ago

0.4.4

6 years ago

0.4.3

6 years ago

0.4.2

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago