@gravity-ui/dynamic-forms 4.13.0 → 4.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,14 +11,14 @@ const helpers_1 = require("../../helpers");
11
11
  const Controller_1 = require("./Controller");
12
12
  const hooks_1 = require("./hooks");
13
13
  const utils_1 = require("./utils");
14
- const DynamicField = ({ name, spec, config, Monaco, generateRandomValue, search, withoutInsertFFDebounce, destroyOnUnregister = true, mutators: externalMutators, __mirror, }) => {
14
+ const DynamicField = ({ name, spec, config, Monaco, generateRandomValue, search, withoutInsertFFDebounce, destroyOnUnregister = true, mutators: externalMutators, shared: externalShared, __mirror, }) => {
15
15
  const DynamicFormsCtx = (0, hooks_1.useCreateContext)();
16
16
  const SearchContext = (0, hooks_1.useCreateSearchContext)();
17
17
  const { tools, store } = (0, hooks_1.useStore)(name);
18
18
  const watcher = (0, hooks_1.useIntegrationFF)(store, withoutInsertFFDebounce, destroyOnUnregister);
19
19
  const { mutatorsStore, mutateDFState } = (0, hooks_1.useMutators)(externalMutators);
20
20
  const { store: searchStore, setField, removeField, isHiddenField } = (0, hooks_1.useSearchStore)();
21
- const shared = (0, hooks_1.useFormSharedStore)();
21
+ const shared = (0, hooks_1.useFormSharedStore)(externalShared);
22
22
  const context = react_1.default.useMemo(() => ({
23
23
  config,
24
24
  Monaco: (0, react_is_1.isValidElementType)(Monaco) ? Monaco : undefined,
@@ -3,9 +3,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useFormSharedStore = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const react_1 = tslib_1.__importDefault(require("react"));
6
- const useFormSharedStore = () => {
7
- const [store, setStore] = react_1.default.useState({});
6
+ const useFormSharedStore = (shared) => {
7
+ const firstRender = react_1.default.useRef(true);
8
+ const [store, setStore] = react_1.default.useState(shared || {});
8
9
  const onChangeShared = react_1.default.useCallback((name, value) => setStore((s) => (Object.assign(Object.assign({}, s), { [name]: value }))), [setStore]);
10
+ react_1.default.useEffect(() => {
11
+ if (firstRender.current) {
12
+ firstRender.current = false;
13
+ }
14
+ else if (shared) {
15
+ setStore(Object.assign(Object.assign({}, store), shared));
16
+ }
17
+ }, [shared]);
9
18
  return { store, onChangeShared };
10
19
  };
11
20
  exports.useFormSharedStore = useFormSharedStore;
@@ -8,9 +8,9 @@ const helpers_1 = require("../../helpers");
8
8
  const ViewController_1 = require("./ViewController");
9
9
  const helpers_2 = require("./helpers");
10
10
  const hooks_1 = require("./hooks");
11
- const DynamicView = ({ value, spec, config, Link, Monaco, showLayoutDescription, }) => {
11
+ const DynamicView = ({ value, spec, config, Link, Monaco, showLayoutDescription, shared: externalShared, }) => {
12
12
  const DynamicFormsCtx = (0, hooks_1.useCreateContext)();
13
- const shared = (0, hooks_1.useViewSharedStore)();
13
+ const shared = (0, hooks_1.useViewSharedStore)(externalShared);
14
14
  const context = react_1.default.useMemo(() => ({
15
15
  config,
16
16
  value,
@@ -3,9 +3,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useViewSharedStore = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const react_1 = tslib_1.__importDefault(require("react"));
6
- const useViewSharedStore = () => {
7
- const [store, setStore] = react_1.default.useState({});
6
+ const useViewSharedStore = (shared) => {
7
+ const firstRender = react_1.default.useRef(true);
8
+ const [store, setStore] = react_1.default.useState(shared || {});
8
9
  const onChangeShared = react_1.default.useCallback((name, value) => setStore((s) => (Object.assign(Object.assign({}, s), { [name]: value }))), [setStore]);
10
+ react_1.default.useEffect(() => {
11
+ if (firstRender.current) {
12
+ firstRender.current = false;
13
+ }
14
+ else if (shared) {
15
+ setStore(Object.assign(Object.assign({}, store), shared));
16
+ }
17
+ }, [shared]);
9
18
  return { store, onChangeShared };
10
19
  };
11
20
  exports.useViewSharedStore = useViewSharedStore;
@@ -12,6 +12,7 @@ export interface DynamicFieldProps {
12
12
  withoutInsertFFDebounce?: boolean;
13
13
  destroyOnUnregister?: boolean;
14
14
  mutators?: DynamicFormMutators;
15
+ shared?: Record<string, any>;
15
16
  __mirror?: WonderMirror;
16
17
  }
17
18
  export declare const DynamicField: React.FC<DynamicFieldProps>;
@@ -7,14 +7,14 @@ import { isCorrectSpec } from '../../helpers';
7
7
  import { Controller } from './Controller';
8
8
  import { useCreateContext, useCreateSearchContext, useDynamicFieldMirror, useFormSharedStore, useIntegrationFF, useMutators, useSearchStore, useStore, } from './hooks';
9
9
  import { getDefaultSearchFunction, isCorrectConfig } from './utils';
10
- export const DynamicField = ({ name, spec, config, Monaco, generateRandomValue, search, withoutInsertFFDebounce, destroyOnUnregister = true, mutators: externalMutators, __mirror, }) => {
10
+ export const DynamicField = ({ name, spec, config, Monaco, generateRandomValue, search, withoutInsertFFDebounce, destroyOnUnregister = true, mutators: externalMutators, shared: externalShared, __mirror, }) => {
11
11
  const DynamicFormsCtx = useCreateContext();
12
12
  const SearchContext = useCreateSearchContext();
13
13
  const { tools, store } = useStore(name);
14
14
  const watcher = useIntegrationFF(store, withoutInsertFFDebounce, destroyOnUnregister);
15
15
  const { mutatorsStore, mutateDFState } = useMutators(externalMutators);
16
16
  const { store: searchStore, setField, removeField, isHiddenField } = useSearchStore();
17
- const shared = useFormSharedStore();
17
+ const shared = useFormSharedStore(externalShared);
18
18
  const context = React.useMemo(() => ({
19
19
  config,
20
20
  Monaco: isValidElementType(Monaco) ? Monaco : undefined,
@@ -1,4 +1,4 @@
1
- export declare const useFormSharedStore: () => {
2
- store: {};
1
+ export declare const useFormSharedStore: (shared?: Record<string, any>) => {
2
+ store: Record<string, any>;
3
3
  onChangeShared: (name: string, value: any) => void;
4
4
  };
@@ -1,6 +1,15 @@
1
1
  import React from 'react';
2
- export const useFormSharedStore = () => {
3
- const [store, setStore] = React.useState({});
2
+ export const useFormSharedStore = (shared) => {
3
+ const firstRender = React.useRef(true);
4
+ const [store, setStore] = React.useState(shared || {});
4
5
  const onChangeShared = React.useCallback((name, value) => setStore((s) => (Object.assign(Object.assign({}, s), { [name]: value }))), [setStore]);
6
+ React.useEffect(() => {
7
+ if (firstRender.current) {
8
+ firstRender.current = false;
9
+ }
10
+ else if (shared) {
11
+ setStore(Object.assign(Object.assign({}, store), shared));
12
+ }
13
+ }, [shared]);
5
14
  return { store, onChangeShared };
6
15
  };
@@ -12,5 +12,6 @@ export interface DynamicViewProps {
12
12
  }>;
13
13
  Monaco?: React.ComponentType<MonacoEditorProps>;
14
14
  showLayoutDescription?: boolean;
15
+ shared?: Record<string, any>;
15
16
  }
16
- export declare const DynamicView: ({ value, spec, config, Link, Monaco, showLayoutDescription, }: DynamicViewProps) => JSX.Element | null;
17
+ export declare const DynamicView: ({ value, spec, config, Link, Monaco, showLayoutDescription, shared: externalShared, }: DynamicViewProps) => JSX.Element | null;
@@ -4,9 +4,9 @@ import { isCorrectSpec } from '../../helpers';
4
4
  import { ViewController } from './ViewController';
5
5
  import { isCorrectViewConfig } from './helpers';
6
6
  import { useCreateContext, useViewSharedStore } from './hooks';
7
- export const DynamicView = ({ value, spec, config, Link, Monaco, showLayoutDescription, }) => {
7
+ export const DynamicView = ({ value, spec, config, Link, Monaco, showLayoutDescription, shared: externalShared, }) => {
8
8
  const DynamicFormsCtx = useCreateContext();
9
- const shared = useViewSharedStore();
9
+ const shared = useViewSharedStore(externalShared);
10
10
  const context = React.useMemo(() => ({
11
11
  config,
12
12
  value,
@@ -1,4 +1,4 @@
1
- export declare const useViewSharedStore: () => {
2
- store: {};
1
+ export declare const useViewSharedStore: (shared?: Record<string, any>) => {
2
+ store: Record<string, any>;
3
3
  onChangeShared: (name: string, value: any) => void;
4
4
  };
@@ -1,6 +1,15 @@
1
1
  import React from 'react';
2
- export const useViewSharedStore = () => {
3
- const [store, setStore] = React.useState({});
2
+ export const useViewSharedStore = (shared) => {
3
+ const firstRender = React.useRef(true);
4
+ const [store, setStore] = React.useState(shared || {});
4
5
  const onChangeShared = React.useCallback((name, value) => setStore((s) => (Object.assign(Object.assign({}, s), { [name]: value }))), [setStore]);
6
+ React.useEffect(() => {
7
+ if (firstRender.current) {
8
+ firstRender.current = false;
9
+ }
10
+ else if (shared) {
11
+ setStore(Object.assign(Object.assign({}, store), shared));
12
+ }
13
+ }, [shared]);
5
14
  return { store, onChangeShared };
6
15
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/dynamic-forms",
3
- "version": "4.13.0",
3
+ "version": "4.14.0",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "main": "build/cjs/index.js",