feat: complete public Form migration and validation focus contract
This commit is contained in:
@@ -189,9 +189,10 @@ export function useChoiceField<T>(
|
||||
dismissed.value = form?.validationErrors.value;
|
||||
}
|
||||
function invalidEvent(event: Event, focus: () => void) {
|
||||
const canceled = event.defaultPrevented;
|
||||
event.preventDefault();
|
||||
touched.value = true;
|
||||
void nextTick(focus);
|
||||
if (!canceled) void nextTick(focus);
|
||||
}
|
||||
function validity(control: HTMLInputElement | undefined) {
|
||||
control?.setCustomValidity(
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, provide, ref, useAttrs } from "vue";
|
||||
import {
|
||||
computed,
|
||||
provide,
|
||||
ref,
|
||||
useAttrs,
|
||||
onMounted,
|
||||
onBeforeUnmount,
|
||||
} from "vue";
|
||||
import {
|
||||
formContextKey,
|
||||
type ValidationBehavior,
|
||||
@@ -13,39 +20,81 @@ const props = withDefaults(
|
||||
}>(),
|
||||
{ validationBehavior: "native", validationErrors: () => ({}) },
|
||||
);
|
||||
const emit = defineEmits<{ invalid: [event: Event] }>();
|
||||
defineSlots<{ default?: () => unknown }>();
|
||||
const attrs = useAttrs();
|
||||
provide(formContextKey, {
|
||||
validationBehavior: computed(() => props.validationBehavior),
|
||||
validationErrors: computed(() => props.validationErrors),
|
||||
});
|
||||
const element = ref<HTMLFormElement>();
|
||||
let validation: { report: boolean; first?: EventTarget | null } | undefined;
|
||||
function invalid(event: Event) {
|
||||
// Native invalid does not bubble. Forward it during capture, while preserving
|
||||
// the same cancelable event for consumer-controlled focus management.
|
||||
if ((event.target as HTMLInputElement).form !== element.value) return;
|
||||
emit("invalid", event);
|
||||
// Composite fields redirect focus from their validation proxy. Only the
|
||||
// first invalid field may do so during report/submit, none during check.
|
||||
if (validation) {
|
||||
if (!validation.report || validation.first) event.preventDefault();
|
||||
validation.first ??= event.target;
|
||||
}
|
||||
}
|
||||
// Use a native capture listener: a Vue capture invoker stamps invalid events
|
||||
// before child Vue listeners run, which can suppress freshly mounted fields'
|
||||
// handlers in the same millisecond. Do not touch Vue's private event markers.
|
||||
onMounted(() => element.value?.addEventListener("invalid", invalid, true));
|
||||
onBeforeUnmount(() =>
|
||||
element.value?.removeEventListener("invalid", invalid, true),
|
||||
);
|
||||
function checkValidity(report = false) {
|
||||
let first:
|
||||
HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement | undefined;
|
||||
for (const control of Array.from(element.value?.elements ?? [])) {
|
||||
if (
|
||||
!("checkValidity" in control) ||
|
||||
(control.getAttribute("data-validation-behavior") ??
|
||||
props.validationBehavior) === "aria"
|
||||
)
|
||||
continue;
|
||||
const field = control as
|
||||
HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement;
|
||||
if (!field.checkValidity() && !first) first = field;
|
||||
}
|
||||
if (report && first) {
|
||||
first.focus();
|
||||
first.reportValidity();
|
||||
let firstEvent: Event | undefined;
|
||||
const previous = validation;
|
||||
validation = { report };
|
||||
try {
|
||||
for (const control of Array.from(element.value?.elements ?? [])) {
|
||||
if (
|
||||
!("checkValidity" in control) ||
|
||||
(control.getAttribute("data-validation-behavior") ??
|
||||
props.validationBehavior) === "aria"
|
||||
)
|
||||
continue;
|
||||
const field = control as
|
||||
HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement;
|
||||
let invalidEvent: Event | undefined;
|
||||
const observe = (event: Event) => {
|
||||
invalidEvent = event;
|
||||
// Associated controls outside the form DOM cannot reach its capture
|
||||
// listener, but are still part of the native form.elements collection.
|
||||
if (!element.value?.contains(field)) invalid(event);
|
||||
};
|
||||
field.addEventListener("invalid", observe, true);
|
||||
try {
|
||||
if (!field.checkValidity() && !first) {
|
||||
first = field;
|
||||
firstEvent = invalidEvent;
|
||||
}
|
||||
} finally {
|
||||
field.removeEventListener("invalid", observe, true);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
validation = previous;
|
||||
}
|
||||
// A second reportValidity() on the control would dispatch invalid twice.
|
||||
// FieldError provides the message; only apply the form's default focus here.
|
||||
if (report && first && !firstEvent?.defaultPrevented) first.focus();
|
||||
return !first;
|
||||
}
|
||||
function submit(event: Event) {
|
||||
const submitter = (event as SubmitEvent).submitter as
|
||||
HTMLButtonElement | HTMLInputElement | null;
|
||||
const booleanAttribute = (value: unknown) => value != null && value !== false;
|
||||
const skip =
|
||||
attrs.novalidate === "" ||
|
||||
attrs.novalidate === true ||
|
||||
attrs.noValidate === true;
|
||||
booleanAttribute(attrs.novalidate) || booleanAttribute(attrs.noValidate);
|
||||
if (!skip && !submitter?.formNoValidate && !checkValidity(true)) {
|
||||
event.preventDefault();
|
||||
event.stopImmediatePropagation();
|
||||
|
||||
@@ -167,9 +167,11 @@ watchEffect(() => {
|
||||
);
|
||||
});
|
||||
function onInvalid(event: Event) {
|
||||
const canceled = event.defaultPrevented;
|
||||
event.preventDefault();
|
||||
showError.value = true;
|
||||
element.value?.querySelector<HTMLButtonElement>("[role=switch]")?.focus();
|
||||
if (!canceled)
|
||||
element.value?.querySelector<HTMLButtonElement>("[role=switch]")?.focus();
|
||||
}
|
||||
function reset(event: Event) {
|
||||
void nextTick(() => {
|
||||
|
||||
Reference in New Issue
Block a user