mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-14 12:14:41 +01:00
43 lines
1.1 KiB
Vue
43 lines
1.1 KiB
Vue
<script setup lang="ts">
|
|
import type { FormError, FormErrorEvent, FormSubmitEvent } from '#ui/types'
|
|
|
|
const state = reactive({
|
|
email: undefined,
|
|
password: undefined
|
|
})
|
|
|
|
const validate = (state: any): FormError[] => {
|
|
const errors = []
|
|
if (!state.email) errors.push({ path: 'email', message: 'Required' })
|
|
if (!state.password) errors.push({ path: 'password', message: 'Required' })
|
|
return errors
|
|
}
|
|
|
|
async function onSubmit(event: FormSubmitEvent<any>) {
|
|
// Do something with data
|
|
console.log(event.data)
|
|
}
|
|
|
|
async function onError(event: FormErrorEvent) {
|
|
const element = document.getElementById(event.errors[0].id)
|
|
element?.focus()
|
|
element?.scrollIntoView({ behavior: 'smooth', block: 'center' })
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<UForm :validate="validate" :state="state" class="space-y-4" @submit="onSubmit" @error="onError">
|
|
<UFormGroup label="Email" name="email">
|
|
<UInput v-model="state.email" />
|
|
</UFormGroup>
|
|
|
|
<UFormGroup label="Password" name="password">
|
|
<UInput v-model="state.password" type="password" />
|
|
</UFormGroup>
|
|
|
|
<UButton type="submit">
|
|
Submit
|
|
</UButton>
|
|
</UForm>
|
|
</template>
|