react-native-credit-card-input:react-native-credit-card-input是一个RN三方库,用于在 React Native 应用程序中实现信用卡输入表单。它提供了一个简单易用的界面,用户可以在其中输入信用卡信息,如卡号、过期日期和 CVV 码。组件还支持自动格式化输入内容,以及验证信用卡信息的有效性。       

react-native-credit-card-input是一个RN三方库,用于在 React Native 应用程序中实现信用卡输入表单。它提供了一个简单易用的界面,用户可以在其中输入信用卡信息,如卡号、过期日期和 CVV 码。组件还支持自动格式化输入内容,以及验证信用卡信息的有效性。       

Branch4Tags5

Document Template: v0.4.2

react-native-credit-card-input

This project is based on react-native-credit-card-input.

This third-party library has been migrated to GitCode and is now available for direct download from npm. The new package name is @react-native-ohos/react-native-credit-card-input, and the version correspondence is as follows:

Name Version(Npm Address) Release Information Supported RN Version Supported Autolink Compile API Version Community Baseline Version Source code address
@react-native-ohos/react-native-credit-card-input ~1.0.1 GitCode Releases 0.77.* / 0.82.* / 0.84.* / 0.86.* No API12+ 1.0.0 sig
@react-native-ohos/react-native-credit-card-input ~0.4.2 GitCode Releases 0.72.* No API12+ 0.4.1 br_rnoh0.72
@react-native-oh-tpl/react-native-credit-card-input <= 0.4.1-0.0.2@deprecated GitHub Releases(deprecated) 0.72.* No API12+ 0.4.1 sig

Introduction

react-native-credit-card-input is a credit card input component library for React Native. It provides the CreditCardInput full form, the LiteCreditCardInput compact form, and the CreditCardView card display component. The built-in useCreditCardForm hook automatically formats and validates the card number, expiry date, and CVC, and detects the card issuer type from the card number.

Installation

Go to the project directory and execute the following instruction:

npm

npm install @react-native-ohos/react-native-credit-card-input@~1.0.1

yarn

yarn add @react-native-ohos/react-native-credit-card-input@~1.0.1

Constraints

Compatibility

This document is verified based on the following versions:

  1. RNOH: 0.77.18; SDK: HarmonyOS 6.0.0 Release SDK; IDE: DevEco Studio 6.0.0.858; ROM: 6.0.0.112;
  2. RNOH: 0.82.21; SDK: HarmonyOS 6.0.1 Release SDK; IDE: DevEco Studio 6.0.1.260; ROM: 6.0.1.116;
  3. RNOH: 0.84.1; SDK: HarmonyOS 6.0.1 Release SDK; IDE: DevEco Studio 6.0.1.260; ROM: 6.0.1.116;
  4. RNOH: 0.86.1; SDK: HarmonyOS 6.0.1 Release SDK; IDE: DevEco Studio 6.0.1.260; ROM: 6.0.1.116;

Permission Requirements

No special permission is required.

Example

The following code shows the basic use scenario of the repository:

The name of the imported repository remains unchanged.

import React, { useState } from "react";
import { SafeAreaView, ScrollView, Switch, Text } from "react-native";
import {
  CreditCardInput,
  LiteCreditCardInput,
  type CreditCardFormData,
} from "react-native-credit-card-input";

export default function App() {
  const [useLite, setUseLite] = useState(true);
  const [form, setForm] = useState<CreditCardFormData | null>(null);

  const _onChange = (formData: CreditCardFormData) => {
    setForm(formData);
  };

  return (
    <SafeAreaView style={{ flex: 1 }}>
      <ScrollView keyboardShouldPersistTaps="handled">
        <Switch value={useLite} onValueChange={setUseLite} />
        {useLite ? (
          <LiteCreditCardInput
            onChange={_onChange}
            placeholderColor={"yellow"}
          />
        ) : (
          <CreditCardInput
            onChange={_onChange}
            labels={{ number: "CARD NUMBER", expiry: "EXPIRY", cvc: "CVC/CVV" }}
            placeholderColor={"yellow"}
          />
        )}
        {form ? <Text>number: {form.values.number}</Text> : null}
        {form ? <Text>expiry: {form.values.expiry}</Text> : null}
        {form ? <Text>cvc: {form.values.cvc}</Text> : null}
      </ScrollView>
    </SafeAreaView>
  );
}

