Comments (6)
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.
How would the onReset
form event be triggered if I reset the form like this:
const formActions = useFormActions(formAtom);
formActions.reset();
from form-atoms.
I would personally use a native form reset. But to accomplish it programmatically, it's the same:
flushSync(() => {
formActions.reset()
})
onReset();
from form-atoms.
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.
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.
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)
- `FormAtomValues` hints wrong type to the developer HOT 5
- `FormAtomValues` should accept `FormAtom` instead of `FormAtomFields` HOT 2
- Number values cannot be used safely with `<InputField />` nor `useInputField` HOT 23
- Set debug labels for the form atoms HOT 3
- Add `clear` action to support controlling `input[type='file']` HOT 3
- Permit array as root form fields HOT 1
- Use atom id instead of 'unnamed-field' in debugLabels HOT 2
- When input is `required` the `_validateCount` increases while the validate function is not being called HOT 9
- Debug label not set in vite HOT 3
- What's the reason for limiting setting the initial value only once? HOT 10
- Pass setter to the `Validate` callback.
- Outdated lockfile? HOT 3
- Release workflow expression syntax error
- Export `validateAtoms` on `formAtom` similarly to `_validateCallback` on `fieldAtom` HOT 1
- Hydration with `useFieldInitialValue` in Next.js renders twice - first empty, then filled input. HOT 21
- Field & Form atoms should be of `PrimitiveAtom` type HOT 4
- Debug label completion HOT 1
- Examples: Validation Option Valibot along with zod HOT 2
- Bug - the `useField` reads value before initialization HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from form-atoms.