Skip to content

Events and forms

Pass handlers in on. The callback receives the native event and the element, with types inferred from the tag. For listener options, use { handler, options }, for example { handler, options: { once: true } }. A managed listener is removed with its lifetime.

Pass a signal or function to value or checked. For text, checkboxes, and radio buttons, write the new value to your signal in on.input before the runtime reapplies the bound property. For a select, use on.change. The handler can normalize or reject a proposed value.

Controlled input, select, and checkbox

Loading the example…

View source
forms.ts
import { mount, signal } from "min-reactive-framework";
import type { Dispose } from "min-reactive-framework";
import { button, div, form, input, label, option, p, select } from "min-reactive-framework/html";
import type { DemoLanguage } from "../example-language.js";
export function run(target: HTMLElement, lang: DemoLanguage): Dispose {
const ru = lang === "ru";
const name = signal("");
const role = signal("reader");
const approved = signal(false);
const result = signal("");
const labels = {
reader: ru ? "Читатель" : "Reader",
contributor: ru ? "Участник" : "Contributor",
};
return mount(target, () =>
form(
{
class: "mr-demo-stack mr-demo-form",
on: {
submit: (event) => {
event.preventDefault();
result.value =
name.value.trim() +
" · " +
(role.value === "reader" ? labels.reader : labels.contributor);
},
},
},
label(
ru ? "Имя" : "Name",
input({
name: "name",
required: true,
value: name,
on: {
input: (_event, field) => {
name.value = field.value;
},
},
}),
),
label(
ru ? "Роль" : "Role",
select(
{
value: role,
on: {
change: (_event, field) => {
role.value = field.value;
},
},
},
option({ value: "reader" }, labels.reader),
option({ value: "contributor" }, labels.contributor),
),
),
label(
{ class: "mr-demo-checkbox" },
input({
type: "checkbox",
checked: approved,
on: {
input: (_event, field) => {
approved.value = field.checked;
},
},
}),
ru ? "Подтвердить выбор" : "Confirm selection",
),
div(
{ class: "mr-demo-line" },
button(
{
type: "submit",
disabled: () => !approved.value || !name.value.trim(),
},
ru ? "Сохранить" : "Save",
),
),
p({ id: "form-result", role: "status" }, result),
),
);
}

For a field the browser should manage after initialization, use defaultValue on an input or textarea and defaultChecked on an input. With a controlled form, intercept reset with event.preventDefault() and reset the signals. In native forms, omitting this handler leaves reset with its usual browser behavior.

import { signal } from "min-reactive-framework";
import { option, select } from "min-reactive-framework/html";
const selected = signal<readonly string[]>(["a"]);
function Choices() {
return select(
{
multiple: true,
value: selected,
on: {
change: (_event, field) => {
selected.value = Array.from(field.selectedOptions, (option) => option.value);
},
},
},
option({ value: "a" }, "Alpha"),
option({ value: "b" }, "Beta"),
);
}

When a managed list of options changes, the runtime reapplies the bound selection after the options are in the DOM. For text input, writes are deferred during IME composition and reapplied when composition ends. Equal property values are left alone to preserve the caret during unrelated updates.