You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: plugins/inertia.md
+16-92Lines changed: 16 additions & 92 deletions
Original file line number
Diff line number
Diff line change
@@ -21,17 +21,11 @@ npm install @formkit/inertia
21
21
22
22
## Usage
23
23
24
-
To use the Inertia plugin we need to import the `useForm` function from `@formkit/inertia`, call the `useForm` function to receive the `form`, it comes with Inertia method calls, reactive states, the plugin event system, and the FormKit plugin.
24
+
To use the Inertia plugin we need to import the `useForm` function from `@formkit/inertia`, call the `useForm` function to receive the `form`, it comes with Inertia's method calls, reactive states, the addons for extensions, and the FormKit plugin.
25
25
26
-
The `useForm` function can take one optional argument:
26
+
The `useForm` function takes one optional argument for the initial fields that will be passed to your form via plugin, it will also return methods like `submit`, `get`, `post`, `put`, `patch` and `delete`. All of these methods will return a suitable function for use as FormKit’s `@submit` handler.
27
27
28
-
-`initialFields`: The initial fields to be passed to your form.
29
-
30
-
### Method Calls
31
-
32
-
The `useForm()` composable returns the methods `get`, `post`, `put`, `patch` and `delete`. All of these methods will return a suitable function for use as FormKit’s `@submit` handler.
33
-
34
-
The easiest way to use it is by creating a new `const` with the resulting method of your choice:
28
+
The easiest way to use it is by creating a new `const` with the resulting method of your choice, and adding the `form.plugin` to the FormKit form `:plugins`:
35
29
36
30
```html
37
31
<scriptsetuplang="ts">
@@ -98,8 +92,6 @@ To cancel a form submission, use the `cancel()` method.
The `useForm()` composable also returns reactive states. The Inertia ones are: `processing`, `progress`, `recentlySuccessful` and `wasSuccessful`, the FormKit based ones are `valid`, `errors`, `dirty` and `node`. For example, you could use the `processing` state to disable the form submit button while Inertia is processing the form (assuming that you’re using your own submit button):
104
96
105
97
```html
@@ -115,104 +107,36 @@ The `useForm()` composable also returns reactive states. The Inertia ones are: `
115
107
</template>
116
108
```
117
109
118
-
### Event Functions
119
-
120
-
If you need to new features, or want to run some code on Inertia event callbacks but want to keep the functionality of this package intact, you can use the provided event functions `on()` and `combine()`. These add functions to the event callbacks without having to deal with option merging.
110
+
## Addons
121
111
122
-
The `on()`function accepts any of the events from Inertia’s event callbacks (without the `on` prefix), specifically: `before`, `start`, `progress`, `success`, `error`, `cancel`, and `finish`. The arguments passed to your callback are the Inertia event’s callback arguments and then FormKit's node:
112
+
The main feature for extending functionality is by passing addons to `addon()`, this way you can target multiple events that will be triggered when those are called by Inertia's event callback system, `addon()` accepts a function or an array of functions with `on()`, it accepts any of the events from Inertia’s event callbacks (without the `on` prefix), specifically: `before`, `start`, `progress`, `success`, `error`, `cancel`,`cancelToken` and `finish`. The arguments passed to your callback are the Inertia event’s callback arguments and then FormKit's node:
123
113
124
114
```html
125
115
<scriptsetuplang="ts">
126
116
import { useForm } from'@formkit/inertia'
127
117
128
118
constform=useForm()
129
-
form.on('before', () => {
130
-
returnconfirm('Are you sure you want to delete this user?')
131
-
})
132
-
</script>
133
-
```
134
-
135
-
The `combine()` function is just a easier way to add multiple events in a single place:
136
-
137
-
```html
138
-
<scriptsetuplang="ts">
139
-
import { useForm } from'@formkit/inertia'
140
-
141
-
constform=useForm()
142
-
form.combine((on) => {
143
-
on('before', () => {
119
+
form.addon((on) => {
120
+
on('before', (visit, node) => {
144
121
returnconfirm('Are you sure you want to delete this user?')
145
122
})
146
123
147
-
on('success', () => {
124
+
on('success', (page, node) => {
148
125
toast('User deleted.')
149
126
})
150
127
})
151
128
</script>
152
129
```
153
130
154
-
## Event Callback Manager
155
-
156
-
The `createEventCallbackManager()` composable returns 3 functions `on()`, `combine()` and `execute()`. The `on` function accepts these events `before`, `start`, `progress`, `success`, `error`, `cancel`, `finish`:
As you can see it only gets `visit` as a parameter because `createEventCallbackManager()` was not specified that its events will receive more than that, but you can extend by passing an array of types of parameters to it:
131
+
If you need a single event callback `useForm()` also returns `on()` directly:
The `execute()` function executes the given event. It expects the event, parameters, and any other parameter passed as a type to `createEventCallbackManager` and returns the result of the event callback from Inertia:
0 commit comments