Skip to content

Commit d24459b

Browse files
author
JB AUBREE
committed
feat: add isLoading boolean and complete playground
1 parent ee476a0 commit d24459b

File tree

9 files changed

+716
-14
lines changed

9 files changed

+716
-14
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,16 @@ Returns an object containing the following properties:
136136
- `validate`: Function to validate the form.
137137
- `errors`: Reactive reference to the current validation errors.
138138
- `isValid`: Reactive reference indicating whether the form is valid.
139+
- `isLoading`: Reactive reference indicating if the form validation is in progress.
139140
- `errorCount`: Reactive reference to the number of errors.
140141
- `clearErrors`: Function to clear validation errors.
141142
- `getErrorMessage`: Function to get the error message for a specific field.
142143
- `focusFirstErroredInput`: Function to focus the first input with an error.
143144
- `focusInput`: Function to focus a specific input.
144145

145-
## Example Tests
146+
## Example
146147

147-
Refer to the `useFormValidation.test.ts` file for comprehensive test cases that demonstrate the functionality and usage of the composable.
148+
Refer to the `playground` folder for comprehensive use cases that demonstrate the functionality and usage of the composable.
148149

149150
## License
150151

playground/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414
"vue": "^3.5.12"
1515
},
1616
"devDependencies": {
17+
"@iconify-json/ph": "^1.1.11",
1718
"@types/node": "^20.16.11",
1819
"@vitejs/plugin-vue": "^5.1.4",
1920
"@vue/tsconfig": "^0.5.1",
2021
"typescript": "~5.5.4",
22+
"unocss": "^0.63.6",
23+
"unuse-ui": "^0.1.24",
2124
"vite": "^5.4.8",
2225
"vue-tsc": "^2.1.6",
2326
"vue-use-form-validation": "workspace:*",

playground/src/App.vue

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,74 @@
11
<script setup lang="ts">
2+
import { UButton, UCard, UContainer, UFormGroup, UIcon, UInput, UNotifications, useToast, UText } from 'unuse-ui'
23
import { ref } from 'vue'
34
import { useFormValidation } from 'vue-use-form-validation'
45
import * as z from 'zod'
56
7+
const toast = useToast()
8+
69
const schema = z.object({
7-
field1: z.string().min(1, 'field1 is required'),
8-
field2: z.string().email('Invalid field2'),
10+
email: z.string().email(),
11+
password: z.string().min(8),
912
})
1013
1114
const form = ref({
12-
field1: '',
13-
field2: '',
15+
email: '',
16+
password: '',
1417
})
1518
16-
const { validate } = useFormValidation(schema, form)
19+
const {
20+
errors,
21+
errorCount,
22+
hasError,
23+
isLoading,
24+
isValid,
25+
focusFirstErroredInput,
26+
focusInput,
27+
getErrorMessage,
28+
validate,
29+
} = useFormValidation(schema, form)
1730
18-
await validate()
31+
async function onSubmit() {
32+
await validate()
33+
if (!isValid.value) {
34+
focusFirstErroredInput()
35+
return
36+
}
37+
toast.add({ title: 'Form is valid !', icon: 'icon-ph-check-circle', color: 'success' })
38+
}
1939
</script>
2040

2141
<template>
22-
Hi
42+
<UContainer class="py-5">
43+
<UText as="h1" label="Form example" class="mb-5" :ui="{ font: 'font-bold', size: 'text-2xl' }" />
44+
<UCard v-if="hasError" class="mb-5" :ui="{ background: 'bg-red-200 dark:bg-red-600', body: { base: 'flex flex-col items-start gap-2' } }">
45+
<UText :label="`Form has ${errorCount} ${errorCount > 1 ? 'errors' : 'error'}:`" :ui="{ font: 'font-bold', size: 'text-lg' }" class="mb-1" />
46+
<div
47+
v-for="
48+
error, i in Object.keys(errors) as Array<keyof typeof errors>
49+
"
50+
:key="i"
51+
class="flex items-center"
52+
>
53+
<UIcon name="icon-ph-dot-bold" color="dark" />
54+
<UButton
55+
class="ml-3" color="dark" :is-padded="false" variant="link"
56+
:label="getErrorMessage(error)"
57+
@click="focusInput({ inputName: error })"
58+
/>
59+
</div>
60+
</UCard>
61+
<form class="flex flex-col gap-3">
62+
<UFormGroup label="Email" :error="getErrorMessage('email')" is-required>
63+
<UInput v-model="form.email" name="email" type="email" placeholder="[email protected]" autofocus size="md" />
64+
</UFormGroup>
65+
<UFormGroup label="Password" :error="getErrorMessage('password')" is-required>
66+
<UInput v-model="form.password" name="password" type="password" placeholder="**********" size="md" />
67+
</UFormGroup>
68+
<UButton label="Submit" is-block :is-loading="isLoading" @click="onSubmit" />
69+
</form>
70+
</UContainer>
71+
<Teleport to="body">
72+
<UNotifications />
73+
</Teleport>
2374
</template>

playground/src/main.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
// @unocss-include
2+
import { UnuseUI } from 'unuse-ui'
13
import { createApp } from 'vue'
24

35
import App from './App.vue'
46

5-
createApp(App).mount('#app')
7+
import 'unuse-ui/dist/style.css'
8+
import 'uno.css'
9+
10+
createApp(App)
11+
.use(UnuseUI)
12+
.mount('#app')

playground/uno.config.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { defineConfig, presetIcons, presetUno } from 'unocss'
2+
import { presetUnuse } from 'unuse-ui'
3+
4+
export default defineConfig({
5+
presets: [
6+
presetIcons({
7+
prefix: 'icon-',
8+
scale: 1,
9+
warn: true,
10+
}),
11+
presetUno(),
12+
presetUnuse(),
13+
],
14+
content: {
15+
pipeline: {
16+
include: [/.*\/unuse-ui\.js(.*)$/, './**/*.vue'],
17+
},
18+
},
19+
})

playground/vite.config.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import vue from '@vitejs/plugin-vue'
1+
import Vue from '@vitejs/plugin-vue'
2+
import Unocss from 'unocss/vite'
23
import { defineConfig } from 'vite'
34

45
export default defineConfig({
56
plugins: [
6-
vue(),
7+
Vue(),
8+
Unocss(),
79
],
810
})

0 commit comments

Comments
 (0)