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: mismatch target/recipient origin error #236

Merged
merged 2 commits into from
Mar 22, 2024
Merged
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
13 changes: 5 additions & 8 deletions src/elements/CardExpirationDateElement.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC, ForwardedRef, RefObject, useRef } from 'react';
import React, { FC, ForwardedRef, MutableRefObject, useRef } from 'react';
import type {
CardExpirationDateElement as ICardExpirationDateElement,
CreateCardExpirationDateElementOptions,
Expand Down Expand Up @@ -30,12 +30,12 @@ interface CardExpirationDateElementProps {
style?: ElementStyle;
validateOnChange?: boolean;
value?: CardExpirationDateValue<'static'> | string;
valueRef?: RefObject<ICardExpirationDateElement>;
valueRef?: MutableRefObject<ICardExpirationDateElement>;
}

const CardExpirationDateElementC: FC<
CardExpirationDateElementProps & {
elementRef?: ForwardedRef<ICardExpirationDateElement>;
elementRef?: ForwardedRef<ICardExpirationDateElement | null>;
}
> = ({
'aria-label': ariaLabel,
Expand Down Expand Up @@ -80,13 +80,10 @@ const CardExpirationDateElementC: FC<
value,
},
bt,
elementRef
elementRef,
valueRef
);

if (valueRef?.current) {
element?.setValueRef(valueRef.current);
}

useListener('ready', element, onReady);
useListener('change', element, onChange);
useListener('focus', element, onFocus);
Expand Down
15 changes: 7 additions & 8 deletions src/elements/CardNumberElement.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC, useRef, ForwardedRef, RefObject } from 'react';
import React, { FC, useRef, ForwardedRef, MutableRefObject } from 'react';
import type {
CardNumberElement as ICardNumberElement,
CreateCardNumberElementOptions,
Expand Down Expand Up @@ -34,7 +34,7 @@ interface CardNumberElementProps {
style?: ElementStyle;
validateOnChange?: boolean;
value?: string;
valueRef?: RefObject<ICardNumberElement>;
valueRef?: MutableRefObject<ICardNumberElement | null>;
}

const CardNumberElementC: FC<
Expand Down Expand Up @@ -66,7 +66,9 @@ const CardNumberElementC: FC<
const wrapperRef = useRef<HTMLDivElement>(null);
const element = useElement<
ICardNumberElement,
CreateCardNumberElementOptions
CreateCardNumberElementOptions & {
valueRef?: MutableRefObject<ICardNumberElement | null>;
}
>(
id,
'cardNumber',
Expand All @@ -88,13 +90,10 @@ const CardNumberElementC: FC<
value,
},
bt,
elementRef
elementRef,
valueRef
);

if (valueRef?.current) {
element?.setValueRef(valueRef.current);
}

useListener('ready', element, onReady);
useListener('change', element, onChange);
useListener('focus', element, onFocus);
Expand Down
11 changes: 4 additions & 7 deletions src/elements/CardVerificationCodeElement.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC, useRef, ForwardedRef, RefObject } from 'react';
import React, { FC, useRef, ForwardedRef, MutableRefObject } from 'react';
import type {
CardVerificationCodeElement as ICardVerificationCodeElement,
CreateCardVerificationCodeElementOptions,
Expand Down Expand Up @@ -34,7 +34,7 @@ interface CardVerificationCodeElementProps {
style?: ElementStyle;
validateOnChange?: boolean;
value?: string;
valueRef?: RefObject<ICardVerificationCodeElement>;
valueRef?: MutableRefObject<ICardVerificationCodeElement | null>;
}

const CardVerificationCodeElementC: FC<
Expand Down Expand Up @@ -86,13 +86,10 @@ const CardVerificationCodeElementC: FC<
value,
},
bt,
elementRef
elementRef,
valueRef
);

if (valueRef?.current) {
element?.setValueRef(valueRef.current);
}

useListener('ready', element, onReady);
useListener('change', element, onChange);
useListener('focus', element, onFocus);
Expand Down
11 changes: 4 additions & 7 deletions src/elements/TextElement.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC, useRef, ForwardedRef, RefObject } from 'react';
import React, { FC, useRef, ForwardedRef, MutableRefObject } from 'react';
import type {
TextElement as ITextElement,
CreateTextElementOptions,
Expand Down Expand Up @@ -29,7 +29,7 @@ interface BaseTextElementProps {
transform?: RegExp | [RegExp, string?];
validation?: RegExp;
value?: string;
valueRef?: RefObject<ITextElement>;
valueRef?: MutableRefObject<ITextElement | null>;
}

interface MaskedTextElementProps extends BaseTextElementProps {
Expand Down Expand Up @@ -90,13 +90,10 @@ const TextElementC: FC<
value,
},
bt,
elementRef
elementRef,
valueRef
);

if (valueRef?.current) {
element?.setValueRef(valueRef.current);
}

useListener('ready', element, onReady);
useListener('change', element, onChange);
useListener('focus', element, onFocus);
Expand Down
33 changes: 27 additions & 6 deletions src/elements/useElement.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
import React, { ForwardedRef, useEffect, useRef, useState } from 'react';
import React, {
ForwardedRef,
MutableRefObject,
useEffect,
useRef,
useState,
} from 'react';
import type {
BaseElement,
ElementType,
} from '@basis-theory/basis-theory-js/types/elements';
import { BasisTheoryReact } from '../core';
import { useBasisTheoryValue } from './useBasisTheoryValue';

type ElementWithSetValueRef = BaseElement<any, any> & {
setValueRef: (element: BaseElement<any, any>) => void;
};

const elementHasSetValueRef = (val: unknown): val is ElementWithSetValueRef =>
Boolean((val as ElementWithSetValueRef).setValueRef);

