Skip to content
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
17 changes: 9 additions & 8 deletions src/components/CardExpirationDateElement.hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@ const useCardExpirationDateElement = ({

const mask = useMask({ type });

useBtRef({
btRef,
elementRef,
id,
setElementValue,
type,
});

const { _onChange, _onBlur, _onFocus } = useUserEventHandlers({
setElementValue,
element: {
Expand All @@ -56,6 +48,15 @@ const useCardExpirationDateElement = ({
onFocus,
});

useBtRef({
btRef,
elementRef,
id,
setElementValue,
type,
onChange: _onChange,
});

return {
_onBlur,
_onChange,
Expand Down
15 changes: 8 additions & 7 deletions src/components/CardNumberElement.hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,6 @@ export const useCardNumberElement = ({
id,
});

useBtRef({
btRef,
elementRef,
id,
setElementValue,
});

const { _onChange, _onBlur, _onFocus } = useUserEventHandlers({
setElementValue,
transform: [' ', ''],
Expand All @@ -106,6 +99,14 @@ export const useCardNumberElement = ({
onFocus,
});

useBtRef({
btRef,
elementRef,
id,
setElementValue,
onChange: _onChange,
});

return {
elementRef,
elementValue,
Expand Down
15 changes: 8 additions & 7 deletions src/components/CardVerificationCodeElement.hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,6 @@ export const useCardVerificationCodeElement = ({
type,
});

useBtRef({
btRef,
elementRef,
id,
setElementValue,
});

const { _onChange, _onBlur, _onFocus } = useUserEventHandlers({
setElementValue,
element: {
Expand All @@ -59,6 +52,14 @@ export const useCardVerificationCodeElement = ({
onFocus,
});

useBtRef({
btRef,
elementRef,
id,
setElementValue,
onChange: _onChange,
});

return {
elementRef,
elementValue,
Expand Down
15 changes: 8 additions & 7 deletions src/components/TextElement.hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ export const useTextElement = ({

useCleanupStateBeforeUnmount(id);

useBtRef({
btRef,
elementRef,
id,
setElementValue,
});

const { _onChange, _onBlur, _onFocus } = useUserEventHandlers({
setElementValue,
element: {
Expand All @@ -56,6 +49,14 @@ export const useTextElement = ({
transform,
});

useBtRef({
btRef,
elementRef,
id,
setElementValue,
onChange: _onChange,
});

return {
elementRef,
_onChange,
Expand Down
13 changes: 11 additions & 2 deletions src/components/shared/useBtRef.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { compose } from 'ramda';
import type { Dispatch, ForwardedRef, RefObject, SetStateAction } from 'react';
import { useEffect } from 'react';
import type { TextInput } from 'react-native';
Expand All @@ -18,6 +17,7 @@ type UseBtRefProps = {
id: string;
setElementValue: Dispatch<SetStateAction<string>>;
type?: ElementType;
onChange?: (value: string) => void;
};

type CreateBtRefArgs = Omit<UseBtRefProps, 'btRef' | 'setElementValue'> & {
Expand Down Expand Up @@ -99,9 +99,18 @@ export const useBtRef = ({
id,
type,
setElementValue,
onChange,
}: UseBtRefProps) => {
useEffect(() => {
const valueSetter = compose(setElementValue, valueFormatter);
const valueSetter: ValueSetter = (val) => {
const formattedValue = valueFormatter(val);

setElementValue(formattedValue);

if (onChange) {
onChange(formattedValue);
}
};

const newBtRef = createBtRef({
id,
Expand Down
Loading