final-form / react-final-form

🏁 High performance subscription-based form state management for React
https://final-form.org/react
MIT License
7.38k stars 480 forks source link

Feature Request: add handleReset to FromRenderProps #381

Open Toanzzz opened 5 years ago

Toanzzz commented 5 years ago

Are you submitting a bug report or a feature request?

Feature request

What is the current behavior?

The FormRenderProps have reset function, but you still need to write custom hander for reset button click, or form's onReset event

const initialValues = {/*...*/}
const handleReset = reset => () => reset(initialValues)

<Form
  initialValues={initialValues}
  render={({ handleSubmit, reset }) =>
    <form onSubmit={handleSubmit} onReset={handleReset(reset)}>
      {/*...*/}
      <button type='reset'>{'Reset'}</button>
      <button type='submit'>{'Save'}</button>
    </form>
  }
/>

What is the expected behavior?

It could be nice if FromRenderProps provide handleReset function, similar to handleSubmit that could give directly to the <form> tag. It should have default behavior to pickup and pass current form's initialValues to reset function.

const initialValues = {/*...*/}

<Form
  initialValues={initialValues}
  render={({ handleSubmit, handleReset}) =>
    <form onSubmit={handleSubmit} onReset={handleReset}>
      {/*...*/}
      <button type='reset'>{'Reset'}</button>
      <button type='submit'>{'Save'}</button>
    </form>
  }
/>

Other information

I'd happy to make a pull request for this

TrySpace commented 3 years ago

I'd like to second this, but also for the <Field> component.

I'm trying to get a custom component to 'reset' its content when .reset() is called on the parent Form, so I need some sort of callback to manually set the value of the custom component, since in my usecase it is not responding to the parent value, I need a callback.