import { v4 as uuidv4 } from "uuid"; import defineComponentProperties from "./defineComponentProperties"; import kebabToCamelCase from "./kebabToCamelCase"; /* An input that accepts card data and registers cards with Moov */ class CardLink extends HTMLElement { static observedAttributes = ['oauth-token', 'account-id', 'validation-event']; static componentProps = ['oauthToken', 'accountID', 'validationEvent', 'inputStyle', 'onEnterKeyPress', 'onError', 'onValidated', 'onSuccess', 'imperativeRef', 'holderName', 'allowedCardBrands']; private _drop: any; [key: string]: any; constructor() { super(); } handleMoovDropsInitialized = () => { if (!this.id) this.id = `moov-card-link-${uuidv4()}`; if (!this.imperativeRef) this.imperativeRef = { current: null as any }; const initialPropValues = {} as Record; CardLink.componentProps.forEach((propName) => { if (propName in this) initialPropValues[propName] = this[propName]; }); const dropObject = window.drops.cardLink(initialPropValues, this.id); this._drop = dropObject; } handleMoovJsLoaded = () => { document.removeEventListener("moov.moovJSLoaded", this.handleMoovJSLoaded); if (window.Moov && !window.drops) window.Moov("fake-token").drops().then(this.handleMoovDropsInitialized); } connectedCallback() { if (window.drops) this.handleMoovDropsInitialized(); if (window.Moov && !window.drops) this.handleMoovJsLoaded(); if (!window.Moov && !window.drops) document.addEventListener("moov.moovJSLoaded", this.handleMoovJsLoaded); } /** Callback fired when an observed attribute changes value */ attributeChangedCallback(name: string, oldValue: string, newValue: string) { if (oldValue === newValue) return; const propertyName = name === "account-id" ? "accountID" : kebabToCamelCase(name); if (propertyName in this) this[propertyName] = newValue; } /** Attempts to register a new card using the current input values. */ submit() { this.imperativeRef.current?.submit?.(); } /** Triggers validation on the current input values */ validate() { this.imperativeRef.current?.validate?.(); } /** Runs validation asynchronously and returns a promise resolving to true or false */ fetchValidity() { return this.imperativeRef.current?.fetchValidity?.(); } /** Clears all user input */ clear() { this.imperativeRef.current?.clear?.(); } } defineComponentProperties(CardLink, CardLink.componentProps); export default CardLink;