Files
heroui-vue/packages/vue/src/components/field/useTextControl.ts
T
2026-10-21 22:33:19 +08:00

130 lines
3.8 KiB
TypeScript

import {
computed,
inject,
nextTick,
onBeforeUnmount,
onMounted,
onUpdated,
ref,
watch,
} from "vue";
import { mergeIds, formContextKey } from "./context";
import { textFieldContextKey } from "../textfield/context";
interface TextControlProps {
modelValue?: string;
defaultValue?: string;
invalid?: boolean;
variant?: "primary" | "secondary";
fullWidth?: boolean;
type?: string;
}
export function useTextControl(
props: TextControlProps,
attrs: Record<string, unknown>,
emit: (event: "update:modelValue", value: string) => void,
) {
const field = inject(textFieldContextKey, undefined);
const formContext = inject(formContextKey, undefined);
const behavior = () =>
field?.validationBehavior.value ?? formContext?.validationBehavior.value;
const element = ref<HTMLInputElement | HTMLTextAreaElement>();
const initial = props.defaultValue ?? "";
const internal = ref(initial);
field?.initialize(props.modelValue ?? initial);
watch(
() => (typeof attrs.id === "string" ? attrs.id : undefined),
(id) => field?.setControlId(id),
{ immediate: true },
);
const value = computed(
() => props.modelValue ?? field?.value.value ?? internal.value,
);
const attrBoolean = (value: unknown) => value != null && value !== false;
const disabled = () =>
attrBoolean(attrs.disabled) || Boolean(field?.disabled.value);
const readOnly = () =>
attrBoolean(attrs.readonly ?? attrs.readOnly) ||
Boolean(field?.readOnly.value);
const required = () =>
behavior() !== "aria" &&
!readOnly() &&
(attrBoolean(attrs.required) || Boolean(field?.required.value));
const invalid = () =>
Boolean(
props.invalid ||
field?.invalid.value ||
attrs["aria-invalid"] === true ||
attrs["aria-invalid"] === "true",
);
const variant = computed(() => props.variant ?? field?.variant.value);
watch(
() => props.modelValue,
(next) => {
if (next !== undefined) field?.syncValue(next);
},
{ immediate: true },
);
watch(element, (next) => field?.setControl(next), { flush: "post" });
function update(event: Event) {
const next = (event.target as HTMLInputElement).value;
if (!disabled() && !readOnly()) {
internal.value = next;
field?.setValue(next);
emit("update:modelValue", next);
}
void nextTick(() => {
if (element.value) {
element.value.value = value.value;
if (props.modelValue !== undefined) field?.syncValue(value.value);
}
});
}
function reset(event: Event) {
void nextTick(() => {
if (event.defaultPrevented) return;
internal.value = initial;
field?.onReset();
if (props.modelValue !== undefined) field?.syncValue(props.modelValue);
if (element.value) element.value.value = value.value;
});
}
let form: HTMLFormElement | null = null;
function bindForm() {
const next = element.value?.form ?? null;
if (next === form) return;
form?.removeEventListener("reset", reset);
form = next;
form?.addEventListener("reset", reset);
}
onMounted(bindForm);
onUpdated(bindForm);
onBeforeUnmount(() => {
form?.removeEventListener("reset", reset);
field?.setControl(undefined);
});
return {
inheritedAttrs: () => field?.controlAttrs() ?? {},
element,
value,
update,
variant,
invalid,
disabled,
readOnly,
required,
id: () =>
typeof attrs.id === "string" ? attrs.id : field?.controlId.value,
name: () =>
typeof attrs.name === "string" ? attrs.name : field?.name.value,
type: () => props.type ?? field?.type.value ?? "text",
describedBy: () =>
mergeIds(
attrs["aria-describedby"] as string | undefined,
field?.describedBy(),
),
onInvalid: (event: Event) => field?.onInvalid(event),
validationBehavior: behavior,
};
}