Skip to content

Commit a534a1f

Browse files
authored
docs: update docs for the plugin
1 parent 323a5cd commit a534a1f

File tree

1 file changed

+16
-92
lines changed

1 file changed

+16
-92
lines changed

plugins/inertia.md

Lines changed: 16 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,11 @@ npm install @formkit/inertia
2121

2222
## Usage
2323

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.
2525

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.
2727

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`:
3529

3630
```html
3731
<script setup lang="ts">
@@ -98,8 +92,6 @@ To cancel a form submission, use the `cancel()` method.
9892
<FormKit type="button" @click="form.cancel()" label="Cancel" />
9993
```
10094

101-
### States
102-
10395
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):
10496

10597
```html
@@ -115,104 +107,36 @@ The `useForm()` composable also returns reactive states. The Inertia ones are: `
115107
</template>
116108
```
117109

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
121111

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:
123113

124114
```html
125115
<script setup lang="ts">
126116
import { useForm } from '@formkit/inertia'
127117
128118
const form = useForm()
129-
form.on('before', () => {
130-
return confirm('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-
<script setup lang="ts">
139-
import { useForm } from '@formkit/inertia'
140-
141-
const form = useForm()
142-
form.combine((on) => {
143-
on('before', () => {
119+
form.addon((on) => {
120+
on('before', (visit, node) => {
144121
return confirm('Are you sure you want to delete this user?')
145122
})
146123
147-
on('success', () => {
124+
on('success', (page, node) => {
148125
toast('User deleted.')
149126
})
150127
})
151128
</script>
152129
```
153130

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`:
157-
158-
```ts
159-
import { createEventCallbackManager } from '@formkit/ineria'
160-
161-
const event = createEventCallbackManager()
162-
event.on('before', (visit) => {
163-
console.log(visit)
164-
})
165-
```
166-
167-
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:
168132

169-
```ts
170-
import { createEventCallbackManager } from '@formkit/ineria'
171-
172-
const event = createEventCallbackManager<[node: FormKitNode]>()
173-
event.on('before', (visit, node) => {
174-
console.log(visit, node)
175-
})
176-
```
177-
178-
The `combine()` function allows you to define multiple events in a single block:
179-
180-
```ts
181-
// addon.ts
182-
import { CombineFunction } from '@formkit/inertia'
183-
184-
return (on) => {
185-
on('before', (visit, node) => {
186-
console.log(visit, node)
187-
})
133+
```html
134+
<script setup lang="ts">
135+
import { useForm } from '@formkit/inertia'
188136
189-
on('success', (page, node) => {
190-
console.log(page, node)
137+
const form = useForm()
138+
form.on('before', (visit, node) => {
139+
return confirm('Are you sure you want to delete this user?')
191140
})
192-
} as CombineFunction<[node: FormKitNode]>
193-
194-
// app.ts
195-
import { createEventCallbackManager } from '@formkit/ineria'
196-
import addon from './addon'
197-
198-
const event = createEventCallbackManager<[node: FormKitNode]>()
199-
event.combine(addon)
200-
```
201-
202-
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:
203-
204-
```ts
205-
import { createEventCallbackManager } from '@formkit/ineria'
206-
207-
const event = createEventCallbackManager<[node: FormKitNode]>()
208-
209-
event.on('before', (visit, node) => {
210-
console.log(visit, node)
211-
})
212-
event.on('before', (visit, node) => {
213-
return false
214-
})
215-
216-
const result = event.execute('before', visit, node) // runs console.log
217-
console.log(result) // returns false
141+
</script>
218142
```

0 commit comments

Comments
 (0)