Skip to content
This repository was archived by the owner on Jan 21, 2021. It is now read-only.

Commit c6eeae3

Browse files
Add a hook for creating a new form (#12)
* Add a hook for creating a new form * wip * Remove hard dependency on @inertiajs/inertia-vue3 * Add optional dependency on @inertiajs/inertia-vue3 * cs * Make sure page data is always updated * wip * wip * cs * Update InertiaForm.js Co-authored-by: Taylor Otwell <[email protected]>
1 parent 78ac120 commit c6eeae3

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,8 @@
3535
"dependencies": {
3636
"@inertiajs/inertia": "^0.4.0",
3737
"prettier": "^1.10.2"
38+
},
39+
"optionalDependencies": {
40+
"@inertiajs/inertia-vue3": "^0.2.0"
3841
}
3942
}

src/InertiaForm.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Inertia } from '@inertiajs/inertia'
2+
import { shallowReactive } from 'vue'
13
import {
24
guardAgainstReservedFieldName,
35
isArray,
@@ -259,6 +261,20 @@ class InertiaForm {
259261
}
260262
}
261263

264+
export function useForm(data, options) {
265+
const adapter = require('@inertiajs/inertia-vue3')
266+
267+
if (! adapter || ! shallowReactive) throw Error('The useForm hook may only be used with Vue 3 and @inertiajs/inertia-vue3.')
268+
269+
return shallowReactive(
270+
InertiaForm.create()
271+
.withData(data)
272+
.withOptions(options)
273+
.withInertia(Inertia)
274+
.withPage(() => adapter.usePage().props.value)
275+
)
276+
}
277+
262278
export default {
263279
install(app) {
264280
if (app.version.split('.')[0] == 3) {

0 commit comments

Comments
 (0)