Giter Club home page Giter Club logo

formkit-addon-inertia's Introduction

FormKit Addon Inertia

Addon for integrating InertiaJS with FormKit

Table of contents

Getting Started

There are two ways to use this addon, firstly there is the composition way, and there is also a formkit plugin.

In the end, the plugin uses the composable inside of it with the correct form node for easy of use.

npm i formkit-addon-inertia

1. Composition

The useInertia is a function that receives a FormKit node and returns all Inertia HTTP methods. Those are visit, get, post, put, patch, delete and reload.

<script setup>
  import { useInertia } from "formkit-addon-inertia";
</script>

<template>
  <FormKit
    type="form"
    @submit="(fields, node) => useInertia(node).post('/users', fields)"
  >
    <FormKit type="text" name="name" label="Name" />
    <FormKit type="email" name="email" label="E-mail" />
  </FormKit>
</template>

2. Plugin

The inertia context property has all Inertia HTTP methods. Those are visit, get, post, put, patch, delete and reload.

<script setup>
  import { plugin as inertiaPlugin } from "formkit-addon-inertia";
</script>

<template>
  <FormKit
    type="form"
    :plugins="[inertiaPlugin]"
    @submit="(fields, node) => node.context.inertia.post('/users', fields)"
  >
    <FormKit type="text" name="name" label="Name" />
    <FormKit type="email" name="email" label="E-mail" />
  </FormKit>
</template>

You can add this plugin as a global formkit plugin so every form has it, instead of defining manually like the example above

import { createApp } from "vue";
import App from "App.vue";
import { plugin, defaultConfig } from "@formkit/vue";
import { plugin as inertiaPlugin } from "formkit-addon-inertia";

createApp(App)
  .use(plugin, defaultConfig({ plugins: [inertiaPlugin] }))
  .mount("#app");

Options

You can use all of InertiaJS callbacks, and we add the FormKit node as the last argument for easy integration of your features.

We by default add some features inside Inertia callbacks to make the use smoother.

There are four features, loading message, disabled prop, progress data attribute and automatic field errors. You can disable any of these by passing any of these properties to the options.

{ disableLoading: true, disableDisabled: true, disableProgress: true, disableErrors: true }

<script setup>
  const onSuccess = (page, node) => {
    // Do any onSuccess logic here
  };

  const submit = (fields, node) => {
    node.context.inertia.post("/users", fields, {
      onSuccess,
      disableProgress: true,
    });
  };
</script>

<template>
  <FormKit type="form" @submit="submit">
    <FormKit type="text" name="name" label="Name" />
    <FormKit type="email" name="email" label="E-mail" />
  </FormKit>
</template>

Contributing

All contributions are welcomed and appreciated!

  • You can always star it!
  • Any bug you found can be reported by opening an issue
  • If you have any cool ideas or features you want to be added just open a discussion about it
  • You can make pull request with fixes or features, read out the contributing guide to get started

License

MIT

formkit-addon-inertia's People

Contributors

fenilli avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

formkit-addon-inertia's Issues

Inertia options param (callbacks) disrupting formkit error handling

Describe the bug
Anything passed to the inertia options param breaks expected formkit behavior. For example, the formkit handling of backend validation errors by field is disrupted when any inertia options are included.

To Reproduce
Add anything, even empty { }, to an inertia call. Only verified with inertia.post() so far.
I'm using this repo's adonisjs stack.

Example Playground.vue (I added console to vue global):

<template>
  <FormKit
    type="form"
    submit-label="Login"
    @submit="(fields, node) => node.context?.inertia.post(
      '/login',
      fields,
      // Form backend validation does not display for each field if ANY options param is set, even an empty {}
      // {}
      {
        onBefore: (visit, node) => {
          $console.log(visit, node);
          // Also it would be helpful to be able to return false from onBefore to cancel the request per the inertia docs
          // https://inertiajs.com/manual-visits#event-callbacks
        },
        onError: (errors, node) => {
          $console.log(errors);
          node.setErrors('testError', 'this should work too?'); //is this node being passed through correctly?
        }
      }
    )">
    <!-- <FormKit type="text" label="E-mail" name="email" validation="required|email" />
    <FormKit type="password" label="Password" name="password" validation="required" autocomplete /> -->
    <FormKit type="text" label="E-mail" name="email" />
    <FormKit type="password" label="Password" name="password" autocomplete />
  </FormKit>

  <pre>{{ $attrs }}</pre>
