GithubHelp home page GithubHelp logo

jeremy-london / formik-wizard Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zaguiini/formik-wizard

0.0 1.0 0.0 185 KB

A multi-step form component powered by formik and react-albus

License: MIT License

JavaScript 32.12% TypeScript 67.88%

formik-wizard's Introduction

THIS LIBRARY IS LOOKING FOR MAINTAINERS. I (LUIS FELIPE ZAGUINI) DON'T HAVE ENOUGH BANDWIDTH ANYMORE TO WORK ON OSS. IF YOU ARE INTERESTED, PLEASE CONTACT ME: luisfelipezaguini [at] gmail [dot] com

Also, we're in the process of making this library way more flexible, allowing people to use whatever form and wizard library they like. Please read this issue and comment if you will to contribute.


formik-wizard

A multi-step form component powered by formik and react-albus.

Why?

Large forms are generally bad for User Experience: it becomes both tiresome to fill and, in most of the cases, it gets slow. I've built this lib to tackle this problem: dividing one big form in multiple smaller forms, it gets much easier to reason about, both as a developer and as a user.

All the smaller forms may include validation (powered by yup) and default values.

You can check the demo here, with the corresponding source code here.

Installation

You need to have formik and react-albus installed -- they are peer dependencies. After that, just yarn add formik-wizard and you're good to go!

If you plan to validate the sections, you need to install yup as well!

Usage

Check out the example source code and the typings. There's a hook called useFormikWizard that you can use to read and write sections values and form statuses. I recommend using immer because you're modifying the steps data directly!

Usage with react-native

It's pretty straightforward: just use the Form prop component as a children forwarder. Example:

<FormikWizard
  {...props}
  Form={({ children }) => children}
/>

That's needed because there's no form web component on React Native and formik-wizard (and formik) fallbacks to it.

Also, React Native doesn't have a submit button/input. To achieve a similar result, grab formik's context and fire its submit handler.

REMEMBER: IT'S NOT FORMIK-WIZARD'S CONTEXT. IT'S FORMIK'S!!!

Troubleshooting

I can't use it as the default export

That's a known issue. Jared palmer's tsdx doesn't handle default exports very well. Two options:

Use it as FormikWizard.default

import FormikWizard from 'formik-wizard'

function App() {
  return <FormikWizard.default />
}

or...

Use the named export

import { FormikWizard } from 'formik-wizard'

function App() {
  return <FormikWizard />
}

How do you use setStatus, setSubmitting inside handleSubmit function?

The onSubmit function expects a Promise. Whatever you return from that Promise will be set as the status. For example:

import { useCallback } from 'react'

const handleSubmit = useCallback((values) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        message: "success"
      })
    }, 5000)
  })
}, [])

While that Promise is pending, the isSubmitting flag is set to true. The status is set automatically from the return of that Promise.

How do you access the Formik context inside the step form (e.g. for conditional rendering)?

The step form is wrapped inside a Formik component but its props aren't propagated to the form component. Anyway, you'll still have access to the Formik context through one of these methods:

  • by using the connect HOC.
  • by using the Field component with a render prop or a callback function as children.
  • by using the useFormikContext hook (available in Formik's v2).

License

MIT

Credits

This project was bootstrapped with TSDX.

formik-wizard's People

Contributors

zaguiini avatar dependabot[bot] avatar michelts avatar rodolfosilva avatar chug2k avatar remiroyc avatar

Watchers

 avatar

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.