Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions renderers/lit/src/0.8/ui/checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,10 @@ export class Checkbox extends Root {
];

#setBoundValue(value: boolean) {
if (!this.value || !this.processor) {
if (!this.value || !("path" in this.value) || !this.value.path) {
return;
}

if (!("path" in this.value)) {
return;
}

if (!this.value.path) {
return;
}

this.processor.setData(
this.component,
this.value.path,
value,
this.surfaceId ?? A2uiMessageProcessor.DEFAULT_SURFACE_ID
);
this.updateBoundData(this.value.path, value);
}

#renderField(value: boolean | number) {
Expand Down
18 changes: 2 additions & 16 deletions renderers/lit/src/0.8/ui/datetime-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,10 @@ export class DateTimeInput extends Root {
];

#setBoundValue(value: string) {
if (!this.value || !this.processor) {
if (!this.value || !("path" in this.value) || !this.value.path) {
return;
}

if (!("path" in this.value)) {
return;
}

if (!this.value.path) {
return;
}

this.processor.setData(
this.component,
this.value.path,
value,
this.surfaceId ?? A2uiMessageProcessor.DEFAULT_SURFACE_ID
);
this.updateBoundData(this.value.path, value);
}

#renderField(value: string) {
Expand Down
15 changes: 14 additions & 1 deletion renderers/lit/src/0.8/ui/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { map } from "lit/directives/map.js";
import { effect } from "signal-utils/subtle/microtask-effect";
import { A2uiMessageProcessor } from "@a2ui/web_core/data/model-processor";
import { StringValue } from "@a2ui/web_core/types/primitives";
import { AnyComponentNode, SurfaceID, Theme } from "@a2ui/web_core/types/types";
import { AnyComponentNode, DataValue, SurfaceID, Theme } from "@a2ui/web_core/types/types";
import { themeContext } from "./context/theme.js";
import { structuralStyles } from "./styles.js";
import { componentRegistry } from "./component-registry.js";
Expand Down Expand Up @@ -76,6 +76,19 @@ export class Root extends SignalWatcher(LitElement) {

#weight: string | number = 1;

protected updateBoundData(relativePath: string, value: DataValue) {
if (!this.processor) {
return;
}
this.processor.setData(
this.component,
relativePath,
value,
this.surfaceId ?? A2uiMessageProcessor.DEFAULT_SURFACE_ID
);
this.requestUpdate();
}
Comment on lines +79 to +90
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To further reduce code duplication, you could move the guard clauses from the subclasses into this helper method. This would make the updateBoundData method more robust and simplify the calling code in Checkbox, DateTimeInput, Slider, and TextField.

The signature could be changed to accept the object containing the path, rather than the path string itself.

For example:

protected updateBoundData(valueHolder: { path?: string } | null, value: DataValue) {
  if (!valueHolder || !('path' in valueHolder) || !valueHolder.path) {
    return;
  }

  if (!this.processor) {
    return;
  }

  this.processor.setData(
    this.component,
    valueHolder.path,
    value,
    this.surfaceId ?? A2uiMessageProcessor.DEFAULT_SURFACE_ID
  );
  this.requestUpdate();
}

Then, in the subclasses, the call would be simplified to this.updateBoundData(this.value, value); (or this.text) without any preceding guards.

  protected updateBoundData(valueHolder: { path?: string } | null, value: DataValue) {
    if (!valueHolder || !('path' in valueHolder) || !valueHolder.path) {
      return;
    }
    if (!this.processor) {
      return;
    }
    this.processor.setData(
      this.component,
      valueHolder.path,
      value,
      this.surfaceId ?? A2uiMessageProcessor.DEFAULT_SURFACE_ID
    );
    this.requestUpdate();
  }

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion. Since updateBoundData() is defined in PR #1022 (not this PR), I will keep this PR focused on the three component fixes and address the signature change in #1022 if the maintainers agree with the approach.


static styles = [
structuralStyles,
css`
Expand Down
19 changes: 2 additions & 17 deletions renderers/lit/src/0.8/ui/slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { customElement, property } from "lit/decorators.js";
import { Root } from "./root.js";
import { A2uiMessageProcessor } from "@a2ui/web_core/data/model-processor";
import * as Primitives from "@a2ui/web_core/types/primitives";
import * as Types from "@a2ui/web_core/types/types";
import { classMap } from "lit/directives/class-map.js";
import { styleMap } from "lit/directives/style-map.js";
import { structuralStyles } from "./styles.js";
Expand Down Expand Up @@ -62,24 +61,10 @@ export class Slider extends Root {
];

#setBoundValue(value: string) {
if (!this.value || !this.processor) {
if (!this.value || !("path" in this.value) || !this.value.path) {
return;
}

if (!("path" in this.value)) {
return;
}

if (!this.value.path) {
return;
}

this.processor.setData(
this.component,
this.value.path,
value,
this.surfaceId ?? A2uiMessageProcessor.DEFAULT_SURFACE_ID
);
this.updateBoundData(this.value.path, value);
}

#renderField(value: string | number) {
Expand Down
17 changes: 2 additions & 15 deletions renderers/lit/src/0.8/ui/text-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import { html, css, nothing } from "lit";
import { customElement, property } from "lit/decorators.js";
import { Root } from "./root.js";
import { A2uiMessageProcessor } from "@a2ui/web_core/data/model-processor";
import * as Primitives from "@a2ui/web_core/types/primitives";
import * as Types from "@a2ui/web_core/types/types";
import { Events } from "@a2ui/web_core";
Expand Down Expand Up @@ -76,22 +75,10 @@ export class TextField extends Root {
];

#setBoundValue(value: string) {
if (!this.text || !this.processor) {
if (!this.text || !("path" in this.text) || !this.text.path) {
return;
}
if (!("path" in this.text)) {
return;
}
if (!this.text.path) {
return;
}

this.processor.setData(
this.component,
this.text.path,
value,
this.surfaceId ?? A2uiMessageProcessor.DEFAULT_SURFACE_ID
);
this.updateBoundData(this.text.path, value);
}

#renderField(value: string | number, label: string) {
Expand Down