</template>
)">

Versions (please complete the following information):

  • FormKit: [e.g. 1.0.0-beta.10]
  • InertiaJS: [e.g. 0.11.0]
  • formkit-inertify: [2.0.0-alpha.4]

add some reactivity props provided by useForm

Hello,

theres some great props provided by inertia such as

    isDirty: Boolean,
    processing: Boolean,
    wasSuccessful: Boolean,
    recentlySuccessful: Boolean,

this will help to do many thing such disabling some content out side the form
or creating a toast ... etc

some reference

https://inertiajs.com/forms#form-helper
https://inertiajs.com/releases/inertia-react-0.5.4-2021-02-25#api
https://discord.com/channels/793529058377072650/1025447518617735210/1052994285991108718

Thank you.

Add support for Laravel Precognition

Laravel precognition lets you use your server side validation rules on the frontend. The thing is, you cannot have both laravel precog and formkit validation rules, they tend to conflict.

An improvement would be to add a special syntax like validation="prec:required" where the plugin will know to disable the formkit validation when needed.

Header response 409

Hi Again, it seems there still a proble with dependencies with inertiajs.
i have a form which for testing i submit with inertia builtin 'useform' (trasforming the data with the form fields) and with the 'useInertia' from your package.

when using useInertia, it seems to not include the required "X-Inertia-Version:" into the header request, causing a 409 versioning error (and then reloading the whole page)

i suppose, since this is a behaviur related to inertia itself, it could depende on package version since inertia had a major update.
im on: "@inertiajs/vue3": "^1.0.2"

thanks for your time!

issue when on update form

hi,

how can i set the data without v-model on update form? like what you put it in the example code

and is there a way to pass the disabled button if i want to use custom action button?

thank you.

When you use prevent on @submit event i throws an error saying e.preventDefault is not a function

So the scenario is this. When you actually have actions to true, and just use the form submission online, it works as a charm, but when you actually hook it to a function, It throws an error.

So the idea is as this example:
I will try to solve it but help is also appreciated if you came across it earlier.

// methods

const submitRequest = (flds, node) => {
    useInertia(node).post(route('category.store'), flds)
}

and in formkit

                            <FormKit
                            type="form"
                            :actions="false"
                            @submit="submitRequest"
                            >

errorBags, preserveScroll and more, Not working anymore

Hi,

Everything was working great but after I updated vue the recentlySuccessful was not working.

And then I upgraded everything even this Addon to @next

Now it return TypeError: str.normalize is not a function

When there an error and the preserveSroll not working anymore

I use Laravel inertia

    "@inertiajs/vue3": "^1.0.0-beta.2",
    "formkit-addon-inertia": "next",
    "@formkit/vue": "^1.0.0-beta.12",


    "@vitejs/plugin-vue": "^4.0.0",

Thank you πŸ™πŸ»

Followed the composable way but component isn't rendered

Hello,

I just copy pasted the composable way but it looks like the inputs arent rendered.

Schermafbeelding 2022-11-24 om 09 37 01

i'm using

 "@inertiajs/inertia": "^0.11.0",
        "@inertiajs/inertia-vue3": "^0.6.0",
        "@inertiajs/progress": "^0.2.7",
        "@vitejs/plugin-vue": "^3.0.0",
        "autoprefixer": "^10.4.2",
        "laravel-vite-plugin": "^0.5.0",
        "postcss": "^8.4.6",
        "vite": "^3.0.0",
        "vue": "^3.2.31"
    },
    "dependencies": {
        "@formkit/vue": "^1.0.0-beta.12",
        "@inertiajs/inertia": "^0.11.0",
        "@inertiajs/inertia-vue3": "^0.6.0",
        "formkit-addon-inertia": "^1.0.0",
        "postcss-nesting": "^10.1.10",
        "vue": "^3.2.36"

Are you sure that

<script setup>
import { useInertia } from "formkit-addon-inertia";
</script>

<template>
  <FormKit type="form" @submit="(fields, node) => useInertia(node).post('/user', fields)">
    <FormKit type="text" name="name" label="Name" />
    <FormKit type="email" name="email" label="E-mail" />
  </FormKit>
</template>

would load the formkit form? no registration on my app.js or anything?

thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.