Skip to content

Commit f1cc5b4

Browse files
Update docs to meet the latest updates
Signed-off-by: Kiran Parajuli <[email protected]>
1 parent 1e72d4d commit f1cc5b4

File tree

7 files changed

+53
-49
lines changed

7 files changed

+53
-49
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"highlight.js": "^11.11.1",
1919
"pinia": "^2.3.0",
2020
"vue": "^3.5.13",
21-
"vue-formik": "^0.1.34",
21+
"vue-formik": "^0.1.38",
2222
"vue-router": "^4.5.0",
2323
"yup": "^1.6.1"
2424
},

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/assets/styles/components/_button.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
button {
2-
@apply px-4 py-2;
2+
@apply px-4 py-2 select-none;
33
}
44

55
button,

src/components/home/ExpoForm.vue

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<div>Contacts</div>
1414
<ol class="flex flex-col gap-2 list-decimal list-inside">
1515
<li v-for="(_, index) in formik.values.contacts" :key="index" class="flex gap-4">
16-
<span> {{ index + 1 }}. </span>
16+
<span> {{ index + 1 }}.</span>
1717
<FormInput
1818
:name="`contacts[${index}].code`"
1919
type="tel"
@@ -88,9 +88,19 @@
8888

8989
<br />
9090

91-
<button type="submit" class="primary-btn w-fit" :disabled="!formik.isValid.value">
92-
Submit
93-
</button>
91+
<div class="flex items-center gap-2">
92+
<button type="submit" class="primary-btn w-fit" :disabled="!formik.isValid.value">
93+
Submit
94+
</button>
95+
<button
96+
type="reset"
97+
class="secondary-btn ml-4 w-fit"
98+
:disabled="!formik.isDirty.value"
99+
@click="() => formik.reset({ keepTouched: false })"
100+
>
101+
Reset
102+
</button>
103+
</div>
94104
</FormikForm>
95105
<div class="overflow-auto border border-gray-500 p-4 rounded-md h-full text-sm">
96106
<pre><code>{{JSON.stringify({
@@ -130,22 +140,20 @@ const sexOptions = [
130140
];
131141
132142
const opts = computed(() => ({
143+
initialValues: InitialValues,
133144
validationSchema: props.value === DemoTabValues.CUSTOM ? props.validationSchema : undefined,
134145
yupSchema: props.value === DemoTabValues.YUP ? props.validationSchema : undefined,
135146
validateOnMount: props.validateOnMount,
147+
onSubmit: (values: any, helpers: any) => {
148+
if (confirm(JSON.stringify(values, null, 2))) {
149+
console.log("Submitted", values);
150+
helpers.reset();
151+
}
152+
},
136153
}));
137154
138-
const formik = computed(() =>
139-
useFormik({
140-
initialValues: InitialValues,
141-
...opts.value,
142-
onSubmit: (values: any) => {
143-
console.log("Submitted:", values);
144-
},
145-
}),
146-
);
147-
148-
const fieldArray = computed(() => useFieldArray(formik.value));
155+
const formik = useFormik(opts.value);
156+
const fieldArray = useFieldArray(formik);
149157
150-
provide("formik", formik.value);
158+
provide("formik", formik);
151159
</script>

src/constants/demo.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ export const ValidationSchema = {
4040

4141
const errs = [];
4242
for (let i = 0; i < value.length; i++) {
43-
const err = {
44-
code: "",
45-
number: "",
46-
};
43+
const err: {
44+
code?: string;
45+
number?: string;
46+
} = {};
4747

4848
if (!value[i].code) {
4949
err.code = "Code is required";
@@ -58,7 +58,9 @@ export const ValidationSchema = {
5858
err.number = "Invalid phone number. Must be 10 digits";
5959
}
6060

61-
errs[i] = err;
61+
if (err.code || err.number) {
62+
errs[i] = err;
63+
}
6264
}
6365

6466
return errs.length ? errs : undefined;
@@ -67,7 +69,6 @@ export const ValidationSchema = {
6769
if (value.length === 0) {
6870
return "Address is required";
6971
}
70-
console.log(value);
7172

7273
const errs = [];
7374
for (let i = 0; i < value.length; i++) {

src/views/DemoView.vue

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,32 @@
33
<h2 class="!text-xl !pb-1">Simple form validation with Vue Formik</h2>
44
<hr class="mb-6" />
55

6-
<div class="mb-4 border-b">
6+
<div class="mb-4 border-b flex gap-4 items-center">
77
<button
88
v-for="tabItem in DemoTabs"
99
:key="tabItem.value"
10-
:class="{ 'bg-gray-800 text-white': tab === tabItem.value }"
11-
class="py-2 px-4 mr-2 last:mr-0"
10+
:class="{
11+
'py-2 px-4 rounded-t-md rounded-b-none': true,
12+
'bg-gray-800 text-white': tab === tabItem.value,
13+
}"
1214
@click="() => (tab = tabItem.value)"
1315
>
1416
{{ tabItem.name }}
1517
</button>
1618

1719
<label for="validateOnMount">
18-
<input id="validateOnMount" v-model="validateOnMount" type="checkbox" class="ml-4" />
19-
<span class="ml-2">Validate on mount</span>
20+
<input id="validateOnMount" :checked="validateOnMount" type="checkbox" disabled />
21+
<span class="text-sm ml-2">Validate on mount</span>
2022
</label>
2123
</div>
2224

2325
<div class="pg-view__content">
2426
<template v-for="tabItem in DemoTabs" :key="tabItem.value">
2527
<ExpoForm
26-
v-if="tab === tabItem.value"
27-
:validation-schema="tabItem.schema"
28-
:value="tab"
28+
v-if="activeTab?.value === tabItem.value"
2929
:validate-on-mount="validateOnMount"
30+
:validation-schema="tabItem.schema"
31+
:value="tabItem.value"
3032
/>
3133
</template>
3234

@@ -49,7 +51,7 @@
4951
</template>
5052

5153
<script setup lang="ts">
52-
import { onBeforeMount, ref } from "vue";
54+
import { computed, onBeforeMount, ref } from "vue";
5355
import ValidationSchemaPreview from "@/components/home/ValidationSchemaPreview.vue";
5456
import ExpoForm from "@/components/home/ExpoForm.vue";
5557
import { DemoTabs } from "@/constants/demo.ts";
@@ -60,5 +62,6 @@ onBeforeMount(() => {
6062
});
6163
6264
const tab = ref(1);
63-
const validateOnMount = ref(false);
65+
const activeTab = computed(() => DemoTabs.find((t) => t.value === tab.value));
66+
const validateOnMount = ref(true);
6467
</script>

src/views/libDocs/composables/UseFieldArrayView.vue

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,8 @@ const { push, pop } = useFieldArray(formik);`;
3939
<blockquote>
4040
<p class="font-semibold">Note: <code>formik</code> parameter</p>
4141
<p>
42-
The composable reads formik from parameters or using injections. If you are using the
43-
composable inside a component that is wrapped with the
44-
<code>withFormik</code> HOC, you don't need to pass the formik object as an argument.
45-
</p>
46-
47-
<p>
48-
If you are using the composable outside of a component, you need to pass the formik object
49-
as an argument to the composable. The composable will automatically detect the formik
50-
object if it is available in the component's context. If both the formik object parameter
51-
and the argument in injection are provided, the fn parameter will take precedence.
42+
The <code>formik</code> parameter is the object returned by the
43+
<code>useFormik</code> composable.
5244
</p>
5345
</blockquote>
5446
</section>

0 commit comments

Comments
 (0)