Skip to content

Commit 87854ca

Browse files
authored
feat: add shared store to context (#238)
1 parent 0520a0a commit 87854ca

File tree

12 files changed

+76
-5
lines changed

12 files changed

+76
-5
lines changed

src/lib/core/components/Form/DynamicField.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
useCreateContext,
1515
useCreateSearchContext,
1616
useDynamicFieldMirror,
17+
useFormSharedStore,
1718
useIntegrationFF,
1819
useMutators,
1920
useSearchStore,
@@ -53,6 +54,7 @@ export const DynamicField: React.FC<DynamicFieldProps> = ({
5354
const watcher = useIntegrationFF(store, withoutInsertFFDebounce, destroyOnUnregister);
5455
const {mutatorsStore, mutateDFState} = useMutators(externalMutators);
5556
const {store: searchStore, setField, removeField, isHiddenField} = useSearchStore();
57+
const shared = useFormSharedStore();
5658

5759
const context = React.useMemo(
5860
() => ({
@@ -61,10 +63,21 @@ export const DynamicField: React.FC<DynamicFieldProps> = ({
6163
generateRandomValue,
6264
tools: {...tools, mutateDFState},
6365
store,
66+
shared,
6467
mutatorsStore,
6568
__mirror,
6669
}),
67-
[tools, config, Monaco, __mirror, generateRandomValue, mutatorsStore, mutateDFState, store],
70+
[
71+
tools,
72+
shared,
73+
config,
74+
Monaco,
75+
__mirror,
76+
generateRandomValue,
77+
mutatorsStore,
78+
mutateDFState,
79+
store,
80+
],
6881
);
6982

7083
const searchContext = React.useMemo(

src/lib/core/components/Form/hooks/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ export * from './useControllerMirror';
22
export * from './useCreateContext';
33
export * from './useDynamicFieldMirror';
44
export * from './useDynamicFormsCtx';
5+
export * from './useFormShared';
6+
export * from './useFormSharedStore';
57
export * from './useGenerateRandomValue';
68
export * from './useIntegrationFF';
79
export * from './useMutateDFState';
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {useDynamicFormsCtx} from './useDynamicFormsCtx';
2+
3+
export const useFormShared = <SharedStore extends Record<string, any>>() =>
4+
useDynamicFormsCtx().shared as {
5+
store: SharedStore;
6+
onChangeShared: <Name extends keyof SharedStore, Value extends SharedStore[Name]>(
7+
name: Name,
8+
value: Value,
9+
) => void;
10+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React from 'react';
2+
3+
export const useFormSharedStore = () => {
4+
const [store, setStore] = React.useState({});
5+
6+
const onChangeShared = React.useCallback(
7+
(name: string, value: any) => setStore((s) => ({...s, [name]: value})),
8+
[setStore],
9+
);
10+
11+
return {store, onChangeShared};
12+
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export * from './constants';
22
export * from './Controller';
33
export * from './DynamicField';
4-
export {useMutateDFState, useStoreValue} from './hooks';
4+
export {useMutateDFState, useStoreValue, useFormShared} from './hooks';
55
export * from './types';
66
export * from './utils';

src/lib/core/components/Form/types/context.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ export interface DynamicFormsContext {
2626
mutateDFState: (mutators: DynamicFormMutators) => void;
2727
};
2828
store: DynamicFieldStore;
29+
shared: {
30+
store: Record<string, any>;
31+
onChangeShared: (name: string, value: any) => void;
32+
};
2933
mutatorsStore: DynamicFormMutatorsStore;
3034
__mirror?: WonderMirror;
3135
}

src/lib/core/components/View/DynamicView.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {FormValue, Spec} from '../../types';
88

99
import {ViewController} from './ViewController';
1010
import {isCorrectViewConfig} from './helpers';
11-
import {useCreateContext} from './hooks';
11+
import {useCreateContext, useViewSharedStore} from './hooks';
1212
import {DynamicViewConfig} from './types';
1313

1414
export interface DynamicViewProps {
@@ -32,6 +32,7 @@ export const DynamicView = ({
3232
showLayoutDescription,
3333
}: DynamicViewProps) => {
3434
const DynamicFormsCtx = useCreateContext();
35+
const shared = useViewSharedStore();
3536

3637
const context = React.useMemo(
3738
() => ({
@@ -40,8 +41,9 @@ export const DynamicView = ({
4041
showLayoutDescription,
4142
Link,
4243
Monaco: isValidElementType(Monaco) ? Monaco : undefined,
44+
shared,
4345
}),
44-
[config, value, Link, Monaco, showLayoutDescription],
46+
[config, value, Link, Monaco, showLayoutDescription, shared],
4547
);
4648

4749
if (isCorrectSpec(spec) && isCorrectViewConfig(config)) {

src/lib/core/components/View/hooks/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ export * from './useCreateContext';
33
export * from './useDynamicFormsCtx';
44
export * from './useRender';
55
export * from './useMonaco';
6+
export * from './useViewShared';
7+
export * from './useViewSharedStore';
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {useDynamicFormsCtx} from './useDynamicFormsCtx';
2+
3+
export const useViewShared = <SharedStore extends Record<string, any>>() =>
4+
useDynamicFormsCtx().shared as {
5+
store: SharedStore;
6+
onChangeShared: <Name extends keyof SharedStore, Value extends SharedStore[Name]>(
7+
name: Name,
8+
value: Value,
9+
) => void;
10+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React from 'react';
2+
3+
export const useViewSharedStore = () => {
4+
const [store, setStore] = React.useState({});
5+
6+
const onChangeShared = React.useCallback(
7+
(name: string, value: any) => setStore((s) => ({...s, [name]: value})),
8+
[setStore],
9+
);
10+
11+
return {store, onChangeShared};
12+
};

0 commit comments

Comments
 (0)