Code

Props
Data Type
Required

name

string

number

string

expiry

string

cvc

string

focused

string ('name', 'number', 'expiry', 'cvc')

namePlaceholder

boolean

accepdCards

boolean

This component should be used together with CreditCardName, CreditCardNumber, CreditCardCVC, CreditCardDate components.

Complement Components

CreditCardName, CreditCardNumber, CreditCardCVC, CreditCardDate

Props
Data Type
Required

value

string

className

string

id

string

name

string

label

string

info

string

helptext

string

error

boolean

errorMessage

string

disabled

boolean

readOnly

boolean

onBlur

function

onChange

function

onChange

function

These four components work exactly like the TextInput and NumberInput components removing a couple of extra features to delimitate the specific experience to give to the user by using this inputs.

Also the tokens used for these components are the same that we're using for the TextInput and NumberInput components.

How To Use

The main component is CreditCard, which is going to receive through the other CreditCard input components the data to perform the animations and interactions expected.

Example

import React, { useState } from 'react';
import {
  CreditCard,
  CreditCardNumber,
  CreditCardName,
  CreditCardDate,
  CreditCardCVC,
  Text
} from '@2600hz/sds-react-components';
import './app.scss';

const App = () => {
  const [focusedData, setFocusedData] = useState<
    'name' | 'number' | 'cvc' | 'expiry'
  >('name');
  
  const [cardData, setCardData] = useState({
    name: '',
    number: '',
    cvc: '',
    expiry: ''
  });
  
  const focusElement(id){
    switch (id) {
      case 'name':
        setFocusedData('name');
        break;
      case 'number':
        setFocusedData('number');
        break;
      case 'cvc':
        setFocusedData('cvc');
        break;
      case 'expiry':
        setFocusedData('expiry');
        break;
      default:
        setFocusedData('name');
        break;
    }
  }

  const change = (e: any) => {
    const { value, id } = e;
    focusElement(id);
    setCardData((prevState) => ({ ...prevState, [id]: value }));
  };

  const focus = (e: any) => {
    const { id } = e;
    focusElement(id);
  };

  return (
    <div>
      <div className="form">
        <Text.h2 textStyle="Heading1">Credit Card Data</Text.h2>
        <CreditCardName
          id="name"
          value={cardData.name}
          onFocus={focus}
          onChange={change}
        />
        <CreditCardNumber
          id="number"
          value={cardData.number}
          onFocus={focus}
          onChange={change}
        />
        <CreditCardDate
          id="expiry"
          value={cardData.expiry}
          onFocus={focus}
          onChange={change}
        />
        <CreditCardCVC
          id="cvc"
          value={cardData.cvc}
          onFocus={focus}
          onChange={change}
        />
      </div>
      <CreditCard
        name={cardData.name}
        number={cardData.number}
        expiry={cardData.expiry}
        cvc={cardData.cvc}
        focused={focusedData}
      />
    </div>
  );
};

export default App;

Live Demo

For detailed code usage documentation, see the Storybooks for each framework below.

React

Last updated