Skip to content

Commit

Permalink
feat: add ability to use external values for shared context (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
bocembocem authored Oct 17, 2024
1 parent d55f030 commit d1b69cb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
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};
};

0 comments on commit d1b69cb

Please sign in to comment.