You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.2 KiB
44 lines
1.2 KiB
<script setup lang="ts">
|
|
import type { HTMLAttributes } from 'vue'
|
|
import { inject } from 'vue'
|
|
import { useFieldError } from 'vee-validate'
|
|
import { cn } from '~/lib/utils'
|
|
import type { FormItemContext } from '.'
|
|
import { FORM_ITEM_INJECT_KEY } from '.'
|
|
|
|
/**
|
|
* FormMessage component - Displays validation error messages
|
|
* Only renders when there is an error for the associated field
|
|
*/
|
|
|
|
interface Props {
|
|
class?: HTMLAttributes['class']
|
|
}
|
|
|
|
const props = defineProps<Props>()
|
|
|
|
// Get the form item context for ID
|
|
const formItemContext = inject<FormItemContext>(FORM_ITEM_INJECT_KEY)
|
|
|
|
// Get the field error message from the nearest parent field
|
|
const error = useFieldError()
|
|
</script>
|
|
|
|
<template>
|
|
<Transition
|
|
enter-active-class="transition-all duration-200 ease-out"
|
|
enter-from-class="opacity-0 -translate-y-1"
|
|
enter-to-class="opacity-100 translate-y-0"
|
|
leave-active-class="transition-all duration-150 ease-in"
|
|
leave-from-class="opacity-100 translate-y-0"
|
|
leave-to-class="opacity-0 -translate-y-1"
|
|
>
|
|
<p
|
|
v-if="error"
|
|
:id="`${formItemContext?.id}-message`"
|
|
:class="cn('form-error', props.class)"
|
|
>
|
|
{{ error }}
|
|
</p>
|
|
</Transition>
|
|
</template>
|
|
|