Página·Grátis
Formulário de cadastro (Vue)
Um formulário de cadastro como componente de arquivo único do Vue 3 em TypeScript, com nome, email e senha. Ao sair de um campo ele diz o que está errado, e os leitores de tela ouvem a mesma mensagem. Sem bibliotecas; as cores vêm de variáveis CSS.
Instalação grátis.
Se o PocketCode estiver instalado no seu Android, isto abre a página no app.
Prévia
SignupForm.vue · 211 linhas
<!--
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"Esta prévia está encurtada. O conteúdo completo vem com a instalação.
Mudanças na 1.0.0
Primeira versão.