feat: establish shared field and form foundation
This commit is contained in:
@@ -1,24 +0,0 @@
|
||||
<script setup lang="ts">
|
||||
import { inject, onBeforeUnmount, useId } from "vue";
|
||||
import { switchKey } from "./context";
|
||||
const ctx = inject(switchKey, undefined);
|
||||
const id = useId();
|
||||
if (ctx) ctx.errorIds.value = [...ctx.errorIds.value, id];
|
||||
onBeforeUnmount(() => {
|
||||
if (ctx)
|
||||
ctx.errorIds.value = ctx.errorIds.value.filter((value) => value !== id);
|
||||
});
|
||||
</script>
|
||||
<template>
|
||||
<p
|
||||
v-if="!ctx || ctx.invalid.value"
|
||||
:id="id"
|
||||
data-slot="field-error"
|
||||
class="hv-field-error"
|
||||
role="alert"
|
||||
>
|
||||
<slot :validation-errors="ctx?.error.value ? [ctx.error.value] : []">
|
||||
{{ ctx?.error.value }}
|
||||
</slot>
|
||||
</p>
|
||||
</template>
|
||||
@@ -1,6 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
import {
|
||||
computed,
|
||||
inject,
|
||||
provide,
|
||||
ref,
|
||||
useId,
|
||||
@@ -14,6 +15,11 @@ import {
|
||||
import { switchVariants, type SwitchVariants } from "@heroui-vue/styles";
|
||||
import { Primitive } from "reka-ui";
|
||||
import { switchKey } from "./context";
|
||||
import {
|
||||
fieldContextKey,
|
||||
formContextKey,
|
||||
errorMessages,
|
||||
} from "../field/context";
|
||||
import SwitchContent from "./SwitchContent.vue";
|
||||
import SwitchControl from "./SwitchControl.vue";
|
||||
import SwitchThumb from "./SwitchThumb.vue";
|
||||
@@ -51,6 +57,13 @@ const emit = defineEmits<{
|
||||
change: [value: boolean];
|
||||
}>();
|
||||
const attrs = useAttrs();
|
||||
const formContext = inject(formContextKey, undefined);
|
||||
const validationMode = computed(
|
||||
() =>
|
||||
props.validationBehavior ??
|
||||
formContext?.validationBehavior.value ??
|
||||
"native",
|
||||
);
|
||||
const rootAttrs = () =>
|
||||
Object.fromEntries(
|
||||
Object.entries(attrs).filter(
|
||||
@@ -74,6 +87,13 @@ const hovered = ref(false),
|
||||
focusVisible = ref(false);
|
||||
const error = ref("");
|
||||
const showError = ref(false);
|
||||
const dismissedErrors = ref<object>();
|
||||
const serverMessages = computed(() => {
|
||||
const source = formContext?.validationErrors.value;
|
||||
return source && source !== dismissedErrors.value && props.name
|
||||
? errorMessages(source[props.name])
|
||||
: [];
|
||||
});
|
||||
const checked = computed(
|
||||
() => props.isSelected ?? props.checked ?? internal.value,
|
||||
);
|
||||
@@ -88,6 +108,7 @@ const descriptionIds = ref<string[]>([]);
|
||||
const errorIds = ref<string[]>([]);
|
||||
function update(value: boolean) {
|
||||
if (disabled.value || readOnly.value) return;
|
||||
dismissedErrors.value = formContext?.validationErrors.value;
|
||||
internal.value = value;
|
||||
emit("update:checked", value);
|
||||
emit("update:isSelected", value);
|
||||
@@ -97,6 +118,17 @@ watch([disabled, readOnly], () => {
|
||||
hovered.value = false;
|
||||
pressed.value = false;
|
||||
});
|
||||
provide(fieldContextKey, {
|
||||
controlId: computed(() => props.id ?? generated),
|
||||
kind: "toggle",
|
||||
disabled,
|
||||
readOnly,
|
||||
required,
|
||||
invalid,
|
||||
errors: computed(() => (error.value ? [error.value] : [])),
|
||||
descriptionIds,
|
||||
errorIds,
|
||||
});
|
||||
provide(switchKey, {
|
||||
hovered,
|
||||
pressed,
|
||||
@@ -114,7 +146,9 @@ provide(switchKey, {
|
||||
update,
|
||||
});
|
||||
watchEffect(() => {
|
||||
const result = props.validate?.(checked.value);
|
||||
const result = serverMessages.value.length
|
||||
? serverMessages.value
|
||||
: props.validate?.(checked.value);
|
||||
error.value =
|
||||
typeof result === "string"
|
||||
? result
|
||||
@@ -123,9 +157,10 @@ watchEffect(() => {
|
||||
: required.value && !checked.value
|
||||
? "请开启此选项。"
|
||||
: "";
|
||||
if (props.validationBehavior === "aria") showError.value = true;
|
||||
if (validationMode.value === "aria" || serverMessages.value.length)
|
||||
showError.value = true;
|
||||
input.value?.setCustomValidity(
|
||||
props.validationBehavior === "aria" || disabled.value || readOnly.value
|
||||
validationMode.value === "aria" || disabled.value || readOnly.value
|
||||
? ""
|
||||
: error.value,
|
||||
);
|
||||
@@ -138,6 +173,7 @@ function onInvalid(event: Event) {
|
||||
function reset(event: Event) {
|
||||
void nextTick(() => {
|
||||
if (event.defaultPrevented) return;
|
||||
dismissedErrors.value = formContext?.validationErrors.value;
|
||||
internal.value = initial;
|
||||
showError.value = false;
|
||||
if (input.value) input.value.checked = checked.value;
|
||||
@@ -188,13 +224,14 @@ defineExpose({
|
||||
ref="input"
|
||||
class="hv-switch__input"
|
||||
type="checkbox"
|
||||
:data-validation-behavior="validationMode"
|
||||
tabindex="-1"
|
||||
aria-hidden="true"
|
||||
:name="name"
|
||||
:value="value ?? 'on'"
|
||||
:checked="checked"
|
||||
:disabled="disabled"
|
||||
:required="required && !readOnly && validationBehavior !== 'aria'"
|
||||
:required="required && !readOnly && validationMode !== 'aria'"
|
||||
@invalid="onInvalid"
|
||||
@change="update(($event.target as HTMLInputElement).checked)"
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user