-
-
Notifications
You must be signed in to change notification settings - Fork 248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Request: onSubmit callback or onSubmitSuccess/onSubmitFailure events #51
Comments
Yep, that's a quite common use case. How about simply doing a I don't really get your idea with |
I typed in a great comment on this last week... but never submitted it and I don't feel like re-typing :/ I think the idea of a Can you provide an example of: onSubmit, sets the button to disabled until submit handler resolves? |
IntroductionLet's assume, that class Example extends React.Component {
constructor (props) {
this.onSubmit = this.onSubmit.bind(this);
this.onSubmitNext = this.onSubmitNext.bind(this);
}
onSubmit (data) {
return new Promise((resolve, reject) => {
this.props.handleSubmit(data, (error, response) =>
error
? reject(error)
: resolve(response)
);
});
}
onSubmitNext () {
// This will be either:
// - a Promise (success)
// - undefined (validation error)
const promise = this.form.submit();
if (promise) {
promise.then(
response => console.log('OK', response),
error => console.log('NO', error)
);
}
}
render () {
return (
<main>
<AutoForm ref={form => this.form = form} onSubmit={this.onSubmit} />
<button onClick={this.onSubmitNext}>
Save & Continue
</button>
</main>
);
}
} FailThis would work perfectly fine, but... SolutionSimply - // onSubmit returns something synchronously or returns a Promise
const Example = ({onSubmit, onSubmitSuccess, onSubmitFailure}) => {
let ref;
return (
<main>
<AutoForm ref={ref => form = ref} onSubmit={onSubmit} />
<button onClick={() => ref.submit().then(onSubmitSuccess, onSubmitFailure)}>
Save & Continue
</button>
</main>
);
}; Note: This won't work with |
Yup - this looks like it's a perfect fit... I'll try it out and let you know if I run into problems... This is very much worth including in docs/examples, as it's a very common use case... you rule! Bonus points: |
I'll reopen this, because... There's no returned value from I don't clearly understand, what you mean - could you develop this idea a little? |
I think I'm trying to make your first example work... without setting the state of the class Example extends React.Component {
constructor (props) {
this.state = { submitting: false, success: false, failure: false };
this.onSubmit = this.onSubmit.bind(this);
}
onSubmit(data) {
this.setState({ submitting: true });
const promise = this.onSubmitMakePromise(data);
if (promise) {
promise.then(
response => this.setState({ submitting: false, success: true })
error => this.setState({ submitting: false, failure: true })
);
}
}
onSubmitMakePromise(data) {
return new Promise((resolve, reject) => {
this.props.handleSubmit(data, (error, response) =>
error ? reject(error) : resolve(response)
);
});
}
...
} |
No, @zeroasterisk - you are not setting state of the |
Of course, we can do both - |
I think we are getting closer every day to a point where we'll be needing form lifecycle hooks :) |
To be honest, I've just finished it few minutes ago and wanted to push it, but new bugs appeard, so it will wait. Probably yes, @serkandurusoy - kind of. |
Example:// New props
<AutoForm
schema={schema}
onSubmit={model => db.saveThatReturnsPromise(model)}
onSubmitSuccess={() => alert('Saved!')}
onSubmitFailure={() => alert('Error!')}
/>
// New API
baseFormRef.submit().then(
() => console.log('Submitted'),
() => console.log('Submit error')
)
validatedFormRef.submit().then(
() => console.log('Submitted'),
() => console.log('Invalid or submit error')
) To summarize current (new) API changes:
How it worked:
How it works now:
Planned changes:
Any feedback appreciated. |
@radekmie Just so to make it clear, is this breaking change? Do existing apps on rc26 need to change their existing onSubmit handlers moving forward? |
Only, if you were programmatically calling |
Hm, as much as I appreaciated the promise based API here, does this not introduce an incosistency to how submit event is getting handled? |
I don't get your point. Why? |
Well, my train of thought is onSubmit => handler passed the doc I would expect onSubmit and ref.submit produce the same output. |
Internally a |
Hm I don't quite understand if It it does, in that case, onSubmit becomes redundant, if it does not, I would very much say deprecating onSubmit would then be a better idea for sake of future opportunities. |
No, |
Oh, so if the So in essence, Do I get it right? |
Yes, indeed. |
I'm impressed - i think this is a very elegant solution... |
If I am submitting a form via a method call (for example) and it's
async
--- I would like the ability to pass down a callback and act on it's response.The new API for
formRef.submit()
is great, but I can not see any way to act on results from that submit.Here's my current use case:
Save & Continue
button, should submit the form, and on success, navigate to the next page.The problem is, I can trigger
formRef.submit()
but I have no way to wait on the response of it, from the React component level, without involving internal state.Here is what I'm doing now to handle this:
But, I could imagine enabling some alternate submit-like handler, when clicking on the "continue" button, which passed either extra arguments into our
onSubmit
function, or received standard events or callbacks from it.The text was updated successfully, but these errors were encountered: