UNPKG

@olo/pay-react-native

Version:
150 lines (143 loc) 8.46 kB
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } // Copyright © 2022 Olo Inc. All rights reserved. // This software is made available under the Olo Pay SDK License (See LICENSE.md file) import React, { forwardRef, useCallback, useImperativeHandle, useRef, useState } from 'react'; import { StyleSheet, Text, View } from 'react-native'; import { CardField } from '../definitions'; import { sharedComponentStyles, sharedErrorStyles } from './styles/sharedStyles'; import usePaymentCardDetailsViewManager from '../hooks/usePaymentCardDetailsViewManager'; import { sharedComponentValues } from './definitions/sharedValues'; // Fabric-only architecture const CardDetailsNative = require('./specs/PaymentCardDetailsViewNativeComponent').default; //prettier-ignore export const PaymentCardDetailsView = /*#__PURE__*/forwardRef(({ componentStyles, cardStyles, errorStyles, viewProps, onCardChange, onFocus, onBlur, onFocusField, onPaymentMethodResult, postalCodeEnabled, disabled, customErrorMessages, displayErrorMessages, placeholders, ...props }, ref) => { const ViewManagerMethods = usePaymentCardDetailsViewManager(); const [displayedError, setDisplayedError] = useState(""); const [allFieldsError, setAllFieldsError] = useState(""); const inputRef = useRef(null); const onCardChangeHandler = useCallback(event => { var _cardEvent$errors, _cardEvent$errors2; const cardEvent = event.nativeEvent; const cardData = { isValid: cardEvent.isValid, cardType: cardEvent.cardType, invalidFields: cardEvent.invalidFields, emptyFields: cardEvent.emptyFields, errors: cardEvent.errors }; setAllFieldsError(((_cardEvent$errors = cardEvent.errors) === null || _cardEvent$errors === void 0 ? void 0 : _cardEvent$errors.allFieldsError) ?? ""); setDisplayedError(((_cardEvent$errors2 = cardEvent.errors) === null || _cardEvent$errors2 === void 0 ? void 0 : _cardEvent$errors2.editedFieldsError) ?? ""); onCardChange === null || onCardChange === void 0 || onCardChange(cardData); }, [onCardChange, allFieldsError, displayedError]); const onFocusEventHandler = useCallback(_event => { onFocus === null || onFocus === void 0 || onFocus(); }, [onFocus]); const onBlurEventHandler = useCallback(_event => { onBlur === null || onBlur === void 0 || onBlur(); }, [onBlur]); const onFocusFieldEventHandler = useCallback(event => { onFocusField === null || onFocusField === void 0 || onFocusField(event.nativeEvent.field); }, [onFocusField]); // Handle payment method result event const onPaymentMethodResultHandler = useCallback(event => { var _eventData$error, _eventData$error2, _eventData$paymentMet; const eventData = event.nativeEvent; // Check for actual content (Fabric may send empty objects for unset optionals) const hasError = ((_eventData$error = eventData.error) === null || _eventData$error === void 0 ? void 0 : _eventData$error.code) || ((_eventData$error2 = eventData.error) === null || _eventData$error2 === void 0 ? void 0 : _eventData$error2.message); const hasPaymentMethod = (_eventData$paymentMet = eventData.paymentMethod) === null || _eventData$paymentMet === void 0 ? void 0 : _eventData$paymentMet.id; // Normalize the result - only include error/paymentMethod if they have actual content const normalizedResult = { ...(hasPaymentMethod ? { paymentMethod: eventData.paymentMethod } : {}), ...(hasError ? { error: eventData.error } : {}) }; // Update error display if there's an error if (hasError) { var _eventData$error3; setDisplayedError(((_eventData$error3 = eventData.error) === null || _eventData$error3 === void 0 ? void 0 : _eventData$error3.message) ?? ""); } // Call the user's callback with normalized result onPaymentMethodResult === null || onPaymentMethodResult === void 0 || onPaymentMethodResult(normalizedResult); }, [onPaymentMethodResult]); const clear = () => { ViewManagerMethods.clear(inputRef); }; const createPaymentMethod = () => { ViewManagerMethods.createPaymentMethod(inputRef); }; const blur = () => { ViewManagerMethods.blur(inputRef); }; const focus = (field = CardField.number) => { ViewManagerMethods.focus(inputRef, field); }; useImperativeHandle(ref, () => ({ clear, createPaymentMethod, blur, focus })); return /*#__PURE__*/React.createElement(View, _extends({ style: componentStyles }, viewProps), /*#__PURE__*/React.createElement(CardDetailsNative, _extends({ style: [defaultStyles.nativeCard, cardStyles === null || cardStyles === void 0 ? void 0 : cardStyles.styles], ref: inputRef, onCardChangeEvent: onCardChangeHandler, onFocusEvent: onFocusEventHandler, onBlurEvent: onBlurEventHandler, onFocusFieldEvent: onFocusFieldEventHandler, onPaymentMethodResultEvent: onPaymentMethodResultHandler, postalCodeEnabled: postalCodeEnabled ?? true, isEnabled: !disabled, customErrorMessages: customErrorMessages, placeholders: { number: (placeholders === null || placeholders === void 0 ? void 0 : placeholders.number) ?? sharedComponentValues.defaultValues.singleLineNumberPlaceholder, expiration: (placeholders === null || placeholders === void 0 ? void 0 : placeholders.expiration) ?? sharedComponentValues.defaultValues.expirationPlaceholder, cvv: (placeholders === null || placeholders === void 0 ? void 0 : placeholders.cvv) ?? sharedComponentValues.defaultValues.cvvPlaceholder, postalCode: (placeholders === null || placeholders === void 0 ? void 0 : placeholders.postalCode) ?? sharedComponentValues.defaultValues.postalCodePlaceholder }, cardStyles: { borderWidth: (cardStyles === null || cardStyles === void 0 ? void 0 : cardStyles.borderWidth) ?? sharedComponentStyles.defaultStyles.borderWidth, backgroundColor: (cardStyles === null || cardStyles === void 0 ? void 0 : cardStyles.backgroundColor) ?? sharedComponentStyles.defaultStyles.backgroundColor, borderColor: (cardStyles === null || cardStyles === void 0 ? void 0 : cardStyles.borderColor) ?? sharedComponentStyles.defaultStyles.borderColor, cornerRadius: (cardStyles === null || cardStyles === void 0 ? void 0 : cardStyles.cornerRadius) ?? sharedComponentStyles.defaultStyles.cornerRadius, cursorColor: (cardStyles === null || cardStyles === void 0 ? void 0 : cardStyles.cursorColor) ?? sharedComponentStyles.defaultStyles.cursorColor, errorTextColor: (cardStyles === null || cardStyles === void 0 ? void 0 : cardStyles.errorTextColor) ?? sharedComponentStyles.defaultStyles.errorTextColor, fontFamily: (cardStyles === null || cardStyles === void 0 ? void 0 : cardStyles.fontFamily) ?? sharedComponentStyles.defaultStyles.fontFamily, fontSize: (cardStyles === null || cardStyles === void 0 ? void 0 : cardStyles.fontSize) ?? sharedComponentStyles.defaultStyles.fontSize, placeholderColor: (cardStyles === null || cardStyles === void 0 ? void 0 : cardStyles.placeholderColor) ?? sharedComponentStyles.defaultStyles.placeholderColor, textPaddingLeft: (cardStyles === null || cardStyles === void 0 ? void 0 : cardStyles.textPaddingLeft) ?? sharedComponentStyles.defaultStyles.paddingLeft, textPaddingRight: (cardStyles === null || cardStyles === void 0 ? void 0 : cardStyles.textPaddingRight) ?? sharedComponentStyles.defaultStyles.paddingRight, textColor: (cardStyles === null || cardStyles === void 0 ? void 0 : cardStyles.textColor) ?? sharedComponentStyles.defaultStyles.textColor, fontWeight: (cardStyles === null || cardStyles === void 0 ? void 0 : cardStyles.fontWeight) ?? sharedComponentStyles.defaultStyles.fontWeight, italic: (cardStyles === null || cardStyles === void 0 ? void 0 : cardStyles.italic) ?? sharedComponentStyles.defaultStyles.italic } }, props)), /*#__PURE__*/React.createElement(Text, { style: [sharedErrorStyles.defaultStyles, errorStyles] }, displayErrorMessages ? displayedError : "")); }); const defaultStyles = StyleSheet.create({ nativeCard: { minHeight: 50 } }); //# sourceMappingURL=PaymentCardDetailsView.js.map