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

feat: add shared store to context #238

Merged
merged 1 commit into from
Oct 15, 2024
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
15 changes: 14 additions & 1 deletion src/lib/core/components/Form/DynamicField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
useCreateContext,
useCreateSearchContext,
useDynamicFieldMirror,
useFormSharedStore,
useIntegrationFF,
useMutators,
useSearchStore,
Expand Down Expand Up @@ -53,6 +54,7 @@ export const DynamicField: React.FC<DynamicFieldProps> = ({
const watcher = useIntegrationFF(store, withoutInsertFFDebounce, destroyOnUnregister);
const {mutatorsStore, mutateDFState} = useMutators(externalMutators);
const {store: searchStore, setField, removeField, isHiddenField} = useSearchStore();
const shared = useFormSharedStore();

const context = React.useMemo(
() => ({
Expand All @@ -61,10 +63,21 @@ export const DynamicField: React.FC<DynamicFieldProps> = ({
generateRandomValue,
tools: {...tools, mutateDFState},
store,
shared,
mutatorsStore,
__mirror,
}),
[tools, config, Monaco, __mirror, generateRandomValue, mutatorsStore, mutateDFState, store],
[
tools,
shared,
config,
Monaco,
__mirror,
generateRandomValue,
mutatorsStore,
mutateDFState,
store,
],
);

const searchContext = React.useMemo(
Expand Down
2 changes: 2 additions & 0 deletions src/lib/core/components/Form/hooks/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export * from './useControllerMirror';
export * from './useCreateContext';
export * from './useDynamicFieldMirror';
export * from './useDynamicFormsCtx';
export * from './useFormShared';
export * from './useFormSharedStore';
export * from './useGenerateRandomValue';
export * from './useIntegrationFF';
export * from './useMutateDFState';
Expand Down
10 changes: 10 additions & 0 deletions src/lib/core/components/Form/hooks/useFormShared.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {useDynamicFormsCtx} from './useDynamicFormsCtx';

export const useFormShared = <SharedStore extends Record<string, any>>() =>
useDynamicFormsCtx().shared as {
store: SharedStore;
onChangeShared: <Name extends keyof SharedStore, Value extends SharedStore[Name]>(
name: Name,
value: Value,
) => void;
};
12 changes: 12 additions & 0 deletions src/lib/core/components/Form/hooks/useFormSharedStore.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react';

export const useFormSharedStore = () => {
const [store, setStore] = React.useState({});

const onChangeShared = React.useCallback(
(name: string, value: any) => setStore((s) => ({...s, [name]: value})),
[setStore],
);

return {store, onChangeShared};
};
2 changes: 1 addition & 1 deletion src/lib/core/components/Form/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from './constants';
export * from './Controller';
export * from './DynamicField';
export {useMutateDFState, useStoreValue} from './hooks';
export {useMutateDFState, useStoreValue, useFormShared} from './hooks';
export * from './types';
export * from './utils';
4 changes: 4 additions & 0 deletions src/lib/core/components/Form/types/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export interface DynamicFormsContext {
mutateDFState: (mutators: DynamicFormMutators) => void;
};
store: DynamicFieldStore;
shared: {
store: Record<string, any>;
onChangeShared: (name: string, value: any) => void;
};
mutatorsStore: DynamicFormMutatorsStore;
__mirror?: WonderMirror;
}
6 changes: 4 additions & 2 deletions src/lib/core/components/View/DynamicView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {FormValue, Spec} from '../../types';

import {ViewController} from './ViewController';
import {isCorrectViewConfig} from './helpers';
import {useCreateContext} from './hooks';
import {useCreateContext, useViewSharedStore} from './hooks';
import {DynamicViewConfig} from './types';

export interface DynamicViewProps {
Expand All @@ -32,6 +32,7 @@ export const DynamicView = ({
showLayoutDescription,
}: DynamicViewProps) => {
const DynamicFormsCtx = useCreateContext();
const shared = useViewSharedStore();

const context = React.useMemo(
() => ({
Expand All @@ -40,8 +41,9 @@ export const DynamicView = ({
showLayoutDescription,
Link,
Monaco: isValidElementType(Monaco) ? Monaco : undefined,
shared,
}),
[config, value, Link, Monaco, showLayoutDescription],
[config, value, Link, Monaco, showLayoutDescription, shared],
);

if (isCorrectSpec(spec) && isCorrectViewConfig(config)) {
Expand Down
2 changes: 2 additions & 0 deletions src/lib/core/components/View/hooks/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export * from './useCreateContext';
export * from './useDynamicFormsCtx';
export * from './useRender';
export * from './useMonaco';
export * from './useViewShared';
export * from './useViewSharedStore';
10 changes: 10 additions & 0 deletions src/lib/core/components/View/hooks/useViewShared.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {useDynamicFormsCtx} from './useDynamicFormsCtx';

export const useViewShared = <SharedStore extends Record<string, any>>() =>
useDynamicFormsCtx().shared as {
store: SharedStore;
onChangeShared: <Name extends keyof SharedStore, Value extends SharedStore[Name]>(
name: Name,
value: Value,
) => void;
};
12 changes: 12 additions & 0 deletions src/lib/core/components/View/hooks/useViewSharedStore.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react';

export const useViewSharedStore = () => {
const [store, setStore] = React.useState({});

const onChangeShared = React.useCallback(
(name: string, value: any) => setStore((s) => ({...s, [name]: value})),
[setStore],
);

return {store, onChangeShared};
};
2 changes: 1 addition & 1 deletion src/lib/core/components/View/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export * from './DynamicView';
export * from './ViewController';
export * from './helpers';
export * from './types';
export {useDynamicFormsCtx} from './hooks';
export {useDynamicFormsCtx, useViewShared} from './hooks';
4 changes: 4 additions & 0 deletions src/lib/core/components/View/types/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ export interface DynamicViewContext {
link: Spec['viewSpec']['link'];
}>;
Monaco?: React.ComponentType<MonacoEditorProps>;
shared: {
store: Record<string, any>;
onChangeShared: (name: string, value: any) => void;
};
}
Loading