Skip to content

Commit

Permalink
feat: update Marks to support the children prop (#155)
Browse files Browse the repository at this point in the history
This children prop is for displaying custom markup when ineligible.
  • Loading branch information
borodovisin committed Sep 14, 2021
1 parent f7b4a25 commit 6aebf98
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
14 changes: 11 additions & 3 deletions src/components/PayPalMarks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,21 @@ describe("<PayPalMarks />", () => {
render: mockRender,
});

render(
const { container } = render(
<PayPalScriptProvider options={{ "client-id": "test" }}>
<PayPalMarks />
<PayPalMarks className="mark-container">
<div className="ineligible"></div>
</PayPalMarks>
</PayPalScriptProvider>
);

await waitFor(() => expect(mockIsEligible).toBeCalled());
await waitFor(() =>
expect(
container.querySelector(".ineligible") instanceof HTMLDivElement
).toBeTruthy()
);
expect(container.querySelector(".mark-container")).toBeNull();
expect(mockIsEligible).toBeCalledTimes(1);
expect(mockRender).not.toBeCalled();
});

Expand Down
27 changes: 24 additions & 3 deletions src/components/PayPalMarks.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import React, { useEffect, useRef, useState, FunctionComponent } from "react";
import React, {
useEffect,
useRef,
useState,
FunctionComponent,
ReactElement,
ReactPortal,
ReactFragment,
} from "react";
import { usePayPalScriptReducer } from "../hooks/scriptProviderHooks";
import { getPayPalWindowNamespace } from "../utils";
import { DEFAULT_PAYPAL_NAMESPACE } from "../constants";
Expand All @@ -12,6 +20,7 @@ export interface PayPalMarksComponentProps extends PayPalMarksComponentOptions {
* Pass a css class to the div container.
*/
className?: string;
children?: ReactElement | ReactPortal | ReactFragment;
}

/**
Expand All @@ -32,10 +41,12 @@ A `FUNDING` object is exported by this library which has a key for every availab
*/
export const PayPalMarks: FunctionComponent<PayPalMarksComponentProps> = ({
className = "",
children,
...markProps
}: PayPalMarksComponentProps) => {
const [{ isResolved, options }] = usePayPalScriptReducer();
const markContainerRef = useRef<HTMLDivElement>(null);
const [isEligible, setIsEligible] = useState(true);
const [, setErrorState] = useState(null);

/**
Expand All @@ -45,7 +56,9 @@ export const PayPalMarks: FunctionComponent<PayPalMarksComponentProps> = ({
const { current } = markContainerRef;

// only render the mark when eligible
if (!current || !mark.isEligible()) return;
if (!current || !mark.isEligible()) {
return setIsEligible(false);
}
// Remove any children before render it again
if (current.firstChild) {
current.removeChild(current.firstChild);
Expand Down Expand Up @@ -89,7 +102,15 @@ export const PayPalMarks: FunctionComponent<PayPalMarksComponentProps> = ({
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isResolved, markProps.fundingSource]);

return <div ref={markContainerRef} className={className} />;
return (
<>
{isEligible ? (
<div ref={markContainerRef} className={className} />
) : (
children
)}
</>
);
};

function getErrorMessage({
Expand Down

0 comments on commit 6aebf98

Please sign in to comment.