Skip to content

Commit

Permalink
prevent initialization of non-editable views properties
Browse files Browse the repository at this point in the history
  • Loading branch information
imanjra committed Oct 23, 2024
1 parent f93110f commit 755a160
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
12 changes: 8 additions & 4 deletions app/packages/core/src/plugins/SchemaIO/components/DynamicIO.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getComponent,
getErrorsForView,
isCompositeView,
isEditableView,
isInitialized,
} from "../utils";
import { AncestorsType, SchemaType, ViewPropsType } from "../utils/types";
Expand Down Expand Up @@ -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) &&
Expand Down
25 changes: 25 additions & 0 deletions app/packages/core/src/plugins/SchemaIO/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}

0 comments on commit 755a160

Please sign in to comment.