feat: complete public Form migration and validation focus contract
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from "vue";
|
||||
import { IconCheck } from "@tabler/icons-vue";
|
||||
import {
|
||||
Button,
|
||||
Description,
|
||||
FieldError,
|
||||
Form,
|
||||
Input,
|
||||
Label,
|
||||
TextField,
|
||||
} from "@heroui-vue/components";
|
||||
|
||||
const result = ref("");
|
||||
function onSubmit(event: Event) {
|
||||
event.preventDefault();
|
||||
const formData = new FormData(event.currentTarget as HTMLFormElement);
|
||||
const data: Record<string, string> = {};
|
||||
formData.forEach((value, key) => {
|
||||
data[key] = value.toString();
|
||||
});
|
||||
result.value = `表单提交数据:${JSON.stringify(data, null, 2)}`;
|
||||
}
|
||||
function validateEmail(value: string) {
|
||||
return /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i.test(value)
|
||||
? null
|
||||
: "请输入有效的邮箱地址";
|
||||
}
|
||||
function validatePassword(value: string) {
|
||||
if (value.length < 8) return "密码至少需要 8 个字符";
|
||||
if (!/[A-Z]/.test(value)) return "密码至少需要包含一个大写字母";
|
||||
if (!/[0-9]/.test(value)) return "密码至少需要包含一个数字";
|
||||
return null;
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Form
|
||||
class="flex w-96 max-w-full flex-col gap-4"
|
||||
data-custom="foo"
|
||||
@submit="onSubmit"
|
||||
@reset="result = ''"
|
||||
>
|
||||
<TextField is-required name="email" type="email" :validate="validateEmail">
|
||||
<Label>邮箱</Label>
|
||||
<Input placeholder="john@example.com" />
|
||||
<FieldError />
|
||||
</TextField>
|
||||
<TextField
|
||||
is-required
|
||||
:min-length="8"
|
||||
name="password"
|
||||
type="password"
|
||||
:validate="validatePassword"
|
||||
>
|
||||
<Label>密码</Label>
|
||||
<Input placeholder="输入密码" />
|
||||
<Description>至少 8 个字符,且包含 1 个大写字母和 1 个数字</Description>
|
||||
<FieldError />
|
||||
</TextField>
|
||||
<div class="flex gap-2">
|
||||
<Button type="submit">
|
||||
<IconCheck aria-hidden="true" class="size-4 shrink-0" />
|
||||
提交
|
||||
</Button>
|
||||
<Button type="reset" variant="secondary">重置</Button>
|
||||
</div>
|
||||
<pre
|
||||
v-if="result"
|
||||
role="status"
|
||||
aria-label="提交结果"
|
||||
class="whitespace-pre-wrap break-all text-xs"
|
||||
>{{ result }}</pre>
|
||||
</Form>
|
||||
</template>
|
||||
Reference in New Issue
Block a user