Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix regression in attaching event handlers #368

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/components/createElementComponent.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,22 @@ describe('createElementComponent', () => {
);
});

it('adds an event handler', () => {
const mockHandler = jest.fn();
render(
<Elements stripe={mockStripe}>
<CardElement onChange={mockHandler} />
</Elements>
);

expect(simulateOn).toBeCalledTimes(1);
expect(simulateOn).toBeCalledWith('change', mockHandler);

const changeEventMock = Symbol('change');
simulateEvent('change', changeEventMock);
expect(mockHandler).toHaveBeenCalledWith(changeEventMock);
});

it('removes old event handlers when an event handler changes', () => {
const mockHandler = jest.fn();
const mockHandler2 = jest.fn();
Expand All @@ -263,6 +279,8 @@ describe('createElementComponent', () => {
);

expect(simulateOn).toBeCalledWith('change', mockHandler);

expect(simulateOn).toBeCalledTimes(1);
expect(simulateOff).not.toBeCalled();

rerender(
Expand Down
74 changes: 59 additions & 15 deletions src/components/createElementComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,42 @@ const createElementComponent = (
// For every event where the merchant provides a callback, call element.on
// with that callback. If the merchant ever changes the callback, removes
// the old callback with element.off and then call element.on with the new one.
useAttachEvent(elementRef, 'blur', onBlur);
useAttachEvent(elementRef, 'focus', onFocus);
useAttachEvent(elementRef, 'escape', onEscape);
useAttachEvent(elementRef, 'click', onClick);
useAttachEvent(elementRef, 'loaderror', onLoadError);
useAttachEvent(elementRef, 'loaderstart', onLoaderStart);
useAttachEvent(elementRef, 'networkschange', onNetworksChange);
useAttachEvent(elementRef, 'lineitemclick', onLineItemClick);
useAttachEvent(elementRef, 'confirm', onConfirm);
useAttachEvent(elementRef, 'cancel', onCancel);
useAttachEvent(
const attachBlurEvent = useAttachEvent(elementRef, 'blur', onBlur);
const attachFocusEvent = useAttachEvent(elementRef, 'focus', onFocus);
const attachEscapeEvent = useAttachEvent(elementRef, 'escape', onEscape);
const attachClickEvent = useAttachEvent(elementRef, 'click', onClick);
const attachLoadErrorEvent = useAttachEvent(
elementRef,
'loaderror',
onLoadError
);
const attachLoaderStartEvent = useAttachEvent(
elementRef,
'loaderstart',
onLoaderStart
);
const attachNetworksChangeEvent = useAttachEvent(
elementRef,
'networkschange',
onNetworksChange
);
const attachLineItemClickEvent = useAttachEvent(
elementRef,
'lineitemclick',
onLineItemClick
);
const attachConfirmEvent = useAttachEvent(elementRef, 'confirm', onConfirm);
const attachCancelEvent = useAttachEvent(elementRef, 'cancel', onCancel);
const attachShippingAddressChangeEvent = useAttachEvent(
elementRef,
'shippingaddresschange',
onShippingAddressChange
);
useAttachEvent(elementRef, 'shippingratechange', onShippingRateChange);
const attachShippingRateChangeEvent = useAttachEvent(
elementRef,
'shippingratechange',
onShippingRateChange
);

let readyCallback: UnknownCallback | undefined;
if (type === 'cart') {
Expand All @@ -117,7 +137,7 @@ const createElementComponent = (
}
}

useAttachEvent(elementRef, 'ready', readyCallback);
const attachReadyEvent = useAttachEvent(elementRef, 'ready', readyCallback);

const changeCallback =
type === 'cart'
Expand All @@ -127,7 +147,11 @@ const createElementComponent = (
}
: onChange;

useAttachEvent(elementRef, 'change', changeCallback);
const attachChangeEvent = useAttachEvent(
elementRef,
'change',
changeCallback
);

const checkoutCallback =
type === 'cart'
Expand All @@ -137,7 +161,11 @@ const createElementComponent = (
}
: onCheckout;

useAttachEvent(elementRef, 'checkout', checkoutCallback);
const attachCheckoutEvent = useAttachEvent(
elementRef,
'checkout',
checkoutCallback
);

React.useLayoutEffect(() => {
if (elementRef.current == null && elements && domNode.current != null) {
Expand All @@ -149,6 +177,22 @@ const createElementComponent = (
}
elementRef.current = element;
element.mount(domNode.current);

attachBlurEvent();
attachFocusEvent();
attachEscapeEvent();
attachClickEvent();
attachLoadErrorEvent();
attachLoaderStartEvent();
attachNetworksChangeEvent();
attachLineItemClickEvent();
attachConfirmEvent();
attachCancelEvent();
attachShippingAddressChangeEvent();
attachShippingRateChangeEvent();
attachReadyEvent();
attachChangeEvent();
attachCheckoutEvent();
}
});

Expand Down
21 changes: 17 additions & 4 deletions src/utils/useAttachEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,28 @@ export const useAttachEvent = <A extends unknown[]>(
event: string,
cb?: (...args: A) => any
) => {
React.useEffect(() => {
if (!cb || !elementRef.current) {
return () => {};
const removePreviousEvent = React.useRef(() => {});

const addEventCallback = React.useCallback(() => {
removePreviousEvent.current();

if (!elementRef.current || !cb) {
removePreviousEvent.current = () => {};
return;
}

const element = elementRef.current;

(element as any).on(event, cb);

return () => (element as any).off(event, cb);
removePreviousEvent.current = () => {
if (element === elementRef.current) {
(element as any).off(event, cb);
}
};
}, [cb, event, elementRef]);

React.useLayoutEffect(() => addEventCallback(), [addEventCallback]);
Copy link
Contributor Author

@awalker-stripe awalker-stripe Jan 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useLayoutEffect instead of useEffect makes sure we only call element.on once when first rendering. This is because this layout effect will execute before the layout effect that creates the element, meaning when rendering, elementRef.current is null and it'll exit early on line 16. (Please lmk if there's a better way around this!)


return addEventCallback;
};