Skip to content

Commit

Permalink
rename form state into flow state
Browse files Browse the repository at this point in the history
  • Loading branch information
charlotteisambert committed Jan 29, 2024
1 parent a1efb65 commit 9680f4e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
10 changes: 5 additions & 5 deletions packages/example/src/FlowNavigatorExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,23 @@ export const FlowNavigatorExample = () => {
);
}

const initialFormState = {
const initialFlowState = {
myValue31: false,
myValue4: hasToPassStep4,
};

type FormState = typeof initialFormState;
type FlowState = typeof initialFlowState;

const config = {
Step31: (formState: FormState) => formState.myValue31,
Step4: (formState: FormState) => formState.myValue4,
Step31: (flowState: FlowState) => flowState.myValue31,
Step4: (flowState: FlowState) => flowState.myValue4,
};

return (
<FlowNavigator.Navigator
screenOptions={{headerShown: false}}
config={config}
initialFormState={initialFormState}>
initialFlowState={initialFlowState}>
<FlowNavigator.Screen name="Step1" component={Step1Page} />
<FlowNavigator.Screen name="Step2" component={Step2Navigator} />
<FlowNavigator.Group>
Expand Down
4 changes: 2 additions & 2 deletions packages/lib/src/navigators/createFlowNavigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function FlowNavigator({
screenListeners,
screenOptions,
config,
initialFormState,
initialFlowState,
...rest
}: FlowNavigatorProps) {
const parentNavigation = useNavigation();
Expand All @@ -46,7 +46,7 @@ function FlowNavigator({
FlowActionHelpers<ParamListBase>,
FlowNavigationOptions,
FlowNavigationEventMap
>(buildFlowRouter(quitFlow, { config, initialFormState }), {
>(buildFlowRouter(quitFlow, { config, initialFlowState }), {
id,
initialRouteName,
children,
Expand Down
32 changes: 16 additions & 16 deletions packages/lib/src/routers/FlowRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import {
StackRouter,
StackRouterOptions,
} from "@react-navigation/native";
import { Config, FlowNavigationState, FormState } from "../types/types";
import { Config, FlowNavigationState, FlowState } from "../types/types";

export type FlowRouterOptions = StackRouterOptions;

export type FlowActionHelpers<ParamList extends ParamListBase> = {
goToNextStep(): void;
goToPreviousStep(): void;
quitFlow(): void;
setStoreState(formState: FormState): void;
setStoreState(flowState: FlowState): void;
} & StackActionHelpers<ParamList>;

export type FlowActionType =
Expand All @@ -35,16 +35,16 @@ export type FlowActionType =
| {
type: "SET_STORE_STATE";
source?: string;
payload: { formState: Object };
payload: { flowState: Object };
};

export const buildFlowRouter =
(
quitFlowHelper: () => void,
{
config,
initialFormState,
}: { config: Config<ParamListBase>; initialFormState: FormState }
initialFlowState,
}: { config: Config<ParamListBase>; initialFlowState: FlowState }
) =>
(
options: FlowRouterOptions
Expand All @@ -65,7 +65,7 @@ export const buildFlowRouter =

const disabledRouteNames = Object.entries(config)
.filter(([_, isRouteNameEnabledCb]) => {
return !isRouteNameEnabledCb(initialFormState);
return !isRouteNameEnabledCb(initialFlowState);
})
.map(([routeName]) => routeName);

Expand All @@ -79,7 +79,7 @@ export const buildFlowRouter =
return {
...router.getInitialState(params),
availableRoutes,
formState: initialFormState,
flowState: initialFlowState,
};
},

Expand Down Expand Up @@ -130,14 +130,14 @@ export const buildFlowRouter =
return state;

case "SET_STORE_STATE":
const newFormState = {
...state.formState,
...action.payload.formState,
const newFlowState = {
...state.flowState,
...action.payload.flowState,
};

const disabledRouteNames = Object.entries(config)
.filter(([_, isRouteNameEnabledCb]) => {
return !isRouteNameEnabledCb(newFormState);
return !isRouteNameEnabledCb(newFlowState);
})
.map(([routeName]) => routeName);

Expand All @@ -162,9 +162,9 @@ export const buildFlowRouter =

return {
...state,
formState: {
...state.formState,
...action.payload.formState,
flowState: {
...state.flowState,
...action.payload.flowState,
},
index: state.index - builtRemovedRoute.length,
availableRoutes,
Expand All @@ -186,8 +186,8 @@ export const buildFlowRouter =
quitFlow: () => {
return { type: "QUIT_FLOW" };
},
setStoreState: (formState) => {
return { type: "SET_STORE_STATE", payload: { formState } };
setStoreState: (flowState) => {
return { type: "SET_STORE_STATE", payload: { flowState } };
},
},
};
Expand Down
8 changes: 4 additions & 4 deletions packages/lib/src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ export type FlowNavigationProp<
export type FlowNavigationState<ParamList extends ParamListBase> =
NavigationState<ParamList> & {
availableRoutes: Extract<keyof ParamList, string>[];
formState: FormState;
flowState: FlowState;
// type: "flow"; : TODO add flow type and key
};

export type Config<ParamList = ParamListBase> = {
[k in Partial<keyof ParamList>]: (formState: FormState) => boolean;
[k in Partial<keyof ParamList>]: (flowState: FlowState) => boolean;
};

export type FormState = Object;
export type FlowState = Object;

export type FlowNavigatorProps = DefaultNavigatorOptions<
ParamListBase,
Expand All @@ -63,7 +63,7 @@ export type FlowNavigatorProps = DefaultNavigatorOptions<
StackRouterOptions &
NativeStackNavigationConfig & {
config: Config<ParamListBase>;
initialFormState: FormState;
initialFlowState: FlowState;
};

export type FlowStackNavigationOptions = NativeStackNavigationOptions & {
Expand Down

0 comments on commit 9680f4e

Please sign in to comment.