import { computed, inject, provide, type ComputedRef, type InjectionKey, } from "vue"; import { getOption } from "@/utils/config"; import type { FieldProps } from "./props"; export type FieldData = { $el: Element | null; props: FieldProps; hasInnerField: boolean; variant?: string; message?: string | string[]; labelId: string; inputAttrs: object; addInnerField: () => void; setInputId: (value: string) => void; setFocus: (value: boolean) => void; setFilled: (value: boolean) => void; setVariant: (value?: string) => void; setMessage: (value?: string | string[]) => void; setInvalid: (value: boolean) => void; }; /** provide/inject type */ type ProvidedField = ComputedRef; /** provide/inject key */ const $FieldKey: InjectionKey = Symbol("FielData"); /** * Provide field component data via dependency injection. * Provided data is a computed ref to ensure reactivity. */ export function provideField(data: ProvidedField): void { provide($FieldKey, data); } /** * Merge the attributes of a parent field with the own attributes of a control. * An own `aria-label` replaces the `aria-labelledby` of the field, because * `aria-labelledby` always wins over `aria-label` in the accessible name * computation and the control would otherwise ignore its own name. * An own `aria-labelledby` still overrides the field one. * A control with its own visible label passes its `ownLabelId`, which is used * instead of the field label, so that several controls in one field do not * share the name of the field. */ export function mergeFieldAttrs( fieldAttrs: object | undefined, attrs: Record, ownLabelId?: string, ): Record { const { "aria-labelledby": labelledby, ...fieldRest } = (fieldAttrs ?? {}) as Record; const hasOwnName = !!attrs["aria-label"] || !!attrs["aria-labelledby"]; return { ...fieldRest, ...(hasOwnName ? {} : { "aria-labelledby": ownLabelId ?? labelledby }), ...attrs, }; } /** Inject parent field component if used inside one. **/ export function injectField(): { parentField: ComputedRef; statusVariantIcon: ComputedRef; statusVariant: ComputedRef; statusMessage: ComputedRef; } { const parentField = inject( $FieldKey, computed(() => undefined), ); /** Get the message prop from parent if it's a Field. */ const statusMessage = computed(() => { if ( (Array.isArray(parentField?.value?.message) && !parentField?.value?.message.length) || (!Array.isArray(parentField?.value?.message) && !parentField?.value?.message) ) return undefined; return parentField?.value.message; }); /** Get the type prop from parent if it's a Field. */ const statusVariant = computed(() => { if (!parentField?.value?.variant) return undefined; if (typeof parentField.value.variant === "string") return parentField.value.variant; if (Array.isArray(parentField.value.variant)) { for (const key in parentField.value.variant as any) { if (parentField.value.variant[key]) return key; } } return undefined; }); const statusVariantIconConfig = getOption("statusVariantIcon", { success: "check", danger: "alert-circle", info: "information", warning: "alert", }); /** Icon name based on the variant. */ const statusVariantIcon = computed(() => { if (!statusVariant.value || !statusVariantIconConfig) return ""; return statusVariantIconConfig[statusVariant.value] || ""; }); return { parentField, statusVariant, statusVariantIcon, statusMessage, }; }