|
@@ -7,6 +7,7 @@ interface Props {
|
|
|
|
|
|
|
|
interface ContextProps {
|
|
interface ContextProps {
|
|
|
formData: PorterFormData;
|
|
formData: PorterFormData;
|
|
|
|
|
+ formState: PorterFormState;
|
|
|
dispatchAction: (event: PorterFormAction) => void;
|
|
dispatchAction: (event: PorterFormAction) => void;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -16,17 +17,46 @@ export const PorterFormContext = createContext<ContextProps | undefined>(
|
|
|
const { Provider } = PorterFormContext;
|
|
const { Provider } = PorterFormContext;
|
|
|
|
|
|
|
|
export const PorterFormContextProvider: React.FC<Props> = (props) => {
|
|
export const PorterFormContextProvider: React.FC<Props> = (props) => {
|
|
|
- const [state, dispatch] = useReducer(
|
|
|
|
|
- (state: PorterFormState, action: PorterFormAction) => {
|
|
|
|
|
- console.log(action);
|
|
|
|
|
- return state;
|
|
|
|
|
- },
|
|
|
|
|
- {
|
|
|
|
|
- components: [],
|
|
|
|
|
|
|
+ const handleAction = (
|
|
|
|
|
+ state: PorterFormState,
|
|
|
|
|
+ action: PorterFormAction
|
|
|
|
|
+ ): PorterFormState => {
|
|
|
|
|
+ switch (action.type) {
|
|
|
|
|
+ case "init-field":
|
|
|
|
|
+ if (!(action.id in state.components)) {
|
|
|
|
|
+ return {
|
|
|
|
|
+ ...state,
|
|
|
|
|
+ components: {
|
|
|
|
|
+ ...state.components,
|
|
|
|
|
+ [action.id]: action.initValue,
|
|
|
|
|
+ },
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+ break;
|
|
|
|
|
+ case "update-field":
|
|
|
|
|
+ return {
|
|
|
|
|
+ ...state,
|
|
|
|
|
+ components: {
|
|
|
|
|
+ ...state.components,
|
|
|
|
|
+ [action.id]: action.updateFunc(state.components[action.id]),
|
|
|
|
|
+ },
|
|
|
|
|
+ };
|
|
|
}
|
|
}
|
|
|
- );
|
|
|
|
|
|
|
+ return state;
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const [state, dispatch] = useReducer(handleAction, {
|
|
|
|
|
+ components: {},
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
return (
|
|
return (
|
|
|
- <Provider value={{ formData: props.formData, dispatchAction: dispatch }}>
|
|
|
|
|
|
|
+ <Provider
|
|
|
|
|
+ value={{
|
|
|
|
|
+ formData: props.formData,
|
|
|
|
|
+ formState: state,
|
|
|
|
|
+ dispatchAction: dispatch,
|
|
|
|
|
+ }}
|
|
|
|
|
+ >
|
|
|
{props.children}
|
|
{props.children}
|
|
|
</Provider>
|
|
</Provider>
|
|
|
);
|
|
);
|