How to Use

Full form

CreditCardInput provides a full input form with field labels:

<CreditCardInput
  autoFocus={false}
  labels={{ number: "CARD NUMBER", expiry: "EXPIRY", cvc: "CVC/CVV" }}
  placeholders={{ number: "1234 5678 1234 5678", expiry: "MM/YY", cvc: "CVC" }}
  labelStyle={{ color: "yellow", fontSize: 12 }}
  inputStyle={{ fontSize: 16, color: "yellow" }}
  placeholderColor={"yellow"}
  onChange={(formData) => console.log(formData.values, formData.status)}
  onFocusField={(field) => console.log(field)}
/>

Compact form

LiteCreditCardInput provides a single-line compact input form. The expiry and CVC fields expand automatically once the card number becomes valid:

<LiteCreditCardInput
  autoFocus
  inputStyle={{ fontSize: 16, color: "yellow" }}
  placeholderColor={"yellow"}
  onChange={(formData) => console.log(formData.values, formData.status)}
  onFocusField={(field) => console.log(field)}
/>

Card display

CreditCardView displays credit card information on its own. For non-American Express cards, it flips to the back when cvc is focused:

<CreditCardView
  type="visa"
  name="TEST USER"
  number="4111 1111 1111 1111"
  expiry="12/30"
  cvc="123"
  focusedField="number"
/>

Available APIs

The **Platform** column indicates the platform where the properties are supported in the original third-party library.

If the value of **OpenHarmony Platform Support** is **yes**, it means that the OpenHarmony platform supports this property; **no** means the opposite; **partially** means some capabilities of this property are supported. The usage method is the same on different platforms and the effect is the same as that of iOS or Android.

Components

Name Parameter Type Required Platform OpenHarmony Platform Support Description
CreditCardInput CreditCardInputProps No iOS, Android Yes Full credit card input form component
LiteCreditCardInput LiteCreditCardInputProps No iOS, Android Yes Compact credit card input form component
CreditCardView CreditCardViewProps No iOS, Android Yes Credit card display component

Properties

CreditCardInputProps

Name Parameter Type Default Value Required Platform OpenHarmony Platform Support Description
autoFocus boolean None No iOS, Android Yes Automatically focuses the card number input after render; no focusing when not set
style ViewStyle None No iOS, Android Yes Style of the outer container
labelStyle TextStyle None No iOS, Android Yes Style of the field labels
inputStyle TextStyle None No iOS, Android Yes Text style of the card number, expiry, and CVC inputs
placeholderColor string 'darkgray' No iOS, Android Yes Color of the placeholder text
labels { number: string; expiry: string; cvc: string } { number: 'CARD NUMBER', expiry: 'EXPIRY', cvc: 'CVC/CVV' } No iOS, Android Yes Label text of the fields; all three fields must be provided when customizing
placeholders { number: string; expiry: string; cvc: string } { number: '1234 5678 1234 5678', expiry: 'MM/YY', cvc: 'CVC' } No iOS, Android Yes Placeholder text of the three input fields; all three fields must be provided when customizing
onChange (formData: CreditCardFormData) => void Empty function Yes iOS, Android Yes Called when the input changes; returns the formatted values, the validation status of each field, and the overall validity
onFocusField (field: 'number' | 'expiry' | 'cvc') => void Empty function No iOS, Android Yes Called when an input field gains focus
testID string None No iOS, Android Yes Test identifier of the outer container

LiteCreditCardInputProps

Name Parameter Type Default Value Required Platform OpenHarmony Platform Support Description
autoFocus boolean false No iOS, Android Yes Automatically focuses the card number input after render
style ViewStyle None No iOS, Android Yes Style of the outer container
inputStyle TextStyle None No iOS, Android Yes Text style of the card number, expiry, and CVC inputs
placeholderColor string 'darkgray' No iOS, Android Yes Color of the placeholder text
placeholders { number: string; expiry: string; cvc: string } { number: '1234 5678 1234 5678', expiry: 'MM/YY', cvc: 'CVC' } No iOS, Android Yes Placeholder text of the three input fields; all three fields must be provided when customizing
onChange (formData: CreditCardFormData) => void Empty function No iOS, Android Yes Called when the input changes; returns the formatted values, the validation status of each field, and the overall validity
onFocusField (field: 'number' | 'expiry' | 'cvc') => void Empty function No iOS, Android Yes Called when an input field gains focus
testID string None No iOS, Android Yes Test identifier of the outer container

