Files
heroui-vue/apps/docs/demos/form-foundation.vue
T
2026-10-21 22:33:19 +08:00

87 lines
2.2 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { ref } from "vue";
import {
Button,
Description,
FieldError,
Form,
Input,
Label,
TextArea,
TextField,
Switch,
SwitchContent,
SwitchControl,
SwitchThumb,
type ValidationErrors,
} from "@heroui-vue/components";
const result = ref("");
const validationErrors = ref<ValidationErrors>({});
function submit(event: Event) {
const data = new FormData(event.currentTarget as HTMLFormElement);
result.value = JSON.stringify(Object.fromEntries(data), null, 2);
}
function reset() {
result.value = "";
validationErrors.value = {};
}
</script>
<template>
<Form
class="flex w-full max-w-md flex-col gap-4"
:validation-errors="validationErrors"
@submit.prevent="submit"
@reset="reset"
>
<TextField
name="email"
type="email"
is-required
default-value=""
full-width
>
<Label>邮箱</Label>
<Input placeholder="name@example.com" />
<Description>输入有效邮箱后可提交,必填错误会在提交时显示。</Description>
<FieldError />
</TextField>
<TextField
name="bio"
default-value=""
full-width
:validate="
(value) => (value.length > 80 ? '个人简介不能超过 80 个字符。' : null)
"
>
<Label>个人简介</Label>
<TextArea placeholder="介绍一下你自己…" :rows="3" />
<FieldError />
</TextField>
<Switch name="notifications" default-checked>
<SwitchContent>
<SwitchControl><SwitchThumb /></SwitchControl>
<Label>接收活动通知</Label>
</SwitchContent>
<Description>关闭时原生 FormData 不包含此项。</Description>
</Switch>
<div class="flex flex-wrap gap-2">
<Button type="submit">提交</Button>
<Button type="reset" variant="secondary">重置</Button>
<Button
variant="tertiary"
@click="
validationErrors = { email: '此邮箱已被使用,请修改后再提交。' }
"
>
显示服务端错误
</Button>
</div>
<pre
v-if="result"
aria-label="提交结果"
role="status"
class="whitespace-pre-wrap text-xs"
>{{ result }}</pre>
</Form>
</template>