Documentation

class Widget

A base class that represents a reusable UI component with encapsulated state.

class Widget extends View;

Description

Widgets are view objects that may include their own state (properties) and event handlers, to render their own view body content.

To be able to use widgets in the view hierarchy, you should also export a builder function that uses the static Widget.builder() method.

For views that require a custom renderer (e.g. a platform-dependent graphic), define a view class that extends Widget, and overrides the Widget.render method altogether.

Note
Widgets are very similar to Activity objects, but they don’t have an active/inactive lifecycle and typically don’t include any application logic (especially not for loading or saving data).

Examples

// Define a widget class to store view state
class CollapsibleWidget extends Widget {
  expanded = false;
  onToggle() {
    this.expanded = !this.expanded;
  }
}

// Export a builder function that uses the class
function Collapsible(title: StringConvertible, ...content: ViewBuilder[]) {
  let width = 300;
  return CollapsibleWidget.builder((v) =>
    UI.Column()
      .width(width, undefined, "100%")
      .with(
        UI.Text(title)
          .icon(v.bind("expanded").then("chevronDown", "chevronNext"))
          .cursor("pointer")
          .onClick("Toggle"),
        UI.ShowWhen(v.bind("expanded"), UI.Column(...content)),
      )
  ).extend({
    expand(set = true) {
      this.initializer.set("expanded", set);
      return this;
    },
    width(w: number) {
      width = w;
      return this;
    },
  });
}

Type members

Static members

Instance members

Inherited members