Skip to content
This repository was archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
feat: add basic form
Browse files Browse the repository at this point in the history
  • Loading branch information
ZTL-UwU committed Dec 12, 2023
1 parent 49c68c6 commit c511d7a
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 440 deletions.
15 changes: 0 additions & 15 deletions .eslintrc.cjs

This file was deleted.

3 changes: 3 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export default antfu({
}, {
rules: {
'style/brace-style': ['warn', '1tbs', { allowSingleLine: true }],
'vue/block-order': ['error', {
order: ['template', 'script', 'style'],
}],
},
ignores: ['components/ui/**', '.github/**/*'],
});
109 changes: 72 additions & 37 deletions src/App.vue
Original file line number Diff line number Diff line change
@@ -1,47 +1,82 @@
<script setup lang="ts">
import HelloWorld from './components/HelloWorld.vue';
import TheWelcome from './components/TheWelcome.vue';
</script>

<template>
<header>
<img alt="Vue logo" class="logo" src="./assets/logo.svg" width="125" height="125">

<div class="wrapper">
<HelloWorld msg="You did it!" />
</div>
</header>
<div class="ml-auto mr-auto w-80%">
<FormItem :msg="form.name.msg" label="姓名">
<input v-model="form.name.val" @input="onUpdate">
</FormItem>
<FormItem :msg="form.id.msg" label="身份证">
<input v-model="form.id.val" @input="onUpdate">
</FormItem>
<FormItem :msg="form.phone.msg" label="手机">
<input v-model="form.phone.val" @input="onUpdate">
</FormItem>

<main>
<TheWelcome />
</main>
<button @click="onSubmit">
submit
</button>
</div>
</template>

<style scoped>
header {
line-height: 1.5;
}
<script setup lang="ts">
import { reactive, ref } from 'vue';
import FormItem from './components/FormItem.vue';
import type { TForm, TFormFields } from './types';
.logo {
display: block;
margin: 0 auto 2rem;
}
const form: TForm = reactive({
name: {
val: '',
msg: '',
},
id: {
val: '',
msg: '',
},
phone: {
val: '',
msg: '',
},
});
@media (min-width: 1024px) {
header {
display: flex;
place-items: center;
padding-right: calc(var(--section-gap) / 2);
}
const formRule = {
name: {
reg: /.{2,5}/,
msg: '姓名长度应在2~5',
},
id: {
reg: /^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
msg: '请输入正确的身份证号',
},
phone: {
reg: /^[1][3,4,5,7,8][0-9]{9}$/,
msg: '请输入正确的手机号',
},
};
.logo {
margin: 0 2rem 0 0;
}
const ticketOpen = ref(false);
header .wrapper {
display: flex;
place-items: flex-start;
flex-wrap: wrap;
function checkFormRule() {
let success = true;
for (const field of ['name', 'id', 'phone'] as TFormFields[]) {
const { reg, msg } = formRule[field];
if (reg.test(form[field].val)) {
form[field].msg = '';
success = true && success;
} else {
form[field].msg = msg;
success = false;
}
}
return success;
};
function onUpdate() {
checkFormRule();
}
</style>
function onSubmit() {
const valid = checkFormRule();
if (!valid)
return;
ticketOpen.value = true;
}
</script>
86 changes: 0 additions & 86 deletions src/assets/base.css

This file was deleted.

1 change: 0 additions & 1 deletion src/assets/logo.svg

This file was deleted.

31 changes: 1 addition & 30 deletions src/assets/main.css
Original file line number Diff line number Diff line change
@@ -1,35 +1,6 @@
@import './base.css';

#app {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
font-weight: normal;
}

a,
.green {
text-decoration: none;
color: hsla(160, 100%, 37%, 1);
transition: 0.4s;
padding: 3px;
}

@media (hover: hover) {
a:hover {
background-color: hsla(160, 100%, 37%, 0.2);
}
}

@media (min-width: 1024px) {
body {
display: flex;
place-items: center;
}

#app {
display: grid;
grid-template-columns: 1fr 1fr;
padding: 0 2rem;
}
}
}
16 changes: 16 additions & 0 deletions src/components/FormItem.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<template>
<div>
{{ label }}
</div>
<slot />
<div class="text-red-400">
{{ msg }}
</div>
</template>

<script setup lang="ts">
defineProps<{
label: string
msg: string
}>();
</script>
43 changes: 0 additions & 43 deletions src/components/HelloWorld.vue

This file was deleted.

Loading

0 comments on commit c511d7a

Please sign in to comment.