Widget.builder()
Creates a widget builder for this class.
static builder<T extends Widget>(
this: new () => T,
defineView?: (v: Binding<T>) => ViewBuilder,
): Widget.Builder<T>;
Summary
This static method creates a builder object that can be used to construct instances of the widget class. The builder provides a fluent interface similar to UIElement.ElementBuilder, with methods for configuration and extension.
Parameters
- defineView — Optional function that receives a binding to the widget instance
Return value
A widget builder object
Description
The builder manages a ViewBuilder.Initializer that handles property setting, bindings, and event handlers. The view function is called lazily on first build, and its result is cached for subsequent builds.
The returned builder object exposes:
initializer- The ViewBuilder.Initializer for setting properties and handlersbuild()- Creates a new instance of the widgetapply()- Applies a modifier function to the builderextend()- Extends the builder with custom methods
and returns a view builder defining the widget’s body. If not provided, the body can be set via the defer callback in Widget.Builder.extend, or by overriding the Widget.body getter in the class itself.
Examples
class MyCounterWidget extends Widget {
value = 0;
onIncrement() { this.value++; }
}
function MyCounter() {
return MyCounterWidget.builder((v) =>
UI.Row(
UI.Text(v.bind("value")),
UI.Button("+").onClick("Increment"),
)
).extend({
initialValue(n: number) {
this.initializer.set("value", n);
return this;
},
});
}
Related
class Widget
A base class that represents a reusable UI component with encapsulated state.
