Skip to content

Commit 519d199

Browse files
committed
feat(components): FormKitDataEdit / FormKitDataView - Add optional v-model for form data
1 parent 0afe6fc commit 519d199

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

dev/pages/data/Edit.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ const schema = reactive(
9090
],
9191
)
9292
93-
const data = ref({ name: 'Tom', date: new Date(), number: 2222.33, text1: 'Ein Text', text2: 'Lorem Ipsum' })
93+
const data = ref({ date: new Date(), text: 'Lorem Ipsum' })
9494
9595
const { showSuccessMessage } = useMessages()
9696
async function submitHandler() {
@@ -111,7 +111,7 @@ async function submitHandler() {
111111
<Checkbox v-if="horizontal" v-model="indentCheckboxes" binary />
112112
</div>
113113
<FormKitDataEdit
114-
:data="data"
114+
v-model="data"
115115
:schema="schema"
116116
:submit-label="t('save')"
117117
:form-class="(horizontal ? ' form-horizontal' : '') + (indentCheckboxes ? ' form-horizontal-checkbox-indent ' : '')"

dev/pages/data/View.vue

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,21 @@ const outputSchema = ref(
4141
],
4242
)
4343
const data = ref({ name: 'Tom', date: new Date(), number: 2222.33, text1: 'Ein Text', text2: 'Lorem Ipsum' })
44+
45+
function updateData() {
46+
if (data.value.name === 'Tom')
47+
data.value = { name: 'Tim', date: new Date(), number: 42, text1: 'Some Text', text2: 'Lorem Ipsum' }
48+
else
49+
data.value = { name: 'Tom', date: new Date(), number: 2222.33, text1: 'Hello', text2: 'World' }
50+
}
4451
</script>
4552

4653
<template>
4754
<PrimeData header="FormKitDataView Demo">
55+
<Button label="changeData (using v-model)" size="small" class="mb-4" @click="updateData" />
4856
<div class="flex gap-2 mb-4">
4957
Horizontal <ToggleSwitch v-model="horizontal" />
5058
</div>
51-
<FormKitDataView :data="data" :schema="outputSchema" :debug-mode="true" :form-class="horizontal ? 'form-horizontal' : ''" />
59+
<FormKitDataView v-model="data" :schema="outputSchema" :debug-mode="true" :form-class="horizontal ? 'form-horizontal' : ''" />
5260
</PrimeData>
5361
</template>

src/components/FormKitDataEdit.vue

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { PropType } from 'vue'
44
import { reset } from '@formkit/core'
55
import { FormKit, FormKitMessages, FormKitSchema } from '@formkit/vue'
66
import { ref } from 'vue'
7+
78
import FormKitDebug from './FormKitDebug.vue'
89
910
const props = defineProps({
@@ -72,14 +73,18 @@ const props = defineProps({
7273
default: false,
7374
},
7475
})
75-
7676
const emit = defineEmits(['dataSaved'])
7777
78+
const formData = defineModel()
79+
80+
if (props.data) {
81+
formData.value = props.data
82+
}
83+
7884
const formSchema = ref(props.schema)
79-
const formData = ref(props.data)
8085
8186
function handleSave() {
82-
emit('dataSaved', props.data)
87+
emit('dataSaved', formSchema.value)
8388
}
8489
8590
function handleReset() {

src/components/FormKitDataView.vue

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import type { FormKitSchemaDefinition } from '@formkit/core'
33
import type { PropType } from 'vue'
44
import { FormKit, FormKitSchema } from '@formkit/vue'
5-
import { ref } from 'vue'
65
import FormKitDebug from './FormKitDebug.vue'
76
87
const props = defineProps({
@@ -28,8 +27,11 @@ const props = defineProps({
2827
},
2928
})
3029
31-
const formSchema = ref(props.schema)
32-
const formData = ref(props.data)
30+
const formData = defineModel()
31+
32+
if (props.data) {
33+
formData.value = props.data
34+
}
3335
</script>
3436

3537
<template>
@@ -40,11 +42,11 @@ const formData = ref(props.data)
4042
:form-class="formClass"
4143
:actions="false"
4244
>
43-
<FormKitSchema v-if="formSchema" :schema="formSchema" :data="formData" />
45+
<FormKitSchema v-if="schema" :schema="schema" :data="formData" />
4446
<slot />
4547
</FormKit>
4648
<FormKitDebug v-if="debugData" :data="formData" header="Data" />
47-
<FormKitDebug v-if="debugSchema" :data="formSchema as object" header="Schema" />
49+
<FormKitDebug v-if="debugSchema" :data="schema as object" header="Schema" />
4850
</div>
4951
</template>
5052

0 commit comments

Comments
 (0)