feat: establish shared field and form foundation
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
<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>
|
||||
@@ -0,0 +1,9 @@
|
||||
<script setup lang="ts">
|
||||
import { Input, Label, TextField } from "@heroui-vue/components";
|
||||
</script>
|
||||
<template>
|
||||
<TextField class="w-full max-w-64" name="email" type="email">
|
||||
<Label>邮箱</Label>
|
||||
<Input placeholder="输入你的邮箱" />
|
||||
</TextField>
|
||||
</template>
|
||||
@@ -0,0 +1,42 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref } from "vue";
|
||||
import {
|
||||
Description,
|
||||
FieldError,
|
||||
Input,
|
||||
Label,
|
||||
TextArea,
|
||||
TextField,
|
||||
} from "@heroui-vue/components";
|
||||
const username = ref("");
|
||||
const bio = ref("");
|
||||
const isUsernameInvalid = computed(
|
||||
() => username.value.length > 0 && username.value.length < 3,
|
||||
);
|
||||
const isBioInvalid = computed(
|
||||
() => bio.value.length > 0 && bio.value.length < 20,
|
||||
);
|
||||
</script>
|
||||
<template>
|
||||
<div class="flex w-full max-w-64 flex-col gap-4">
|
||||
<TextField
|
||||
v-model="username"
|
||||
is-required
|
||||
:is-invalid="isUsernameInvalid"
|
||||
name="username"
|
||||
>
|
||||
<Label>用户名</Label>
|
||||
<Input placeholder="jane_doe" />
|
||||
<FieldError v-if="isUsernameInvalid">
|
||||
用户名至少需要 3 个字符。
|
||||
</FieldError>
|
||||
<Description v-else>为你的资料选择一个唯一的用户名。</Description>
|
||||
</TextField>
|
||||
<TextField v-model="bio" is-required :is-invalid="isBioInvalid" name="bio">
|
||||
<Label>个人简介</Label>
|
||||
<TextArea placeholder="介绍一下你自己…" />
|
||||
<FieldError v-if="isBioInvalid">个人简介至少需要 20 个字符。</FieldError>
|
||||
<Description v-else>至少 20 个字符 ({{ bio.length }}/20).</Description>
|
||||
</TextField>
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,10 @@
|
||||
<script setup lang="ts">
|
||||
import { Description, Input, Label, TextField } from "@heroui-vue/components";
|
||||
</script>
|
||||
<template>
|
||||
<TextField class="w-full max-w-64" name="username">
|
||||
<Label>用户名</Label>
|
||||
<Input placeholder="输入用户名" />
|
||||
<Description>为你的账户选择一个唯一的用户名</Description>
|
||||
</TextField>
|
||||
</template>
|
||||
Reference in New Issue
Block a user