feat: complete public Form migration and validation focus contract

This commit is contained in:
maoyingjie
2026-10-27 18:28:13 +08:00
parent f424fa5597
commit 2c221e0995
22 changed files with 843 additions and 35 deletions
+4
View File
@@ -549,5 +549,9 @@
".hv-avatar--lg",
".hv-avatar--soft"
]
},
"form": {
"exports": ["Form", "FormRoot", "TextField", "Input", "Label", "FieldError", "Button"],
"css": [".hv-textfield", ".hv-input", ".hv-field-error", ".hv-button"]
}
}
+48
View File
@@ -0,0 +1,48 @@
import { h } from "vue";
import {
Form,
FormRoot,
type FormProps,
type FormRootProps,
type FormSlots,
type FormExpose,
} from "@heroui-vue/components";
const props: FormProps = {
validationBehavior: "native",
validationErrors: { email: ["Invalid"] },
action: "/submit",
method: "post",
enctype: "multipart/form-data",
target: "_self",
noValidate: true,
"aria-label": "Account",
class: ["flex", { compact: true }],
onSubmit: (event: SubmitEvent) => event.preventDefault(),
onReset: (event: Event) => event.preventDefault(),
onInvalid: (event: Event) => event.preventDefault(),
};
const rootProps: FormRootProps = props;
const slots: FormSlots = { default: () => h("input", { name: "email" }) };
h(Form, rootProps, { default: slots.default });
h(FormRoot, { validationBehavior: "aria" });
declare const form: FormExpose;
const valid: boolean = form.checkValidity();
const reported: boolean = form.reportValidity();
form.reset();
form.element?.requestSubmit();
// @ts-expect-error validation mode is constrained
const invalidMode: FormProps = { validationBehavior: "automatic" };
// @ts-expect-error field errors accept strings or string arrays
const invalidErrors: FormProps = { validationErrors: { email: 42 } };
// @ts-expect-error no React render function API
const invalidRender: FormProps = { render: () => h("form") };
// @ts-expect-error submit receives native SubmitEvent, not a field value
const invalidSubmit: FormProps = { onSubmit: (value: string) => value };
void [
valid,
reported,
invalidMode,
invalidErrors,
invalidRender,
invalidSubmit,
];