Skip to content

Commit c382f22

Browse files
authored
Merge branch 'main' into date-picker-fix
2 parents 36a6868 + 76998fe commit c382f22

File tree

7 files changed

+44
-9
lines changed

7 files changed

+44
-9
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## [4.14.0](https://github.com/gravity-ui/dynamic-forms/compare/v4.13.0...v4.14.0) (2024-10-17)
4+
5+
6+
### Features
7+
8+
* add ability to use external values for shared context ([#240](https://github.com/gravity-ui/dynamic-forms/issues/240)) ([d1b69cb](https://github.com/gravity-ui/dynamic-forms/commit/d1b69cb8f98672423616b40f245d61120dbf21c4))
9+
310
## [4.13.0](https://github.com/gravity-ui/dynamic-forms/compare/v4.12.0...v4.13.0) (2024-10-15)
411

512

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@gravity-ui/dynamic-forms",
3-
"version": "4.13.0",
3+
"version": "4.14.0",
44
"description": "",
55
"license": "MIT",
66
"main": "build/cjs/index.js",

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)