feat(FormField): wrap error with Presence to be animated

This commit is contained in:
Benjamin Canac
2025-05-14 12:57:40 +02:00
parent 717e35f098
commit 4fad9521b2
7 changed files with 191 additions and 23 deletions

View File

@@ -0,0 +1,47 @@
<script setup lang="ts">
import type { FormError } from '@nuxt/ui'
const state = reactive({
email: undefined,
password: undefined
})
const form = useTemplateRef('form')
const validate = (state: any): FormError[] => {
const errors = []
if (!state.email) errors.push({ name: 'email', message: 'Required' })
if (!state.password) errors.push({ name: 'password', message: 'Required' })
return errors
}
</script>
<template>
<UForm ref="form" :validate="validate" :state="state" class="space-y-4">
<UFormField
label="Email"
name="email"
:ui="{
error: 'data-[state=open]:animate-[fade-in_200ms_ease-out] data-[state=closed]:animate-[fade-out_200ms_ease-in]'
}"
>
<UInput v-model="state.email" />
</UFormField>
<UFormField
label="Password"
name="password"
:ui="{
error: 'data-[state=open]:animate-[fade-in_200ms_ease-out] data-[state=closed]:animate-[fade-out_200ms_ease-in]'
}"
>
<UInput v-model="state.password" type="password" />
</UFormField>
<div class="flex gap-2">
<UButton label="Submit" type="submit" color="neutral" />
<UButton label="Clear" color="neutral" variant="outline" @click="form?.clear()" />
</div>
</UForm>
</template>

View File

@@ -170,6 +170,36 @@ slots:
:u-input{placeholder="Enter your email" class="w-full"}
::
## Examples
### With error animation
You can animate the `error` slot by using the `ui` prop.
::component-example
---
collapse: true
name: 'form-field-error-animation-example'
---
::
::tip
You can also configure this globally in your `app.config.ts`:
```ts
export default defineAppConfig({
ui: {
formField: {
slots: {
error: 'data-[state=open]:animate-[fade-in_200ms_ease-out] data-[state=closed]:animate-[fade-out_200ms_ease-in]'
}
}
}
})
```
::
## API
### Props