Skip to content

Commit

Permalink
fix: Update control input IDs on schema change (#2307)
Browse files Browse the repository at this point in the history
This commit updates the IDs of all controls if the schema prop handed over to JSON Forms changes.

closes #2213
  • Loading branch information
LukasBoll committed Apr 16, 2024
1 parent 0ee8bf2 commit c0549f2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/angular/src/library/abstract-control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
JsonFormsState,
JsonSchema,
OwnPropsOfControl,
removeId,
StatePropsOfControl,
} from '@jsonforms/core';
import { Component, Input, OnDestroy, OnInit } from '@angular/core';
Expand Down Expand Up @@ -148,6 +149,7 @@ export abstract class JsonFormsAbstractControl<
if (this.subscription) {
this.subscription.unsubscribe();
}
removeId(this.id);
}

isEnabled(): boolean {
Expand Down
11 changes: 11 additions & 0 deletions packages/react/src/JsonForms.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ export class JsonFormsDispatchRenderer extends React.Component<
}
}

componentDidUpdate(prevProps: JsonFormsProps) {
if (prevProps.schema !== this.props.schema) {
removeId(this.state.id);
this.setState({
id: isControl(this.props.uischema)
? createId(this.props.uischema.scope)
: undefined,
});
}
}

render() {
const {
schema,
Expand Down
17 changes: 17 additions & 0 deletions packages/vue/src/jsonFormsCompositions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
mapStateToLabelProps,
LabelElement,
Categorization,
isControl,
} from '@jsonforms/core';
import {
PropType,
Expand All @@ -45,6 +46,7 @@ import {
onBeforeMount,
onUnmounted,
ref,
watch,
} from 'vue';

/**
Expand Down Expand Up @@ -184,6 +186,21 @@ export function useControl<R, D, P extends {}>(
}
});

watch(
() => (props as unknown as RendererProps).schema,
(newSchema, prevSchem) => {
if (
newSchema !== prevSchem &&
isControl((control.value as any).uischema)
) {
if (id.value) {
removeId(id.value);
}
id.value = createId((control.value as any).uischema.scope);
}
}
);

onUnmounted(() => {
if (id.value) {
removeId(id.value);
Expand Down

0 comments on commit c0549f2

Please sign in to comment.