onFormReset
Runs when the owning form is reset. For components defined with form-associated.
onFormReset(fn: () => void): voidonFormReset(() => {
editor.text = "";
});Parameters
| Parameter | Type | Meaning |
|---|---|---|
fn |
() => void |
Runs from the element's formResetCallback |
Before your hooks run, REALM re-reads every declared prop from its attribute, so props with an attribute go back to their initial values automatically. Use this hook for state that is not a prop.
Example
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>onFormReset demo</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@realmorg/realm@alpha/dist/realm.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@realmorg/ui@alpha/dist/theme.css">
<script type="module" src="https://cdn.jsdelivr.net/npm/@realmorg/realm@alpha/dist/realm.js"></script>
</head>
<body>
<import-element from="https://cdn.jsdelivr.net/npm/@realmorg/ui@alpha/components/button.html"></import-element>
<import-element from="./tally-field.html"></import-element>
<form>
<tally-field name="tally"></tally-field>
<ui-button type="reset" variant="ghost">Reset</ui-button>
<output id="reset-log"></output>
</form>
</body>
</html><custom-element name="tally-field" form-associated>
<script>
const props = defineProps({ value: prop.number().default(0) });
onFormReset(() => {
document.getElementById("reset-log").value = "Reset ran";
});
</script>
<ui-button variant="outline" onclick="props.value++">Tally: <slot name="@value"></slot></ui-button>
</custom-element>