Coder Social home page Coder Social logo

onReset callback about form-atoms HOT 6 CLOSED

jaredlunde avatar jaredlunde commented on June 23, 2024
onReset callback

from form-atoms.

Comments (6)

jaredLunde avatar jaredLunde commented on June 23, 2024

Can't you use the native onReset event? https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/reset_event

import { flushSync } from 'react-dom';
...

           <form
              onSubmit={form.submit(async (values) => {
                  await onSubmit(values, formActions);
              })}
              onReset={(event) => {
                flushSync(() => {
                  form.reset();
                 })
                 
                onReset?.(event);
              }}
              {...elementProps}
            >
              {typeof children === "function"
                ? children(form.fieldAtoms, formActions)
                : children}
            </form>

Or can you do something with derived state? https://jotai.org/docs/guides/composing-atoms#basic-derived-atoms

from form-atoms.

schiller-manuel avatar schiller-manuel commented on June 23, 2024

How would the onReset form event be triggered if I reset the form like this:

const formActions = useFormActions(formAtom);
formActions.reset();

from form-atoms.

jaredLunde avatar jaredLunde commented on June 23, 2024

I would personally use a native form reset. But to accomplish it programmatically, it's the same:

flushSync(() => {
  formActions.reset()
})
                 
onReset();

from form-atoms.

jaredLunde avatar jaredLunde commented on June 23, 2024

That is, I don't see why this is needed in the library. You can just invoke the reset thing you're doing when you invoke the reset. Or use Jotai's derived state patterns.

from form-atoms.

schiller-manuel avatar schiller-manuel commented on June 23, 2024

It is not needed, it would be nice to have. I have a component that listens to the dirty state of the form atom and displays save/discard buttons in case the form is dirty. When the discard button is clicked, the form is reset via formActions.reset().
Currently I have to prop drill the onReset callback to that component.
If the formAtom would execute it upon reset, the component would not need to be made aware of it.

from form-atoms.

jaredLunde avatar jaredLunde commented on June 23, 2024

There are ways to solve this with React/Jotai (prop drilling, as you mentioned is one). Minor inconveniences are perfectly OK in software engineering and do not necessitate unnecessary abstractions (as my GitHub bio has trumpeted for years 😏)

from form-atoms.

Related Issues (20)

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.