@formkit/inertia

中文文档
0.1.5发布于 2023/11/11MITTypeScriptESM + CJS零依赖近 30 天下载 4874

FormKit + Inertia = ❤️

原始使用文档
文档目录6 节
  1. Table of Contents
  2. Installation
  3. Usage
  4. Addons
  5. Roadmap
  6. Types

Inertia Plugin

GitHub Build Status Npm Version Npm Version

The @formkit/inertia plugin aims to seamlessly integrate Inertia.js with FormKit forms, leveraging a robust event system that harnesses Inertia.js event callbacks and FormKit plugins for a smooth and powerful web development experience.

Table of Contents

  1. Installation
  2. Usage
  3. Addons
  4. Roadmap
  5. Types

Installation

To use the Inertia plugin we need to have a Laravel project already with Inertia Vue.JS installed and running you can check how by looking into the first sections of the guide Using FormKit with Laravel Inertia.

Now you can install using your preferred package manager by following this bash command:

npm install @formkit/inertia

Usage

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.

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.

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:

<script setup lang="ts">
  import { useForm } from '@formkit/inertia'

  const form = useForm()
  const submitHandler = form.post('/login')
</script>

<template>
  <FormKit type="form" @submit="submitHandler" :plugins="[form.plugin]">
    <FormKit type="text" name="username" label="Username" />
    <FormKit type="password" name="password" label="Password" />
  </FormKit>
</template>

You could also also define the handler directly in your template:

<FormKit
  type="form"
  @submit="(fields, node) => form.post('/login')(fields, node)"
  :plugins="[form.plugin]"
>
  <!-- The rest of your form -->
</FormKit>

The functions support all visit options from Inertia, such as preserveState, preserveScroll, and event callbacks.

The options event callbacks will overwrite any default events to that specific event, meaning that if you for example add onStart you will lose the events from start that are for example loading, disabling and processing.

<FormKit
  type="form"
  @submit="(fields, node) => form.post('/login', {
    preserveScroll: true,
    onSuccess: () => form.node.reset(),
  })(fields, node)"
  :plugins="[form.plugin]"
>
  <!-- The rest of your form -->
</FormKit>

To cancel a form submission, use the cancel() method.

<FormKit
  type="form"
  @submit="(fields, node) => form.post('/login')(fields, node)"
  :plugins="[form.plugin]"
>
  <!-- The rest of your form -->
</FormKit>

<FormKit type="button" @click="form.cancel()" label="Cancel" />

(back to top)

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

<template>
  <FormKit type="form" @submit="submit" :plugins="[form.plugin]">
    <FormKit type="text" name="username" label="Username" />
    <FormKit type="password" name="password" label="Password" />

    <template #submit>
      <FormKit type="submit" label="Log in" :disabled="form.processing" />
    </template>
  </FormKit>
</template>

(back to top)

Addons

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:

<script setup lang="ts">
  import { useForm } from '@formkit/inertia'

  const form = useForm()
  form.addon((on) => {
    on('before', (visit, node) => {
      return confirm('Are you sure you want to delete this user?')
    })

    on('success', (page, node) => {
      toast('User deleted.')
    })
  })
</script>

If you need a single event callback useForm() also returns on() directly:

<script setup lang="ts">
  import { useForm } from '@formkit/inertia'

  const form = useForm()
  form.on('before', (visit, node) => {
    return confirm('Are you sure you want to delete this user?')
  })
</script>

(back to top)

Roadmap

  • Make the success and error events to be able to return a Promise<void> to delay the call to the finish event
  • Add support for Laravel Precognition

(back to top)

Types

useForm
export const useForm: <F extends RequestPayload>(initialFields?: F | undefined) => {
  on: <T extends "before" | "start" | "progress" | "finish" | "cancel" | "success" | "error" | "cancelToken">(name: T, cb: EventCallback[T]) => void;
  addon: (addons: AddonExtension | AddonExtension[]) => void;
  plugin: (node: FormKitNode) => false | undefined;
  node: Ref<FormKitNode | null>;
  dirty: Ref<boolean | null>;
  errors: Ref<boolean | null>;
  valid: Ref<boolean | null>;
  processing: Ref<boolean>;
  progress: Ref<number>;
  recentlySuccessful: Ref<boolean>;
  wasSuccessful: Ref<boolean>;
  submit: (method: Method, url: URL | string, options?: Exclude<VisitOptions, 'method' | 'data'>) => (data: F, node: FormKitNode) => void;
  get: (url: URL | string, options?: Exclude<VisitOptions, 'method' | 'data'>) => (data: F, node: FormKitNode) => void;
  post: (url: URL | string, options?: Exclude<VisitOptions, 'method' | 'data'>) => (data: F, node: FormKitNode) => void;
  put: (url: URL | string, options?: Exclude<VisitOptions, 'method' | 'data'>) => (data: F, node: FormKitNode) => void;
  patch: (url: URL | string, options?: Exclude<VisitOptions, 'method' | 'data'>) => (data: F, node: FormKitNode) => void;
  delete: (url: URL | string, options?: Exclude<VisitOptions, 'method' | 'data'>) => (data: F, node: FormKitNode) => void;
  cancel: () => void;
}
AddonExtension
export type AddonExtension = (on: ReturnType<typeof createEventManager>['on']) => void;

(back to top)

版本与发布记录

@formkit/inertia 共发布 5 个版本,首次发布于 2023/10/31;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.5latest2023/11/113 年前
0.1.42023/11/103 年前
0.1.32023/11/083 年前
0.1.22023/11/023 年前
0.1.12023/10/313 年前

关于 @formkit/inertia 的常见问题

@formkit/inertia 是什么?

FormKit + Inertia = ❤️

如何安装 @formkit/inertia?

在项目目录运行 npm install @formkit/inertia,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:vue(>= 3.0.0)、@formkit/vue(>= 1.2.0)、@inertiajs/vue3(>= 1.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@formkit/inertia 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@formkit/inertia 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@formkit/inertia 可以商用吗?

@formkit/inertia 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@formkit/inertia 的下载量有多少?

根据 npm 官方下载统计,@formkit/inertia 在 2026/10/16 – 2026/11/14 这 30 天内被下载 4,874 次,日均约 162 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@formkit/inertia 还在维护吗?

最新版本 0.1.5 发布于 2023/11/11(3 年前);至今共发布 5 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/10/31。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。