-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new way of declaring forms (#218)
- Loading branch information
1 parent
49f83d5
commit 9546c82
Showing
39 changed files
with
816 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<template> | ||
<div class="bg-opacity-5 bg-white mt-4 px-8 py-4 rounded-lg"> | ||
<div class="auto-rows-auto gap-2 grid grid-cols-[1fr_3fr] grid-flow-row"> | ||
<b>Name</b> | ||
<span v-text="form.name"></span> | ||
|
||
<b>isDirty</b> | ||
<span v-text="form.isDirty" /> | ||
|
||
<b>isValid</b> | ||
<span v-text="form.isValid" /> | ||
|
||
<b>getValues()</b> | ||
<pre v-text="values" /> | ||
|
||
<b>Event log</b> | ||
<textarea | ||
v-model="eventLog" | ||
class="font-mono w-full" | ||
rows="10" | ||
readonly /> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import {computed, toRefs} from 'vue'; | ||
import {type MaybeUnwrapNestedRefs} from '@myparcel-vfb/core'; | ||
import {type FormInstance} from '@myparcel/vue-form-builder'; | ||
import {useFormEventLog} from '../composables/useFormEventLog'; | ||
const props = defineProps<{form: MaybeUnwrapNestedRefs<FormInstance>}>(); | ||
const propRefs = toRefs(props); | ||
const eventLog = useFormEventLog(propRefs.form.value); | ||
const values = computed(() => props.form.getValues()); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<template> | ||
<TSubmitButton | ||
v-if="element" | ||
:element="element" | ||
:outline="outline" | ||
@click="() => form?.submit()"> | ||
<template | ||
v-for="name in Object.keys($slots)" | ||
:key="name" | ||
#[name]="scope"> | ||
<slot | ||
:name="name" | ||
v-bind="scope || {}" /> | ||
</template> | ||
</TSubmitButton> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
import {inject} from 'vue'; | ||
import {INJECT_ELEMENT, INJECT_FORM} from '@myparcel-vfb/core'; | ||
import TSubmitButton from './template/TSubmitButton.vue'; | ||
defineProps<{outline?: boolean}>(); | ||
const form = inject(INJECT_FORM); | ||
const element = inject(INJECT_ELEMENT); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<template> | ||
<div class="bg-red-700 border border-red-800 col-span-2 dark:bg-red-900 mt-3 p-5 rounded-lg"> | ||
<ul> | ||
<li | ||
v-for="error in errors" | ||
:key="error" | ||
v-text="error" /> | ||
</ul> | ||
</div> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
defineProps<{errors: string[]}>(); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import {type Ref, ref} from 'vue'; | ||
import {FormHook, type FormInstance, type InteractiveElement, type MaybeUnwrapNestedRefs} from '@myparcel-vfb/core'; | ||
import {isOfType} from '@myparcel/ts-utils'; | ||
|
||
export const useFormEventLog = (form: MaybeUnwrapNestedRefs<FormInstance>): Ref<string> => { | ||
const eventLog = ref<string>(''); | ||
|
||
Object.values(FormHook).forEach((hook) => { | ||
form.off(hook); | ||
form.on(hook, (_, ...args) => { | ||
const additionalArgs = args as unknown as [unknown, unknown]; | ||
|
||
let log = `${new Date().toISOString()} | ${hook}`; | ||
|
||
if (additionalArgs.length && isOfType<InteractiveElement>(additionalArgs[0], 'ref')) { | ||
log += ` (${additionalArgs[0].name}, ${additionalArgs[1]})`; | ||
} | ||
|
||
eventLog.value = `${log}\n${eventLog.value}`; | ||
}); | ||
}); | ||
|
||
return eventLog; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import {type ComponentOrHtmlElement, type ElementName, type Validator} from '@myparcel-vfb/core'; | ||
|
||
export const emailValidator = (): Validator<ComponentOrHtmlElement, ElementName, string> => ({ | ||
validate: (_, value) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value), | ||
errorMessage: `Does this look like a valid email address to you?`, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from './emailValidator'; | ||
export * from './regexValidator'; | ||
export * from './stringContainsValidator'; | ||
export * from './stringLengthValidator'; | ||
export * from './stringNotContainsValidator'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import {type ComponentOrHtmlElement, type ElementName, type Validator} from '@myparcel-vfb/core'; | ||
|
||
export const regexValidator = (regex: RegExp): Validator<ComponentOrHtmlElement, ElementName, string> => ({ | ||
validate: (_, value) => regex.test(value), | ||
errorMessage: `Value must match ${regex.toString()}`, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {type ComponentOrHtmlElement, type ElementName, type Validator} from '@myparcel/vue-form-builder'; | ||
import {type OneOrMore, toArray} from '@myparcel/ts-utils'; | ||
|
||
export const stringContainsValidator = ( | ||
search: OneOrMore<string>, | ||
): Validator<ComponentOrHtmlElement, ElementName, string> => { | ||
const array = toArray(search); | ||
|
||
return { | ||
validate: (_, value) => array.every((search) => value.includes(search)), | ||
errorMessage: `Value must contain "${array.join('", "')}"`, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {type ComponentOrHtmlElement, type ElementName, type Validator} from '@myparcel-vfb/core'; | ||
|
||
export const stringLengthValidator = ( | ||
minLength: number, | ||
maxLength?: number, | ||
): Validator<ComponentOrHtmlElement, ElementName, string> => { | ||
return { | ||
validate: (_, value) => value.length >= minLength && value.length <= (maxLength ?? Infinity), | ||
errorMessage: maxLength | ||
? `Value must be between ${minLength} and ${maxLength} characters long.` | ||
: `Value must be at least ${minLength} characters long.`, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {type ComponentOrHtmlElement, type ElementName, type Validator} from '@myparcel-vfb/core'; | ||
import {type OneOrMore, toArray} from '@myparcel/ts-utils'; | ||
|
||
export const stringNotContainsValidator = ( | ||
search: OneOrMore<string>, | ||
): Validator<ComponentOrHtmlElement, ElementName, string> => { | ||
const array = toArray(search); | ||
|
||
return { | ||
validate: (_, value) => array.every((search) => !value.includes(search)), | ||
errorMessage: `Value must not contain "${array.join('", "')}"`, | ||
}; | ||
}; |
Oops, something went wrong.