@effect-app/vue-components 0.3.2 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/types/components/OmegaForm/OmegaFormStuff.d.ts +13 -0
- package/dist/types/components/OmegaForm/OmegaInput.vue.d.ts +2 -14
- package/dist/types/components/OmegaForm/OmegaWrapper.vue.d.ts +7 -3
- package/dist/types/components/OmegaForm/index.d.ts +2 -42
- package/dist/types/components/OmegaForm/useOmegaForm.d.ts +6 -2
- package/dist/vue-components.es11.js +43 -33
- package/dist/vue-components.es5.js +88 -61
- package/dist/vue-components.es6.js +1 -1
- package/dist/vue-components.es7.js +2 -2
- package/package.json +3 -1
- package/src/components/OmegaForm/OmegaFormStuff.ts +12 -1
- package/src/components/OmegaForm/OmegaInput.vue +2 -15
- package/src/components/OmegaForm/OmegaWrapper.vue +61 -23
- package/src/components/OmegaForm/index.ts +3 -3
- package/src/components/OmegaForm/useOmegaForm.ts +50 -4
- package/src/stories/OmegaForm/ComplexForm.vue +39 -33
- package/src/stories/OmegaForm/EmailForm.vue +1 -1
- package/src/stories/OmegaForm/Meta.vue +47 -0
- package/src/stories/OmegaForm/OneHundredWaysToWriteAForm.vue +188 -0
- package/src/stories/OmegaForm/PersistencyForm.vue +6 -6
- package/src/stories/OmegaForm/SimpleForm.vue +1 -1
- package/src/stories/OmegaForm/SimpleFormVuetifyDefault.vue +1 -1
- package/src/stories/OmegaForm/SumExample.vue +3 -5
- package/src/stories/OmegaForm/form.Input.vue +86 -0
- package/src/stories/OmegaForm.stories.ts +24 -1
|
@@ -1,7 +1,19 @@
|
|
|
1
1
|
<template>
|
|
2
|
-
<form novalidate @submit.prevent.stop="
|
|
2
|
+
<form novalidate @submit.prevent.stop="formToUse.handleSubmit()">
|
|
3
3
|
<fieldset :disabled="formIsSubmitting">
|
|
4
|
-
|
|
4
|
+
<!-- Render externalForm + default slots if props.form is provided -->
|
|
5
|
+
<template v-if="props.form">
|
|
6
|
+
<slot name="externalForm" :subscribed-values="subscribedValues" />
|
|
7
|
+
<slot />
|
|
8
|
+
<!-- default slot -->
|
|
9
|
+
</template>
|
|
10
|
+
<!-- Render internalForm slot if form was created locally -->
|
|
11
|
+
<slot
|
|
12
|
+
v-else-if="localForm"
|
|
13
|
+
name="internalForm"
|
|
14
|
+
:form="localForm"
|
|
15
|
+
:subscribed-values="subscribedValues"
|
|
16
|
+
/>
|
|
5
17
|
</fieldset>
|
|
6
18
|
</form>
|
|
7
19
|
</template>
|
|
@@ -62,7 +74,7 @@ import {
|
|
|
62
74
|
type OmegaFormReturn,
|
|
63
75
|
useOmegaForm,
|
|
64
76
|
} from "./useOmegaForm"
|
|
65
|
-
import { watch } from "vue"
|
|
77
|
+
import { computed, watch, defineSlots } from "vue"
|
|
66
78
|
|
|
67
79
|
const props = defineProps<
|
|
68
80
|
{
|
|
@@ -81,32 +93,40 @@ const props = defineProps<
|
|
|
81
93
|
)
|
|
82
94
|
>()
|
|
83
95
|
|
|
84
|
-
const
|
|
85
|
-
props.form
|
|
96
|
+
const localForm = computed(() => {
|
|
97
|
+
if (props.form || !props.schema) {
|
|
98
|
+
return undefined
|
|
99
|
+
}
|
|
100
|
+
return useOmegaForm<From, To>(props.schema, props, props.omegaConfig)
|
|
101
|
+
})
|
|
86
102
|
|
|
87
|
-
const
|
|
103
|
+
const formToUse = computed(() => props.form ?? localForm.value!)
|
|
88
104
|
|
|
89
|
-
|
|
105
|
+
const formIsSubmitting = useStore(
|
|
106
|
+
formToUse.value.store,
|
|
107
|
+
state => state.isSubmitting,
|
|
108
|
+
)
|
|
90
109
|
|
|
91
110
|
const subscribedValues = getOmegaStore(
|
|
92
|
-
|
|
111
|
+
formToUse.value as OmegaFormApi<To, From>,
|
|
93
112
|
props.subscribe,
|
|
94
113
|
)
|
|
95
114
|
|
|
96
115
|
const formSubmissionAttempts = useStore(
|
|
97
|
-
|
|
116
|
+
formToUse.value.store,
|
|
98
117
|
state => state.submissionAttempts,
|
|
99
118
|
)
|
|
100
119
|
|
|
101
|
-
const errors =
|
|
120
|
+
const errors = computed(() => formToUse.value.useStore(state => state.errors))
|
|
102
121
|
|
|
103
122
|
watch(
|
|
104
|
-
() => [
|
|
123
|
+
() => [formToUse.value.filterItems, errors.value.value],
|
|
105
124
|
() => {
|
|
106
|
-
const filterItems: FilterItems | undefined =
|
|
125
|
+
const filterItems: FilterItems | undefined = formToUse.value.filterItems
|
|
126
|
+
const currentErrors = errors.value.value
|
|
107
127
|
if (!filterItems) return {}
|
|
108
|
-
if (!
|
|
109
|
-
const errorList = Object.values(
|
|
128
|
+
if (!currentErrors) return {}
|
|
129
|
+
const errorList = Object.values(currentErrors)
|
|
110
130
|
.filter(
|
|
111
131
|
(fieldErrors): fieldErrors is Record<string, StandardSchemaV1Issue[]> =>
|
|
112
132
|
Boolean(fieldErrors),
|
|
@@ -116,22 +136,40 @@ watch(
|
|
|
116
136
|
.flat()
|
|
117
137
|
.map((issue: StandardSchemaV1Issue) => issue.message),
|
|
118
138
|
)
|
|
139
|
+
|
|
119
140
|
if (errorList.some(e => e === filterItems.message)) {
|
|
120
|
-
filterItems.items
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
141
|
+
// TODO: Investigate if filterItems.items should be typed based on DeepKeys<To>.
|
|
142
|
+
filterItems.items.forEach((item: keyof From) => {
|
|
143
|
+
const m = formToUse.value.getFieldMeta(item as any)
|
|
144
|
+
if (m) {
|
|
145
|
+
formToUse.value.setFieldMeta(item as any, {
|
|
146
|
+
...m,
|
|
147
|
+
errorMap: {
|
|
148
|
+
onSubmit: [
|
|
149
|
+
{ path: [item as string], message: filterItems.message },
|
|
150
|
+
],
|
|
151
|
+
},
|
|
152
|
+
})
|
|
153
|
+
}
|
|
128
154
|
})
|
|
129
155
|
}
|
|
130
156
|
return {}
|
|
131
157
|
},
|
|
132
158
|
)
|
|
133
159
|
|
|
134
|
-
provideOmegaErrors(formSubmissionAttempts, errors, props.showErrorsOn)
|
|
160
|
+
provideOmegaErrors(formSubmissionAttempts, errors.value, props.showErrorsOn)
|
|
161
|
+
|
|
162
|
+
defineSlots<{
|
|
163
|
+
// Default slot (no props)
|
|
164
|
+
default(): void
|
|
165
|
+
// Named slot when form is created internally via schema
|
|
166
|
+
internalForm(props: {
|
|
167
|
+
form: OmegaFormReturn<To, From>
|
|
168
|
+
subscribedValues: typeof subscribedValues.value
|
|
169
|
+
}): void
|
|
170
|
+
// Named slot when form is passed via props (provides subscribedValues)
|
|
171
|
+
externalForm(props: { subscribedValues: typeof subscribedValues.value }): void
|
|
172
|
+
}>()
|
|
135
173
|
</script>
|
|
136
174
|
|
|
137
175
|
<style scoped>
|
|
@@ -10,9 +10,9 @@ export { default } from "./OmegaWrapper.vue"
|
|
|
10
10
|
|
|
11
11
|
export { OmegaForm, OmegaInput, OmegaErrors }
|
|
12
12
|
|
|
13
|
-
const OmegaFormCE = defineCustomElement(OmegaForm)
|
|
14
|
-
const OmegaInputCE = defineCustomElement(OmegaInput)
|
|
15
|
-
const OmegaErrorsCE = defineCustomElement(OmegaErrors)
|
|
13
|
+
const OmegaFormCE = defineCustomElement(OmegaForm as any)
|
|
14
|
+
const OmegaInputCE = defineCustomElement(OmegaInput as any)
|
|
15
|
+
const OmegaErrorsCE = defineCustomElement(OmegaErrors as any)
|
|
16
16
|
|
|
17
17
|
export { OmegaFormCE, OmegaInputCE, OmegaErrorsCE }
|
|
18
18
|
|
|
@@ -13,9 +13,20 @@ import {
|
|
|
13
13
|
type FormProps,
|
|
14
14
|
type MetaRecord,
|
|
15
15
|
type OmegaFormApi,
|
|
16
|
+
type OmegaInputProps,
|
|
16
17
|
} from "./OmegaFormStuff"
|
|
17
|
-
import {
|
|
18
|
+
import {
|
|
19
|
+
computed,
|
|
20
|
+
onBeforeUnmount,
|
|
21
|
+
onMounted,
|
|
22
|
+
onUnmounted,
|
|
23
|
+
defineComponent,
|
|
24
|
+
h,
|
|
25
|
+
type DefineComponent,
|
|
26
|
+
type Component,
|
|
27
|
+
} from "vue"
|
|
18
28
|
import { isObject } from "effect/Predicate"
|
|
29
|
+
import OmegaInput from "./OmegaInput.vue"
|
|
19
30
|
|
|
20
31
|
type keysRule<T> =
|
|
21
32
|
| {
|
|
@@ -39,11 +50,14 @@ export type OmegaConfig<T> = {
|
|
|
39
50
|
} & keysRule<T>
|
|
40
51
|
}
|
|
41
52
|
|
|
42
|
-
|
|
53
|
+
interface OF<To, From> extends OmegaFormApi<To, From> {
|
|
43
54
|
meta: MetaRecord<To>
|
|
44
55
|
filterItems?: FilterItems
|
|
45
56
|
clear: () => void
|
|
46
57
|
}
|
|
58
|
+
export interface OmegaFormReturn<To, From> extends OF<To, From> {
|
|
59
|
+
Input: DefineComponent<Omit<OmegaInputProps<From, To>, "form">, {}, {}>
|
|
60
|
+
}
|
|
47
61
|
|
|
48
62
|
export const useOmegaForm = <
|
|
49
63
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -265,7 +279,39 @@ export const useOmegaForm = <
|
|
|
265
279
|
window.removeEventListener("blur", saveDataInUrl)
|
|
266
280
|
})
|
|
267
281
|
|
|
268
|
-
const
|
|
282
|
+
const formWithExtras: OF<To, From> = Object.assign(form, {
|
|
283
|
+
meta,
|
|
284
|
+
filterItems,
|
|
285
|
+
clear,
|
|
286
|
+
})
|
|
287
|
+
|
|
288
|
+
return Object.assign(formWithExtras, {
|
|
289
|
+
Input: defineComponent({
|
|
290
|
+
name: "FormInput",
|
|
291
|
+
inheritAttrs: true,
|
|
292
|
+
setup(_, { attrs, slots }) {
|
|
293
|
+
const name = attrs.name as NestedKeyOf<To>
|
|
294
|
+
const label = attrs.label as string
|
|
295
|
+
if (!name || !label) {
|
|
296
|
+
throw new Error("OmegaForm.Input requires name and label props")
|
|
297
|
+
}
|
|
269
298
|
|
|
270
|
-
|
|
299
|
+
return () =>
|
|
300
|
+
h(
|
|
301
|
+
OmegaInput as Component,
|
|
302
|
+
{
|
|
303
|
+
...attrs,
|
|
304
|
+
name,
|
|
305
|
+
label,
|
|
306
|
+
form: formWithExtras,
|
|
307
|
+
},
|
|
308
|
+
slots,
|
|
309
|
+
)
|
|
310
|
+
},
|
|
311
|
+
}) as any as DefineComponent<
|
|
312
|
+
Omit<OmegaInputProps<From, To>, "form">,
|
|
313
|
+
{},
|
|
314
|
+
{}
|
|
315
|
+
>,
|
|
316
|
+
})
|
|
271
317
|
}
|
|
@@ -1,36 +1,42 @@
|
|
|
1
1
|
<template>
|
|
2
|
-
<OmegaForm :form="
|
|
3
|
-
<
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
2
|
+
<OmegaForm :form="exampleForm">
|
|
3
|
+
<OmegaInput label="aString" :form="exampleForm" name="aString" />
|
|
4
|
+
<OmegaInput label="aStringMin2" :form="exampleForm" name="aStringMin2" />
|
|
5
|
+
<OmegaInput
|
|
6
|
+
label="aStringMin2Max4"
|
|
7
|
+
:form="exampleForm"
|
|
8
|
+
name="aStringMin2Max4"
|
|
9
|
+
/>
|
|
10
|
+
<OmegaInput
|
|
11
|
+
label="aStringMin2Max3Nullable"
|
|
12
|
+
:form="exampleForm"
|
|
13
|
+
name="aStringMin2Max3Nullable"
|
|
14
|
+
/>
|
|
15
|
+
<OmegaInput label="aNumber" :form="exampleForm" name="aNumber" />
|
|
16
|
+
<OmegaInput label="aNumberMin2" :form="exampleForm" name="aNumberMin2" />
|
|
17
|
+
<OmegaInput
|
|
18
|
+
label="aNumberMin2Max"
|
|
19
|
+
:form="exampleForm"
|
|
20
|
+
name="aNumberMin2Max"
|
|
21
|
+
/>
|
|
22
|
+
<OmegaInput
|
|
23
|
+
label="aNumberMin2Max4Nullable"
|
|
24
|
+
:form="exampleForm"
|
|
25
|
+
name="aNumberMin2Max4Nullable"
|
|
26
|
+
/>
|
|
27
|
+
<OmegaInput
|
|
28
|
+
label="aSelect"
|
|
29
|
+
:form="exampleForm"
|
|
30
|
+
name="aSelect"
|
|
31
|
+
:options="[
|
|
32
|
+
{ title: 'a', value: 'a' },
|
|
33
|
+
{ title: 'b', value: 'b' },
|
|
34
|
+
{ title: 'c', value: 'c' },
|
|
35
|
+
]"
|
|
36
|
+
/>
|
|
37
|
+
<button>Submit</button>
|
|
38
|
+
<button type="reset" @click.prevent="exampleForm.clear()">Clear</button>
|
|
39
|
+
<button type="button" @click="exampleForm.reset()">Reset</button>
|
|
34
40
|
</OmegaForm>
|
|
35
41
|
</template>
|
|
36
42
|
|
|
@@ -38,7 +44,7 @@
|
|
|
38
44
|
import { S } from "effect-app"
|
|
39
45
|
import { OmegaForm, OmegaInput, useOmegaForm } from "../../components/OmegaForm"
|
|
40
46
|
|
|
41
|
-
const
|
|
47
|
+
const exampleForm = useOmegaForm(
|
|
42
48
|
S.Struct({
|
|
43
49
|
aString: S.String,
|
|
44
50
|
aStringMin2: S.String.pipe(S.minLength(2)),
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
:on-submit="onSubmit"
|
|
5
5
|
:default-values="defaultValues"
|
|
6
6
|
>
|
|
7
|
-
<template #
|
|
7
|
+
<template #internalForm="{ form }">
|
|
8
8
|
<OmegaInput label="email" name="email" :form="form" />
|
|
9
9
|
<OmegaInput label="confirm" name="confirm" :form="form" />
|
|
10
10
|
<button>submit</button>
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<OmegaForm
|
|
3
|
+
:schema="
|
|
4
|
+
S.Struct({
|
|
5
|
+
a: S.NullOr(S.String),
|
|
6
|
+
b: S.UndefinedOr(S.Number),
|
|
7
|
+
c: S.NullishOr(S.Number),
|
|
8
|
+
d: S.String,
|
|
9
|
+
e: S.Number,
|
|
10
|
+
f: S.Number,
|
|
11
|
+
struct: S.Struct({
|
|
12
|
+
a: S.NullOr(S.String),
|
|
13
|
+
b: S.UndefinedOr(S.Number),
|
|
14
|
+
c: S.NullishOr(S.Number),
|
|
15
|
+
d: S.String,
|
|
16
|
+
e: S.Number,
|
|
17
|
+
f: S.Number,
|
|
18
|
+
}),
|
|
19
|
+
})
|
|
20
|
+
"
|
|
21
|
+
>
|
|
22
|
+
<template #internalForm="{ form }">
|
|
23
|
+
<ul>
|
|
24
|
+
<li v-for="key in Object.keys(form.meta)" :key="key">
|
|
25
|
+
{{ key }}: {{ (form.meta as any)[key] }}
|
|
26
|
+
</li>
|
|
27
|
+
</ul>
|
|
28
|
+
<OmegaInput label="a" :form="form" name="a" />
|
|
29
|
+
<OmegaInput label="b" :form="form" name="b" />
|
|
30
|
+
<OmegaInput label="c" :form="form" name="c" />
|
|
31
|
+
<OmegaInput label="d" :form="form" name="d" />
|
|
32
|
+
<OmegaInput label="e" :form="form" name="e" />
|
|
33
|
+
<OmegaInput label="f" :form="form" name="f" />
|
|
34
|
+
<OmegaInput label="struct.a" :form="form" name="struct.a" />
|
|
35
|
+
<OmegaInput label="struct.b" :form="form" name="struct.b" />
|
|
36
|
+
<OmegaInput label="struct.c" :form="form" name="struct.c" />
|
|
37
|
+
<OmegaInput label="struct.d" :form="form" name="struct.d" />
|
|
38
|
+
<OmegaInput label="struct.e" :form="form" name="struct.e" />
|
|
39
|
+
<OmegaInput label="struct.f" :form="form" name="struct.f" />
|
|
40
|
+
</template>
|
|
41
|
+
</OmegaForm>
|
|
42
|
+
</template>
|
|
43
|
+
|
|
44
|
+
<script setup lang="ts">
|
|
45
|
+
import { OmegaForm, OmegaInput } from "../../components/OmegaForm"
|
|
46
|
+
import { S } from "effect-app"
|
|
47
|
+
</script>
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<div class="container">
|
|
3
|
+
<h1>One hundred ways to write a form</h1>
|
|
4
|
+
<p>
|
|
5
|
+
OmegaForm is a powerful and flexible form library that wraps
|
|
6
|
+
<a href="https://tanstack.com/form/latest" target="_blank">TanStack Form</a>
|
|
7
|
+
and uses
|
|
8
|
+
<a href="https://effect.website/docs/schema/introduction/" target="_blank">
|
|
9
|
+
Effect Schema
|
|
10
|
+
</a>
|
|
11
|
+
as the schema definition. All of this allows you to create forms in a
|
|
12
|
+
declarative way.
|
|
13
|
+
We also use
|
|
14
|
+
<a href="https://vuetifyjs.com/" target="_blank">Vuetify</a> as peer dependency
|
|
15
|
+
for the UI components, but you can use any other UI library you want or custom inputs.
|
|
16
|
+
Here are some examples of how to write forms using
|
|
17
|
+
different approaches
|
|
18
|
+
</p>
|
|
19
|
+
<p>for our example, we will use the following dependencies</p>
|
|
20
|
+
<pre v-highlightjs><code class="javascript">{{ `import { S } from "effect-app"
|
|
21
|
+
import { OmegaForm, OmegaInput, useOmegaForm } from "@effect-app/vue-components"` }}</code></pre>
|
|
22
|
+
|
|
23
|
+
<h2>Simplest way</h2>
|
|
24
|
+
<p>Now, let's write a form using the following schema:</p>
|
|
25
|
+
<pre v-highlightjs><code class="typescript">{{ `const schema = S.Struct({
|
|
26
|
+
name: S.String,
|
|
27
|
+
age: S.Number,
|
|
28
|
+
})` }}</code></pre>
|
|
29
|
+
|
|
30
|
+
<p>Now, let's write in the template the form using the following schema:</p>
|
|
31
|
+
<pre
|
|
32
|
+
v-highlightjs
|
|
33
|
+
><code class="vue">{{ `<OmegaForm :schema="schema" :on-submit="console.log">
|
|
34
|
+
<template #internalForm="{ form }">
|
|
35
|
+
<OmegaInput label="name" :form="form" name="name" />
|
|
36
|
+
<OmegaInput label="age" :form="form" name="age" />
|
|
37
|
+
</template>
|
|
38
|
+
</OmegaForm>` }}</code></pre>
|
|
39
|
+
|
|
40
|
+
<OmegaForm :schema="schema" :on-submit="console.log">
|
|
41
|
+
<template #internalForm="{ form }">
|
|
42
|
+
<OmegaInput label="name" :form="form" name="name" />
|
|
43
|
+
<OmegaInput label="age" :form="form" name="age" />
|
|
44
|
+
</template>
|
|
45
|
+
</OmegaForm>
|
|
46
|
+
|
|
47
|
+
<p>
|
|
48
|
+
<code>OmegaInput</code>
|
|
49
|
+
is a component that will render a form input based on the schema. It's
|
|
50
|
+
alread embedded inside the
|
|
51
|
+
<code>OmegaForm</code>
|
|
52
|
+
component, so you don't need to import it separately or pass form as a prop:
|
|
53
|
+
</p>
|
|
54
|
+
<pre v-highlightjs><code class="vue">{{ `<OmegaForm :schema="schema">
|
|
55
|
+
<template #internalForm="{ form }">
|
|
56
|
+
<component :is="form.Input" label="name" name="name" />
|
|
57
|
+
<component :is="form.Input" label="age" name="age" />
|
|
58
|
+
</template>
|
|
59
|
+
</OmegaForm>` }}</code></pre>
|
|
60
|
+
<p>you can also register to the values via the `subscribe` prop</p>
|
|
61
|
+
<pre
|
|
62
|
+
v-highlightjs
|
|
63
|
+
><code class="vue">{{ `<OmegaForm :schema="schema" :subscribe="['values']">
|
|
64
|
+
<template #internalForm="{ form, subscribedValues: { values } }">
|
|
65
|
+
<component :is="form.Input" label="name" name="name" />
|
|
66
|
+
<component :is="form.Input" label="age" name="age" />
|
|
67
|
+
<pre>\{\{ values \}\}</pre>
|
|
68
|
+
</template>
|
|
69
|
+
</OmegaForm>` }}</code></pre>
|
|
70
|
+
|
|
71
|
+
<OmegaForm :schema="schema" :subscribe="['values']">
|
|
72
|
+
<template #internalForm="{ form, subscribedValues: { values } }">
|
|
73
|
+
<component :is="form.Input" label="name" name="name" />
|
|
74
|
+
<component :is="form.Input" label="age" name="age" />
|
|
75
|
+
<pre>{{ values }}</pre>
|
|
76
|
+
</template>
|
|
77
|
+
</OmegaForm>
|
|
78
|
+
|
|
79
|
+
<h2>Using the useOmegaForm hook</h2>
|
|
80
|
+
<p>
|
|
81
|
+
The useOmegaForm hook is a hook that returns the form instance and the
|
|
82
|
+
values. It's a good way to create a form in a functional way.
|
|
83
|
+
</p>
|
|
84
|
+
<pre
|
|
85
|
+
v-highlightjs
|
|
86
|
+
><code class="typescript">{{ `const form = useOmegaForm(schema)` }}</code></pre>
|
|
87
|
+
<p>
|
|
88
|
+
Now, you can use the form instance to create the form in the template.
|
|
89
|
+
</p>
|
|
90
|
+
<pre v-highlightjs><code class="vue">{{ `<OmegaForm :form="form">
|
|
91
|
+
<form.Input" label="name" name="name" />
|
|
92
|
+
<form.Input" label="age" name="age" />
|
|
93
|
+
</OmegaForm>` }}</code></pre>
|
|
94
|
+
|
|
95
|
+
<p>you can still register to the values via the `subscribe` prop</p>
|
|
96
|
+
<pre
|
|
97
|
+
v-highlightjs
|
|
98
|
+
><code class="vue">{{ `<OmegaForm :form="form" :subscribe="['values']">
|
|
99
|
+
<template #externalForm="{ subscribedValues: { values } }">
|
|
100
|
+
<form.Input" label="name" name="name" />
|
|
101
|
+
<form.Input" label="age" name="age" />
|
|
102
|
+
<pre>\{\{ values \}\}</pre>
|
|
103
|
+
</template>
|
|
104
|
+
</OmegaForm>` }}</code></pre>
|
|
105
|
+
<p>
|
|
106
|
+
<strong>Note:</strong> the template name is <code>externalForm</code>
|
|
107
|
+
because the form is not inside the component, it's outside. And you don't
|
|
108
|
+
have access to the form instance inside the template variables anymore.
|
|
109
|
+
</p>
|
|
110
|
+
|
|
111
|
+
<h3>Using custom inputs</h3>
|
|
112
|
+
<p>
|
|
113
|
+
You can use custom inputs by passing an OmegaInput a child component.
|
|
114
|
+
</p>
|
|
115
|
+
<pre v-highlightjs><code class="vue">{{ `<OmegaForm :form="form">
|
|
116
|
+
<form.Input label="name" name="name">
|
|
117
|
+
<template #default="{ field, label }">
|
|
118
|
+
<label :for="name">\{\{ label \}\}</label>
|
|
119
|
+
<input
|
|
120
|
+
:id="name"
|
|
121
|
+
v-model="field.state.value"
|
|
122
|
+
:name="name"
|
|
123
|
+
style="border: 1px solid red"
|
|
124
|
+
@change="(e) => field.handleChange(e.target.value)"
|
|
125
|
+
/>
|
|
126
|
+
</template>
|
|
127
|
+
</form.Input>
|
|
128
|
+
</OmegaForm>` }}</code></pre>
|
|
129
|
+
<OmegaForm :form="form">
|
|
130
|
+
<form.Input label="name" name="name">
|
|
131
|
+
<template #default="{ field, label, name }">
|
|
132
|
+
<label :for="name">{{ label }}</label>
|
|
133
|
+
<input
|
|
134
|
+
:id="name"
|
|
135
|
+
v-model="field.state.value"
|
|
136
|
+
:name="name"
|
|
137
|
+
style="border: 1px solid red"
|
|
138
|
+
@change="(e: any) => field.handleChange(e.target.value)"
|
|
139
|
+
/>
|
|
140
|
+
</template>
|
|
141
|
+
</form.Input>
|
|
142
|
+
</OmegaForm>
|
|
143
|
+
<h3>Known issues</h3>
|
|
144
|
+
<p>
|
|
145
|
+
You can't write something like this:
|
|
146
|
+
</p>
|
|
147
|
+
<pre v-highlightjs><code class="vue">{{ `<OmegaForm :schema="schema">
|
|
148
|
+
<template #internalForm="{ form }">
|
|
149
|
+
<form.Input label="name" name="name" />
|
|
150
|
+
<form.Input label="age" name="age" />
|
|
151
|
+
</template>
|
|
152
|
+
</OmegaForm>` }}</code></pre>
|
|
153
|
+
<p>
|
|
154
|
+
because When Vue's template compiler encounters <code>form.Input</code>, it try to resolve or analyze the form object and its Input property earlier in the rendering pipeline. Since form.Input is provided by the parent OmegaForm through the slot, this direct usage inside the slot might create a tighter, more immediate dependency loop that the compiler/renderer detects or falls into, leading to the stack overflow.
|
|
155
|
+
</p>
|
|
156
|
+
</div>
|
|
157
|
+
</template>
|
|
158
|
+
|
|
159
|
+
<script setup lang="ts">
|
|
160
|
+
import { S } from "effect-app"
|
|
161
|
+
import { OmegaForm, OmegaInput, useOmegaForm } from "../../components/OmegaForm"
|
|
162
|
+
|
|
163
|
+
const schema = S.Struct({
|
|
164
|
+
name: S.String,
|
|
165
|
+
age: S.Number,
|
|
166
|
+
})
|
|
167
|
+
const form = useOmegaForm(schema)
|
|
168
|
+
</script>
|
|
169
|
+
|
|
170
|
+
<style scoped>
|
|
171
|
+
p,
|
|
172
|
+
pre {
|
|
173
|
+
margin-bottom: 1rem;
|
|
174
|
+
}
|
|
175
|
+
form {
|
|
176
|
+
margin-bottom: 2rem;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
.container {
|
|
180
|
+
max-width: 1200px;
|
|
181
|
+
margin: 0 auto;
|
|
182
|
+
padding: 0 1rem;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
h1, h2, h3 {
|
|
186
|
+
text-wrap: balance;
|
|
187
|
+
}
|
|
188
|
+
</style>
|
|
@@ -4,17 +4,17 @@
|
|
|
4
4
|
:subscribe="['errors', 'values']"
|
|
5
5
|
show-errors-on="onChange"
|
|
6
6
|
>
|
|
7
|
-
<template #
|
|
7
|
+
<template #externalForm="{ subscribedValues: { errors, values: vvv } }">
|
|
8
8
|
<div>Errors: {{ errors }}</div>
|
|
9
|
-
<div>Values: {{
|
|
10
|
-
<OmegaInput label="first" :form="
|
|
9
|
+
<div>Values: {{ vvv }}</div>
|
|
10
|
+
<OmegaInput label="first" :form="addForm" name="first" />
|
|
11
11
|
<div>+</div>
|
|
12
|
-
<OmegaInput label="second" :form="
|
|
12
|
+
<OmegaInput label="second" :form="addForm" name="second" />
|
|
13
13
|
<br />
|
|
14
14
|
<hr />
|
|
15
15
|
<br />
|
|
16
|
-
<OmegaInput label="third.fourth" :form="
|
|
17
|
-
<OmegaInput label="third.fifth" :form="
|
|
16
|
+
<OmegaInput label="third.fourth" :form="addForm" name="third.fourth" />
|
|
17
|
+
<OmegaInput label="third.fifth" :form="addForm" name="third.fifth" />
|
|
18
18
|
</template>
|
|
19
19
|
</OmegaForm>
|
|
20
20
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<template>
|
|
2
2
|
<OmegaForm :schema="schema" :on-submit="onSubmit" :subscribe="['values']">
|
|
3
|
-
<template #
|
|
3
|
+
<template #internalForm="{ form, subscribedValues: { values } }">
|
|
4
4
|
<div>values: {{ values }}</div>
|
|
5
5
|
<OmegaInput label="asder2" name="asder2" :form="form">
|
|
6
6
|
<template #default="inputProps">
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<template>
|
|
2
2
|
<OmegaForm :schema="schema" :subscribe="['values']">
|
|
3
|
-
<template #
|
|
3
|
+
<template #internalForm="{ form, subscribedValues: { values } }">
|
|
4
4
|
<OmegaInput label="aString" :form="form" name="aString" />
|
|
5
5
|
<pre>{{ values }}</pre>
|
|
6
6
|
</template>
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
<template>
|
|
2
2
|
<OmegaForm :form="addForm">
|
|
3
|
-
<
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
<OmegaInput label="second" :form="form" name="second" />
|
|
7
|
-
</template>
|
|
3
|
+
<OmegaInput label="first" :form="addForm" name="first" />
|
|
4
|
+
<div>+</div>
|
|
5
|
+
<OmegaInput label="second" :form="addForm" name="second" />
|
|
8
6
|
</OmegaForm>
|
|
9
7
|
|
|
10
8
|
<!-- Technically you can do this only with a subscribe but only inside OmegaForm Context -->
|