CreditCardViewProps

Name Parameter Type Default Value Required Platform OpenHarmony Platform Support Description
focusedField 'name' | 'number' | 'expiry' | 'cvc' None No iOS, Android Yes The currently highlighted field; for non-American Express cards, the back of the card is shown when cvc is focused
type 'visa' | 'mastercard' | 'american-express' | 'diners-club' | 'discover' | 'jcb' None No iOS, Android Yes Card issuer type, used for the brand icon and the American Express CVC position
name string None No iOS, Android Yes Cardholder's name, displayed in upper case; the placeholder is shown when empty
number string None No iOS, Android Yes Card number text to display; formatting must be done by the caller
expiry string None No iOS, Android Yes Expiry text to display, usually in MM/YY format
cvc string None No iOS, Android Yes CVC text to display
placeholders { number: string; expiry: string; cvc: string; name: string } { number: '•••• •••• •••• ••••', expiry: '••/••', cvc: '•••', name: '' } No iOS, Android Yes Placeholder text for empty display fields; all four fields must be provided when customizing
style ViewStyle None No iOS, Android Yes Style of the card's outer container; the default card size is 300 × 190
fontFamily string Courier (iOS) / monospace (Android, Web) No iOS, Android Yes Font family of the card view; the system default font is used on OpenHarmony when not specified
imageFront ImageSourcePropType None No iOS, Android Yes Background image of the card front
imageBack ImageSourcePropType None No iOS, Android Yes Background image of the card back

API

Name Type Parameter Type Return Value Required Platform OpenHarmony Platform Support Description
useCreditCardForm function (onChange: (formData: CreditCardFormData) => void) { values, status, onChangeValue } No iOS, Android Yes Form formatting and validation hook: onChangeValue(field, value) receives a raw field value, formats and validates it internally, and calls back onChange

CreditCardFormData

Name Type Description
valid boolean true only when the status of number, expiry, and cvc are all valid
values.number string Card number formatted according to the detected card type
values.expiry string Formatted expiry (MM/YY)
values.cvc string CVC limited to the length allowed by the card type
values.type string | undefined Detected card issuer type; undefined when not recognized
status CreditCardFormState Validation status of each field: incomplete, invalid, or valid

Known Issues

None

Other

None

Directory Structure

/react-native-credit-card-input  # Project root directory
├── src                          # Component source code (TypeScript)
│   ├── CreditCardInput.tsx      # Full form component
│   ├── LiteCreditCardInput.tsx  # Compact form component
│   ├── CreditCardView.tsx       # Card display component
│   ├── useCreditCardForm.tsx    # Form formatting and validation hook
│   ├── Icons.ts                 # Brand icon mapping
│   ├── __tests__                # Unit tests
│   └── index.tsx                # Export entry
├── jest                         # Additional unit tests
├── example                      # Example project
├── COMMITTERS.md                # Committers
├── CHANGELOG.md                 # Version changelog
├── buildEnv.sh                  # Environment build script
├── babel.config.js              # Babel configuration
├── jest.config.js               # Jest configuration
├── jest.setup.ts                # Jest setup
├── lefthook.yml                 # Git hooks configuration
├── tsconfig.json                # TypeScript configuration
├── tsconfig.build.json          # TypeScript build configuration
├── LICENSE                      # Open source license
├── OAT.xml                      # OAT scanning configuration
├── README.OpenSource            # Open source information
├── README.md                    # Chinese document
├── README_en.md                 # English document
└── package.json                 # Package configuration

How to Contribute

If you find any problem when using react-native-credit-card-input, submit an Issue. Of course, PR is also welcome.

License

This project is licensed under The MIT License (MIT).

Introduction

react-native-credit-card-input是一个RN三方库,用于在 React Native 应用程序中实现信用卡输入表单。它提供了一个简单易用的界面,用户可以在其中输入信用卡信息,如卡号、过期日期和 CVV 码。组件还支持自动格式化输入内容,以及验证信用卡信息的有效性。

Customize your domain