页面·免费
注册表单(Vue)
用 TypeScript 写的 Vue 3 单文件组件注册表单,包含姓名、邮箱和密码。离开某个字段后会提示哪里有问题,读屏软件也会读出同样的提示。不依赖任何库;颜色来自 CSS 变量。
免费安装。
如果你的 Android 设备上装了 PocketCode,这会在应用里打开此页面。
预览
SignupForm.vue · 211 行
<!--
Sign-up form for Vue 3 (script setup + TypeScript), with no libraries.
Each field says what is wrong under it after you leave it or press "Create account",
and screen readers hear the same message. The parent gets the values in `submit`:
<SignupForm :pending="saving" @submit="createAccount" />
Colours come from CSS variables with fallbacks: set --form-text, --form-muted,
--form-border, --form-error, --form-primary-bg and --form-primary-text to match your app.
-->
<script setup lang="ts">
import { computed, reactive, ref, useId } from 'vue';
export interface SignupValues {
name: string;
email: string;
password: string;
}
const props = withDefaults(defineProps<{ pending?: boolean; minPassword?: number }>(), {
pending: false,
minPassword: 8,
});
const emit = defineEmits<{ submit: [values: SignupValues] }>();
const id = useId();
const values = reactive<SignupValues>({ name: '', email: '', password: '' });
const touched = reactive({ name: false, email: false, password: false });
const showPassword = ref(false);
const errors = computed(() => ({
name: values.name.trim() ? '' : 'Enter your name.',
email: /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(values.email.trim()) ? '' : 'Enter an email like [email protected].',
password:
values.password.length >= props.minPassword ? '' : `Use at least ${props.minPassword} characters.`,
}));
type Field = keyof SignupValues;
function errorFor(field: Field): string {
return touched[field] ? errors.value[field] : '';
}
function onSubmit() {
touched.name = touched.email = touched.password = true;
const firstBad = (['name', 'email', 'password'] as const).find((f) => errors.value[f]);
if (firstBad) {
document.getElementById(`${id}-${firstBad}`)?.focus();
return;
}
emit('submit', { name: values.name.trim(), email: values.email.trim(), password: values.password });
}
</script>
<template>
<form class="signup" novalidate :aria-busy="pending" @submit.prevent="onSubmit">
<h2 class="title">Create your account</h2>
<div class="field">
<label :for="`${id}-name`">Name</label>
<input
:id="`${id}-name`"
v-model="values.name"
type="text"
autocomplete="name"
:aria-invalid="!!errorFor('name')"
:aria-describedby="errorFor('name') ? `${id}-name-error` : undefined"
@blur="touched.name = true"
/>
<p v-if="errorFor('name')" :id="`${id}-name-error`" class="error">{{ errorFor('name') }}</p>
</div>
<div class="field">
<label :for="`${id}-email`">Email</label>
<input
:id="`${id}-email`"
v-model="values.email"
type="email"
inputmode="email"此预览已截短。安装后可获得完整内容。
1.0.0 的变更
首个版本。