From 755a1601a2564542e76f4f392c8f0d38a54cb501 Mon Sep 17 00:00:00 2001 From: imanjra Date: Wed, 23 Oct 2024 08:47:49 -0400 Subject: [PATCH] prevent initialization of non-editable views properties --- .../plugins/SchemaIO/components/DynamicIO.tsx | 12 ++++++--- .../core/src/plugins/SchemaIO/utils/index.ts | 25 +++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/app/packages/core/src/plugins/SchemaIO/components/DynamicIO.tsx b/app/packages/core/src/plugins/SchemaIO/components/DynamicIO.tsx index e7931a1a09..eadc3dff7e 100644 --- a/app/packages/core/src/plugins/SchemaIO/components/DynamicIO.tsx +++ b/app/packages/core/src/plugins/SchemaIO/components/DynamicIO.tsx @@ -7,6 +7,7 @@ import { getComponent, getErrorsForView, isCompositeView, + isEditableView, isInitialized, } from "../utils"; import { AncestorsType, SchemaType, ViewPropsType } from "../utils/types"; @@ -63,21 +64,24 @@ function useCustomComponents() { // todo: need to improve initializing the state... refactor this function function useStateInitializer(props: ViewPropsType) { - const { data, schema, onChange } = props; + const { data, onChange } = props; const computedSchema = getComputedSchema(props); const { default: defaultValue } = computedSchema; + const shouldInitialize = useMemo(() => { + return !isCompositeView(computedSchema) && isEditableView(computedSchema); + }, [computedSchema]); const basicData = useMemo(() => { - if (!isCompositeView(schema)) { + if (shouldInitialize) { return data; } - }, [data, schema]); + }, [shouldInitialize, data]); const unboundState = useUnboundState({ computedSchema, props }); useEffect(() => { const { computedSchema, props } = unboundState; const { data, path, root_id } = props || {}; if ( - !isCompositeView(computedSchema) && + shouldInitialize && !isEqual(data, defaultValue) && !isPathUserChanged(path, root_id) && !isNullish(defaultValue) && diff --git a/app/packages/core/src/plugins/SchemaIO/utils/index.ts b/app/packages/core/src/plugins/SchemaIO/utils/index.ts index adf614e4a8..672a81b46c 100644 --- a/app/packages/core/src/plugins/SchemaIO/utils/index.ts +++ b/app/packages/core/src/plugins/SchemaIO/utils/index.ts @@ -92,6 +92,27 @@ const COMPOSITE_VIEWS = [ "ButtonGroupView", ]; +const NON_EDITABLE_VIEWS = [ + "AlertView", + "ArrowNavView", + "ButtonView", + "ErrorView", + "GridView", + "HeaderView", + "HiddenView", + "ImageView", + "JSONView", + "KeyValueView", + "LabelValueView", + "LinkView", + "LoadingView", + "MarkdownView", + "MediaPlayerView", + "ProgressView", + "TableView", + "TagsView", +]; + export function isCompositeView(schema: SchemaType) { return COMPOSITE_VIEWS.includes(schema?.view?.component); } @@ -100,3 +121,7 @@ export function isInitialized(props: ViewPropsType) { const { initialData, path } = props || {}; return !isNullish(get(initialData, path)); } + +export function isEditableView(schema: SchemaType) { + return !NON_EDITABLE_VIEWS.includes(schema?.view?.component); +}