Skip to content

Commit d1b69cb

Browse files
authored
feat: add ability to use external values for shared context (#240)
1 parent d55f030 commit d1b69cb

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export interface DynamicFieldProps {
3333
withoutInsertFFDebounce?: boolean;
3434
destroyOnUnregister?: boolean;
3535
mutators?: DynamicFormMutators;
36+
shared?: Record<string, any>;
3637
__mirror?: WonderMirror;
3738
}
3839

@@ -46,6 +47,7 @@ export const DynamicField: React.FC<DynamicFieldProps> = ({
4647
withoutInsertFFDebounce,
4748
destroyOnUnregister = true,
4849
mutators: externalMutators,
50+
shared: externalShared,
4951
__mirror,
5052
}) => {
5153
const DynamicFormsCtx = useCreateContext();
@@ -54,7 +56,7 @@ export const DynamicField: React.FC<DynamicFieldProps> = ({
5456
const watcher = useIntegrationFF(store, withoutInsertFFDebounce, destroyOnUnregister);
5557
const {mutatorsStore, mutateDFState} = useMutators(externalMutators);
5658
const {store: searchStore, setField, removeField, isHiddenField} = useSearchStore();
57-
const shared = useFormSharedStore();
59+
const shared = useFormSharedStore(externalShared);
5860

5961
const context = React.useMemo(
6062
() => ({
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
import React from 'react';
22

3-
export const useFormSharedStore = () => {
4-
const [store, setStore] = React.useState({});
3+
export const useFormSharedStore = (shared?: Record<string, any>) => {
4+
const firstRender = React.useRef(true);
5+
const [store, setStore] = React.useState(shared || {});
56

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

12+
React.useEffect(() => {
13+
if (firstRender.current) {
14+
firstRender.current = false;
15+
} else if (shared) {
16+
setStore({
17+
...store,
18+
...shared,
19+
});
20+
}
21+
}, [shared]);
22+
1123
return {store, onChangeShared};
1224
};

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export interface DynamicViewProps {
2121
}>;
2222
Monaco?: React.ComponentType<MonacoEditorProps>;
2323
showLayoutDescription?: boolean;
24+
shared?: Record<string, any>;
2425
}
2526

2627
export const DynamicView = ({
@@ -30,9 +31,10 @@ export const DynamicView = ({
3031
Link,
3132
Monaco,
3233
showLayoutDescription,
34+
shared: externalShared,
3335
}: DynamicViewProps) => {
3436
const DynamicFormsCtx = useCreateContext();
35-
const shared = useViewSharedStore();
37+
const shared = useViewSharedStore(externalShared);
3638

3739
const context = React.useMemo(
3840
() => ({
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
import React from 'react';
22

3-
export const useViewSharedStore = () => {
4-
const [store, setStore] = React.useState({});
3+
export const useViewSharedStore = (shared?: Record<string, any>) => {
4+
const firstRender = React.useRef(true);
5+
const [store, setStore] = React.useState(shared || {});
56

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

12+
React.useEffect(() => {
13+
if (firstRender.current) {
14+
firstRender.current = false;
15+
} else if (shared) {
16+
setStore({
17+
...store,
18+
...shared,
19+
});
20+
}
21+
}, [shared]);
22+
1123
return {store, onChangeShared};
1224
};

0 commit comments

Comments
 (0)