Skip to content

Commit

Permalink
remove FormizConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoPerard committed Aug 23, 2023
1 parent dc29522 commit f4f7c84
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 26 deletions.
11 changes: 1 addition & 10 deletions apps/examples/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,7 @@ function MyApp({ Component, pageProps }: AppProps) {
<title>Formiz Examples</title>
</Head>
<ChakraProvider>
<FormizProvider
config={{
required: (formattedValue) => {
const defaultValidation = !formattedValue && formattedValue !== 0;
return typeof formattedValue === "string"
? !formattedValue.trim()
: defaultValidation;
},
}}
>
<FormizProvider>
<Component {...pageProps} />
<FormizDevTools />
</FormizProvider>
Expand Down
13 changes: 2 additions & 11 deletions packages/formiz-core/src/FormizProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import { FormizConfig, FormizProviderProps, Store } from "@/types";
import { FormizProviderProps, Store } from "@/types";
import { createContext, useContext, useMemo, useRef, useState } from "react";
import { UseBoundStore, StoreApi } from "zustand";

const defaultConfig: FormizConfig = {
required: (formattedValue) => !formattedValue && formattedValue !== 0,
};

type FormizContextValue = {
config: FormizConfig;
stores: Array<UseBoundStore<StoreApi<Store>>>;
registerStore(store: UseBoundStore<StoreApi<Store>>): void;
unregisterStore(store: UseBoundStore<StoreApi<Store>>): void;
};

const FormizContext = createContext<FormizContextValue>({
config: defaultConfig,
stores: [],
registerStore: () => {},
unregisterStore: () => {},
Expand All @@ -30,15 +24,12 @@ export const useFormizContext = () => {
return formizContext;
};

export const FormizProvider = ({ children, config }: FormizProviderProps) => {
const configRef = useRef(config);

export const FormizProvider = ({ children }: FormizProviderProps) => {
const [stores, setStores] = useState<Array<UseBoundStore<StoreApi<Store>>>>(
[]
);
const contextValue = useMemo(() => {
const value: FormizContextValue = {
config: { ...defaultConfig, ...configRef.current },
stores,
registerStore: (newStore) =>
setStores((currentStores) => [...currentStores, newStore]),
Expand Down
5 changes: 0 additions & 5 deletions packages/formiz-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,12 +479,7 @@ export interface FormizProps {
children?: React.ReactNode;
}

export interface FormizConfig {
required: (value: any, rawValue: any) => boolean;
}

export interface FormizProviderProps {
config?: FormizConfig;
children: React.ReactNode;
}

Expand Down

0 comments on commit f4f7c84

Please sign in to comment.