npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

reactjs-credit-card

v1.0.6

Published

This Package has react credit card form components and form validation system

Downloads

98

Readme

Hunel React Credit Card

npm

Github

Demo

For Demo Files

Hunel React Credit Card System is a completely customizable credit card component and validation system.

Installation

npm install reactjs-credit-card --save

Usage

Create a HunelCrediCard instance and Wrap any component with HunelProvider; index.js;

import { HunelProvider, HunelCreditCard } from 'reactjs-credit-card';

const hunel = new HunelCreditCard();

ReactDOM.render(
  <HunelProvider config={hunel}>
    <App />
  </HunelProvider>,
  document.getElementById('root')
);

Now we can import Card and form components in any component which wrapped with HunelProvider. Card component is our virtual card. Form components are our form components which can completely customizable.

App.js;

import {
  CardHolder,
  CardNumber,
  CardSecurityCode,
  ValidThruMonth,
  ValidThruYear,
} from "reactjs-credit-card/form";
import Card from "reactjs-credit-card/card";

export default function App(){
return (<div>
<Card />
<form>
    <CardNumber placeholder="Card Number" />
    <CardHolder placeholder="Card Holder" />
    <ValidThruMonth />
    <ValidThruYear />
    <CardSecurityCode placeholder="CVV" className="input-text semi" />
    <button>Submit</button>
<form>
</div>);
}

Get form data and validation with Hook

We can get form data object with hook and hoc.Hook and hoc returns a function.When we called this function,It returns an array which has data object and general verification result. Also data object has special verification result for each form component.

Then let's more develop on App component and declare a hook and also little bit customize form components;

import './style.css';
import {
  CardHolder,
  CardNumber,
  CardSecurityCode,
  ValidThruMonth,
  ValidThruYear,
} from 'reactjs-credit-card/form';
import Card from 'reactjs-credit-card/card';
import { useCardForm } from 'reactjs-credit-card';
import { useState } from 'react';

function App() {
  const getFormData = useCardForm();
  const [numberValid, setNumberValid] = useState(true);

  function handleSubmit(e) {
    e.preventDefault();
    const [data, isValid] = getFormData();

    console.log(data, isValid); //log all form data and verification results

    if (!data.number.isValid) setNumberValid(false); //we'll set a hook to show a error if card number is invalid
    //check the general verification result and alert with special verification result
    if (!isValid)
      alert(
        `${data.holder.value} form data values invalid :) and holder also ${
          data.holder.isValid ? 'valid' : 'invalid'
        }`
      );
  }

  //remove error function if focused on CardNumber
  function handleFocus() {
    setNumberValid(true);
  }

  return (
    <div className="container">
      <div className="form-box">
        <form onSubmit={handleSubmit}>
          //If numberValid state is false then show a error
          <CardNumber
            placeholder="Card Number"
            className={`input-text${!numberValid ? ' error' : ''}`}
            onFocus={handleFocus}
          />
          <CardHolder placeholder="Card Holder" className="input-text" />
          <div className="flex-wrapper">
            <div className="semi flex-wrapper">
              <ValidThruMonth className="input-text semi" />
              <ValidThruYear className="input-text semi" />
            </div>
            <CardSecurityCode placeholder="CVV" className="input-text semi" />
          </div>
          <button className="btn">Submit</button>
        </form>
      </div>
      //fixClass property is used to change all card components sizes by changing
      font-size //default fonts-size 11px.
      <Card fixClass="fix-new" cardClass="card-new" />
    </div>
  );
}

you can take a look reactjs-credicard-example

Get form data and validation with HOC

We can get form data and verification results with hoc.Let's modify the same example with hoc;

import './style.css';
import {
  CardHolder,
  CardNumber,
  CardSecurityCode,
  ValidThruMonth,
  ValidThruYear,
} from 'reactjs-credit-card/form';
import Card from 'reactjs-credit-card/card';
import { cardForm } from 'reactjs-credit-card'; //import the HOC
import { useState } from 'react';

function App({ getCardForm }) {
  const [numberValid, setNumberValid] = useState(true);
  //we can get getCardForm property like hook usage after wrap the App component with HOC
  function handleSubmit(e) {
    const [data, isValid] = getCardForm();
    e.preventDefault();
    console.log(data, isValid); //log all form data and verification results

    if (!data.number.isValid) setNumberValid(false); //we'll set a hook to show a error if card number is invalid
    //check the general verification result and alert with special verification result
    if (!isValid)
      alert(
        `${data.holder.value} form data values invalid :) and holder also ${
          data.holder.isValid ? 'valid' : 'invalid'
        }`
      );
  }

  //remove error function if focused on CardNumber
  function handleFocus() {
    setNumberValid(true);
  }

  return (
    <div className="container">
      <div className="form-box">
        <form onSubmit={handleSubmit}>
          //If numberValid state is false then show a error
          <CardNumber
            placeholder="Card Number"
            className={`input-text${!numberValid ? ' error' : ''}`}
            onFocus={handleFocus}
          />
          <CardHolder placeholder="Card Holder" className="input-text" />
          <div className="flex-wrapper">
            <div className="semi flex-wrapper">
              <ValidThruMonth className="input-text semi" />
              <ValidThruYear className="input-text semi" />
            </div>
            <CardSecurityCode placeholder="CVV" className="input-text semi" />
          </div>
          <button className="btn">Submit</button>
        </form>
      </div>
      //fixClass property is used to change all card components sizes by changing
      font-size //default fonts-size 11px.
      <Card fixClass="fix-new" cardClass="card-new" />
    </div>
  );
}
export default cardForm(App); //wrap with hoc

Customization

We can add any default property to the form components;

<CardHolder
  placeholder="Card Holder"
  className="input-text"
  onBlur={handleOnBlur}
  onAnimationEnd={handleAnimationEnd}
  onAnimationStart={handleAnimationStart}
/>

Card component accept 2 property which fixClass and cardClass. fixClass uses to change the Card component size. cardClass uses styling the Card component.

<Card fixClass="fix-new" cardClass="card-new" />
.fix-new {
  font-size: 10px !important; /*we can easly set the credit card size*/
}
.card-new {
  background: url(./any_pattern.jpg) !important; /*also we can easly set the credit card background*/
}

Customization for HunelCreditCard Instance

HunelCreditCard's constructer accept a object for customizing.But you don't have to declare a config object.

Config Object

For now config object has middlePartHide property which default value false. middlePartHide is uses to hide the credit card number on the Card component. Config object also has yearLength property which uses to declare valid thru year certain range.

import { HunelProvider, HunelCreditCard } from 'reactjs-credit-card';

//you can declare a object
const hunel = new HunelCreditCard({
  middlePartHide: false,
  yearLength: 15, //from 2022 to 2036
});

//also you can create instance without declare a config object
ReactDOM.render(
  <HunelProvider config={hunel}>
    <App />
  </HunelProvider>,
  document.getElementById('root')
);

Card Type Support

For now Mastercard,Visa,American Express and Discover card types have support.

IF You Dont't Use Webpack Image Loader

If you don't use webpack image loader then you can download the image files as manuel.And after download extract the files to public file Download the images