Skip to content
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
4 changes: 3 additions & 1 deletion src/lib/core/components/Form/DynamicField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface DynamicFieldProps {
withoutInsertFFDebounce?: boolean;
destroyOnUnregister?: boolean;
mutators?: DynamicFormMutators;
shared?: Record<string, any>;
__mirror?: WonderMirror;
}

Expand All @@ -46,6 +47,7 @@ export const DynamicField: React.FC<DynamicFieldProps> = ({
withoutInsertFFDebounce,
destroyOnUnregister = true,
mutators: externalMutators,
shared: externalShared,
__mirror,
}) => {
const DynamicFormsCtx = useCreateContext();
Expand All @@ -54,7 +56,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 shared = useFormSharedStore(externalShared);

const context = React.useMemo(
() => ({
Expand Down
16 changes: 14 additions & 2 deletions src/lib/core/components/Form/hooks/useFormSharedStore.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import React from 'react';

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

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

React.useEffect(() => {
if (firstRender.current) {
firstRender.current = false;
} else if (shared) {
setStore({
...store,
...shared,
});
}
}, [shared]);

return {store, onChangeShared};
};
4 changes: 3 additions & 1 deletion src/lib/core/components/View/DynamicView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface DynamicViewProps {
}>;
Monaco?: React.ComponentType<MonacoEditorProps>;
showLayoutDescription?: boolean;
shared?: Record<string, any>;
}

export const DynamicView = ({
Expand All @@ -30,9 +31,10 @@ export const DynamicView = ({
Link,
Monaco,
showLayoutDescription,
shared: externalShared,
}: DynamicViewProps) => {
const DynamicFormsCtx = useCreateContext();
const shared = useViewSharedStore();
const shared = useViewSharedStore(externalShared);

const context = React.useMemo(
() => ({
Expand Down
16 changes: 14 additions & 2 deletions src/lib/core/components/View/hooks/useViewSharedStore.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import React from 'react';

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

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

React.useEffect(() => {
if (firstRender.current) {
firstRender.current = false;
} else if (shared) {
setStore({
...store,
...shared,
});
}
}, [shared]);

return {store, onChangeShared};
};
Loading