From dd716109921aa906a8fda54e531f7f41eaec4313 Mon Sep 17 00:00:00 2001 From: PavlenkoM Date: Wed, 2 Oct 2024 10:57:19 +0300 Subject: [PATCH] feat(payment): STRIPE-444 New Stripe UPE (PoC) --- .../paymentMethod/PaymentMethodList.tsx | 11 ++++++- .../src/app/ui/form/CustomChecklistItem.tsx | 22 ++++++++++++++ packages/core/src/app/ui/form/index.ts | 1 + .../checkout/checklist/_checklist.scss | 11 +++++++ .../src/stripe-upe/StripeUPEPaymentMethod.tsx | 29 ++++++++++++++++++- 5 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 packages/core/src/app/ui/form/CustomChecklistItem.tsx diff --git a/packages/core/src/app/payment/paymentMethod/PaymentMethodList.tsx b/packages/core/src/app/payment/paymentMethod/PaymentMethodList.tsx index b19af754cc..c909bffce7 100644 --- a/packages/core/src/app/payment/paymentMethod/PaymentMethodList.tsx +++ b/packages/core/src/app/payment/paymentMethod/PaymentMethodList.tsx @@ -4,7 +4,7 @@ import React, { FunctionComponent, memo, useCallback, useMemo } from 'react'; import { connectFormik, ConnectFormikProps } from '../../common/form'; import { isMobile } from '../../common/utility'; -import { Checklist, ChecklistItem } from '../../ui/form'; +import { Checklist, ChecklistItem, CustomChecklistItem } from '../../ui/form'; import getUniquePaymentMethodId, { parseUniquePaymentMethodId } from './getUniquePaymentMethodId'; import PaymentMethodTitle from './PaymentMethodTitle'; @@ -116,6 +116,15 @@ const PaymentMethodListItem: FunctionComponent = ({ [method], ); + if (method.initializationData?.accordion) { // TODO: accordion key will be changed to more relevant after changes on BCapp side + return ( + + ); + } + return ( = ({ + content, + htmlId, +}) => { + return ( +
  • + {content} +
  • + ); +}; + +export default memo(CustomChecklistItem); diff --git a/packages/core/src/app/ui/form/index.ts b/packages/core/src/app/ui/form/index.ts index 35410e41b5..50ae2e0a02 100644 --- a/packages/core/src/app/ui/form/index.ts +++ b/packages/core/src/app/ui/form/index.ts @@ -17,5 +17,6 @@ export { default as CheckboxInput } from './CheckboxInput'; export { default as Label, LabelProps } from './Label'; export { default as Legend, LegendProps } from './Legend'; export { default as ChecklistItemInput, ChecklistItemInputProps } from './ChecklistItemInput'; +export { default as CustomChecklistItem, CustomChecklistItemProps } from './CustomChecklistItem'; export { default as DynamicFormField } from './DynamicFormField'; export { default as DynamicFormFieldType } from './DynamicFormFieldType'; diff --git a/packages/core/src/scss/components/checkout/checklist/_checklist.scss b/packages/core/src/scss/components/checkout/checklist/_checklist.scss index 9f1c3cae08..500c1e7e05 100644 --- a/packages/core/src/scss/components/checkout/checklist/_checklist.scss +++ b/packages/core/src/scss/components/checkout/checklist/_checklist.scss @@ -212,3 +212,14 @@ will-change: $loading-skeleton-will-change; } } + +.custom-checklist-item { + margin: 0; + overflow: hidden; + border-bottom: 0; + + .paymentMethod--hosted, + .widget { + padding: 0; + } +} diff --git a/packages/stripe-integration/src/stripe-upe/StripeUPEPaymentMethod.tsx b/packages/stripe-integration/src/stripe-upe/StripeUPEPaymentMethod.tsx index 04c8ba1ea8..43cda6d549 100644 --- a/packages/stripe-integration/src/stripe-upe/StripeUPEPaymentMethod.tsx +++ b/packages/stripe-integration/src/stripe-upe/StripeUPEPaymentMethod.tsx @@ -1,6 +1,13 @@ import { PaymentInitializeOptions } from '@bigcommerce/checkout-sdk'; import { noop, some } from 'lodash'; -import React, { FunctionComponent, useCallback, useMemo } from 'react'; +import React, { + FunctionComponent, + useCallback, + useContext, + useEffect, + useMemo, + useRef, +} from 'react'; import { getAppliedStyles } from '@bigcommerce/checkout/dom-utils'; import { HostedWidgetPaymentComponent } from '@bigcommerce/checkout/hosted-widget-integration'; @@ -13,6 +20,7 @@ import { PaymentMethodResolveId, toResolvableComponent, } from '@bigcommerce/checkout/payment-integration-api'; +import { AccordionContext } from '@bigcommerce/checkout/ui'; const StripeUPEPaymentMethod: FunctionComponent = ({ paymentForm, @@ -22,9 +30,19 @@ const StripeUPEPaymentMethod: FunctionComponent = ({ onUnhandledError = noop, ...rest }) => { + const collapseStripeElement = useRef<() => void>(); + const { onToggle, selectedItemId } = useContext(AccordionContext); const containerId = `stripe-${method.id}-component-field`; const paymentContext = paymentForm; + useEffect(() => { + if (selectedItemId?.includes('stripeupe-')) { + return; + } + + collapseStripeElement.current?.(); + }, [selectedItemId]); + const renderSubmitButton = useCallback(() => { paymentContext.hidePaymentSubmitButton(method, false); }, [paymentContext, method]); @@ -69,6 +87,10 @@ const StripeUPEPaymentMethod: FunctionComponent = ({ return getAppliedStyles(parentContainer, properties); }; + // const accordionCollapseListener = (collapseElement: () => void) => { + // collapseStripeElement.current = collapseElement; + // }; + const initializeStripePayment = useCallback( async (options: PaymentInitializeOptions) => { const formInput = getStylesFromElement(`${containerId}--input`, [ @@ -97,6 +119,10 @@ const StripeUPEPaymentMethod: FunctionComponent = ({ }, onError: onUnhandledError, render: renderSubmitButton, + paymentMethodSelect: onToggle, + handleClosePaymentMethod: (collapseElement: () => void) => { + collapseStripeElement.current = collapseElement; + }, }, }); }, @@ -107,6 +133,7 @@ const StripeUPEPaymentMethod: FunctionComponent = ({ method, paymentContext, renderSubmitButton, + onToggle, ], );