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

Feature: Frames ref for submitCard() #31

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 6 additions & 3 deletions src/Frames.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import React, { useEffect } from "react";
import React, { useEffect, useImperativeHandle } from "react";
import { View, StyleSheet } from "react-native";
import { framesReducer } from "./utils/reducer";
import {
FramesContextType,
FramesProps,
FramesState,
FramesDispatch,
FramesRef,
} from "./types/types";
import { log } from "./utils/logger";
import { tokenize, formatDataForTokenization } from "./utils/http";

export const FramesContext = React.createContext({} as FramesContextType);

const Frames = (props: FramesProps) => {
const Frames = React.forwardRef<FramesRef, FramesProps>((props, ref) => {
// @ts-ignore
const [state, dispatch]: [FramesState, FramesDispatch] = React.useReducer(
framesReducer,
Expand Down Expand Up @@ -154,14 +155,16 @@ const Frames = (props: FramesProps) => {
}
}, [state.validation.card]);

useImperativeHandle(ref, () => ({ submitCard }));

return (
<View style={[styles.container, props.style]}>
<FramesContext.Provider value={{ state, dispatch, submitCard }}>
{props.children}
</FramesContext.Provider>
</View>
);
};
});

export default Frames;

Expand Down
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export { default as ExpiryDate } from "./components/ExpiryDate";
export { default as Cvv } from "./components/Cvv";
export { default as SubmitButton } from "./components/SubmitButton";
export { default as FramesConsumer } from "./Frames";
export type { FramesRef } from "./types/types";
5 changes: 5 additions & 0 deletions src/types/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,15 @@ export interface FrameValidationChangedParams {
isEmpty: boolean;
}

export interface FramesRef {
submitCard: () => void;
}

export interface FramesProps extends ViewStyle {
style?: StyleProp<ViewStyle>;
children: any;
config: FramesConfig;
ref?: React.Ref<FramesRef>;
frameValidationChanged?: (e: FrameValidationChangedParams) => void;
paymentMethodChanged?: (e: PaymentMethodChangeParams) => void;
cardValidationChanged?: (e: FrameCardValidationChangedEvent) => void;
Expand Down