import * as React from 'react'; import { AriaLabel, ChargebeeInstance, Classes, Fonts, Placeholder, Styles } from "@chargebee/chargebee-js-types"; import FieldContainer from "./FieldContainer"; import { CancellablePromise, makeCancelablePromise } from 'utils'; // Set source for Chargebee.js KVL logging immediately when module loads if (typeof window !== 'undefined') { (window as any).CbJsSource = 'react'; } export interface ChargebeeComponentProps { children?: React.ReactNode; type?: string; fonts?: Fonts; classes?: Classes; icon?: boolean; styles?: Styles; showTestCards?: boolean; locale?: string; placeholder?: Placeholder; currency?: string; ariaLabel?: AriaLabel; className?: string; onBlur?: React.MouseEventHandler; onChange?: React.ChangeEventHandler; onFocus?: React.FocusEventHandler; onReady?: React.EventHandler; onKeyPress?: Function; forwardedRef?: React.LegacyRef; } interface ChargebeeComponentState { moduleLoaded: Boolean; cbInstance: ChargebeeInstance; } export default class ChargebeeComponents extends React.Component { private loader: CancellablePromise; constructor(props: ChargebeeComponentProps) { super(props); this.state = { moduleLoaded: false, cbInstance: null, } } componentWillUnmount() { this.loader.cancel(); } componentDidMount() { // @ts-ignore const cbInstance = Chargebee.getInstance(); this.loader = makeCancelablePromise(cbInstance.load("components")); this.loader.promise.then(({isCancelled}) => { if (isCancelled) { return; } this.setState({ cbInstance: cbInstance, moduleLoaded: true }); }); } render() { const {forwardedRef, ...rest} = this.props; return ( <> {this.state.moduleLoaded ? : ''} ) } }