question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request for a `createForm` function or another way to wrap `useForm`

See original GitHub issue

Hey, I’m building a library where I have a custom form helper that looks exactly like SlimeForm. I think SlimeForm is doing a very good job, and I would love to depend on it for my own form helper.

The issue is that I need to override the submit function with custom logic (this is for a routing library). I wanted to provide my own useForm that would defer the logic to SlimeForm, but I can’t get the typings because they’re not exported.

I was initially going to PR to export them, but I thought it would be nice if SlimeForm provided some API to generate a pre-configured useForm hook.

I’d happily PR that but I wanted to see if that would be accepted. I’m not sure about the API, but I’m thinking something like that:

// Somewhere in my own library
import { router } from './router'
import type { VisitOptions } from './types'
import { createForm } from 'slimeform'

export const useForm = createForm({
  submit: ({ form }) => async (overrides?: VisitOptions) => {
    // Here I implement my custom submit method, and this method is returned
    return await router.visit({
      method: overrides.method ?? 'POST',
      data: form,
      ...overrides
    })
  },
})
// Somewhere in userland, using my library
import { useForm } from 'my-library'

const { form, submit } = useForm({
  form: () => ({
    email: '',
    password: '',
  })
})

// These parameters are from `VisitOptions`
submit({ url: '/register', method: 'POST' })

Basically, every property from the object given to createForm would be a callback that return a function, and that function would be provided to the user. The callback would have form and status given to it.

It seems a bit complicated, but it’s also flexible. If you have another idea on how I could achieve my goal, I’m open to it too!

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
innocenzicommented, Jun 29, 2022

I’ll investigate and report back, thanks!

0reactions
LittleSoundcommented, Jun 29, 2022

About these questions:

Question 1

The submitter function was not designed to be used in this way, perhaps you can read the documentation to understand its intent.

import { mySubmitForm } from './myFetch.ts'
const { _, submitter } = useForm(/* ... */)
// Wrap the generic submission code and use it later
const { submit, submitting } = submitter(mySubmitForm({ url: '/register', method: 'POST' }))

In general, the submit function is bound to the form’s @submit.prevent, so it doesn’t really need to be called with parameters. But you can provide various arguments when you create it as in the code above.

Question 2 and 3

I tried reading the code you provided, but I can’t see what is causing these errors, I need a runnable minimal reproduction and an issue. Note: They are currently working fine in 🚀 slimeform-playground.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create powerful and flexible forms with React Hook Form
Learn how to create performant, flexible, and extensible forms with easy-to-use validation with React Hooks.
Read more >
useForm | React Hook Form - Simple React forms validation
This option allows you to configure validation strategy when inputs with errors get re-validated after a user submits the form ( onSubmit event...
Read more >
Build a React form component via react-hook-form
Here's a quick guide to building functional forms with the ... Finally, we wrap our form in the <Styles> tag to apply the...
Read more >
How to Create Forms in React using react-hook-form
In such a case, we can call the reset function returned by the useForm hook to clear the form data. const { reset...
Read more >
Build forms using React, the easy way ( with Typescript )
This tutorial would require you to have some knowledge of modern react ... import { useState } from "react"; // useForm functional component ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found