refactor: ♻️ Rewrite authentication page

This commit is contained in:
Jesse Wierzbinski 2024-12-02 17:20:27 +01:00
parent 1194bc4ffb
commit c483f35b99
No known key found for this signature in database
26 changed files with 373 additions and 797 deletions

View file

@ -0,0 +1,16 @@
<script lang="ts" setup>
import { Slot } from "radix-vue";
import { useFormField } from "./useFormField";
const { error, formItemId, formDescriptionId, formMessageId } = useFormField();
</script>
<template>
<Slot
:id="formItemId"
:aria-describedby="!error ? `${formDescriptionId}` : `${formDescriptionId} ${formMessageId}`"
:aria-invalid="!!error"
>
<slot />
</Slot>
</template>

View file

@ -0,0 +1,20 @@
<script lang="ts" setup>
import { cn } from "@/lib/utils";
import type { HTMLAttributes } from "vue";
import { useFormField } from "./useFormField";
const props = defineProps<{
class?: HTMLAttributes["class"];
}>();
const { formDescriptionId } = useFormField();
</script>
<template>
<p
:id="formDescriptionId"
:class="cn('text-sm text-muted-foreground', props.class)"
>
<slot />
</p>
</template>

View file

@ -0,0 +1,19 @@
<script lang="ts" setup>
import { cn } from "@/lib/utils";
import { useId } from "radix-vue";
import { type HTMLAttributes, provide } from "vue";
import { FORM_ITEM_INJECTION_KEY } from "./injectionKeys";
const props = defineProps<{
class?: HTMLAttributes["class"];
}>();
const id = useId();
provide(FORM_ITEM_INJECTION_KEY, id);
</script>
<template>
<div :class="cn('space-y-2', props.class)">
<slot />
</div>
</template>

View file

@ -0,0 +1,23 @@
<script lang="ts" setup>
import { cn } from "@/lib/utils";
import type { LabelProps } from "radix-vue";
import type { HTMLAttributes } from "vue";
import { Label } from "~/components/ui/label";
import { useFormField } from "./useFormField";
const props = defineProps<LabelProps & { class?: HTMLAttributes["class"] }>();
const { error, formItemId } = useFormField();
</script>
<template>
<Label
:class="cn(
error && 'text-destructive',
props.class,
)"
:for="formItemId"
>
<slot />
</Label>
</template>

View file

@ -0,0 +1,16 @@
<script lang="ts" setup>
import { ErrorMessage } from "vee-validate";
import { toValue } from "vue";
import { useFormField } from "./useFormField";
const { name, formMessageId } = useFormField();
</script>
<template>
<ErrorMessage
:id="formMessageId"
as="p"
:name="toValue(name)"
class="text-sm font-medium text-destructive"
/>
</template>

View file

@ -0,0 +1,7 @@
export { default as FormControl } from "./FormControl.vue";
export { default as FormDescription } from "./FormDescription.vue";
export { default as FormItem } from "./FormItem.vue";
export { default as FormLabel } from "./FormLabel.vue";
export { default as FormMessage } from "./FormMessage.vue";
export { FORM_ITEM_INJECTION_KEY } from "./injectionKeys";
export { Field as FormField, Form } from "vee-validate";

View file

@ -0,0 +1,3 @@
import type { InjectionKey } from "vue";
export const FORM_ITEM_INJECTION_KEY = Symbol() as InjectionKey<string>;

View file

@ -0,0 +1,37 @@
import {
FieldContextKey,
useFieldError,
useIsFieldDirty,
useIsFieldTouched,
useIsFieldValid,
} from "vee-validate";
import { inject } from "vue";
import { FORM_ITEM_INJECTION_KEY } from "./injectionKeys";
export function useFormField() {
const fieldContext = inject(FieldContextKey);
const fieldItemContext = inject(FORM_ITEM_INJECTION_KEY);
if (!fieldContext) {
throw new Error("useFormField should be used within <FormField>");
}
const { name } = fieldContext;
const id = fieldItemContext;
const fieldState = {
valid: useIsFieldValid(name),
isDirty: useIsFieldDirty(name),
isTouched: useIsFieldTouched(name),
error: useFieldError(name),
};
return {
id,
name,
formItemId: `${id}-form-item`,
formDescriptionId: `${id}-form-item-description`,
formMessageId: `${id}-form-item-message`,
...fieldState,
};
}