const shallowDifference = <
A extends Record<string, unknown>,
B extends Record<string, unknown>
Expand Down Expand Up @@ -51,7 +64,8 @@ const useElement = <
wrapperRef: React.RefObject<HTMLDivElement>,
options: Options,
btFromProps?: BasisTheoryReact,
ref?: ForwardedRef<Element>
ref?: ForwardedRef<Element>,
targetValueRef?: MutableRefObject<Element | null>
): Element | undefined => {
const bt = useBasisTheoryValue(btFromProps);
const [lastOptions, setLastOptions] = useState<Options>();
Expand All @@ -75,11 +89,18 @@ const useElement = <
ref.current = newElement;
}

newElement.mount(`#${id}`).catch((mountError) => {
setLastOptions(() => {
throw mountError;
(async () => {
await newElement.mount(`#${id}`).catch((mountError) => {
setLastOptions(() => {
throw mountError;
});
});
});

if (elementHasSetValueRef(newElement) && targetValueRef?.current) {
newElement.setValueRef(targetValueRef.current);
}
})();

bt.indexElement(id, newElement);
setLastOptions(options);
}
Expand Down
7 changes: 5 additions & 2 deletions test/elements/CardExpirationDateElement.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ describe('CardExpirationDateElement', () => {
const chance = new Chance();
const refArray = [React.createRef<ICardExpirationDateElement>(), undefined];

const valueRef = React.createRef<ICardExpirationDateElement>();

let id: string;
let wrapperDiv: HTMLDivElement;
let style: ElementStyle;
Expand Down Expand Up @@ -93,7 +95,7 @@ describe('CardExpirationDateElement', () => {
style={style}
validateOnChange={validateOnChange}
value={value}
valueRef={React.createRef()}
valueRef={valueRef}
/>
);

Expand All @@ -117,7 +119,8 @@ describe('CardExpirationDateElement', () => {
},
undefined,
// eslint-disable-next-line unicorn/no-null
typeof ref === 'undefined' ? null : ref // undefined ref gets forwarded as null
typeof ref === 'undefined' ? null : ref, // undefined ref gets forwarded as null,
valueRef // undefined ref gets forwarded as null
);
expect(useListener).toHaveBeenCalledWith('ready', element, onReady);
expect(useListener).toHaveBeenCalledWith('change', element, onChange);
Expand Down
7 changes: 5 additions & 2 deletions test/elements/CardNumberElement.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ describe('CardNumberElement', () => {
const chance = new Chance();
const refArray = [React.createRef<ICardNumberElement>(), undefined];

const valueRef = React.createRef<ICardNumberElement>();

let id: string;
let wrapperDiv: HTMLDivElement;
let style: ElementStyle;
Expand Down Expand Up @@ -93,7 +95,7 @@ describe('CardNumberElement', () => {
style={style}
validateOnChange={validateOnChange}
value={value}
valueRef={React.createRef()}
valueRef={valueRef}
/>
);

Expand All @@ -119,7 +121,8 @@ describe('CardNumberElement', () => {
},
undefined,
// eslint-disable-next-line unicorn/no-null
typeof ref === 'undefined' ? null : ref // undefined ref gets forwarded as null
typeof ref === 'undefined' ? null : ref, // undefined ref gets forwarded as null
valueRef // undefined ref gets forwarded as null
);
expect(useListener).toHaveBeenCalledWith('ready', element, onReady);
expect(useListener).toHaveBeenCalledWith('change', element, onChange);
Expand Down
7 changes: 5 additions & 2 deletions test/elements/CardVerificationCodeElement.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ describe('CardVerificationCodeElement', () => {
const chance = new Chance();
const refArray = [React.createRef<ICardVerificationCodeElement>(), undefined];

const valueRef = React.createRef<ICardVerificationCodeElement>();

let id: string;
let wrapperDiv: HTMLDivElement;
let style: ElementStyle;
Expand Down Expand Up @@ -91,7 +93,7 @@ describe('CardVerificationCodeElement', () => {
style={style}
validateOnChange={validateOnChange}
value={value}
valueRef={React.createRef()}
valueRef={valueRef}
/>
);

Expand All @@ -116,7 +118,8 @@ describe('CardVerificationCodeElement', () => {
},
undefined,
// eslint-disable-next-line unicorn/no-null
typeof ref === 'undefined' ? null : ref // undefined ref gets forwarded as null
typeof ref === 'undefined' ? null : ref, // undefined ref gets forwarded as null
valueRef
);
expect(useListener).toHaveBeenCalledWith('ready', element, onReady);
expect(useListener).toHaveBeenCalledWith('change', element, onChange);
Expand Down
7 changes: 5 additions & 2 deletions test/elements/TextElement.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ describe('TextElement', () => {
const chance = new Chance();
const refArray = [React.createRef<ITextElement>(), undefined];

const valueRef = React.createRef<ITextElement>();

let id: string;
let wrapperDiv: HTMLDivElement;
let style: ElementStyle;
Expand Down Expand Up @@ -99,7 +101,7 @@ describe('TextElement', () => {
transform={transform}
validation={validation}
value={value}
valueRef={React.createRef()}
valueRef={valueRef}
/>
);

Expand All @@ -125,7 +127,8 @@ describe('TextElement', () => {
},
undefined,
// eslint-disable-next-line unicorn/no-null
typeof ref === 'undefined' ? null : ref // undefined ref gets forwarded as null
typeof ref === 'undefined' ? null : ref, // undefined ref gets forwarded as null
valueRef
);
expect(useListener).toHaveBeenCalledWith('ready', element, onReady);
expect(useListener).toHaveBeenCalledWith('change', element, onChange);
Expand Down
Loading