-
-
Notifications
You must be signed in to change notification settings - Fork 245
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
Add submitting
state property that is true
while onSubmit
is pending
#449
Conversation
- The successs and failure callback behaviour was not being tested when submitting without `onSubmit`. - The value passed to the `onSubmit` callbacks was not tested.
A new state property `submitting` is introduced, which is `true` while an onSubmit promise is pending and `false` otherwise.
The presence of `onSubmitFailure` makes it look like it might be ok to let `onSubmit` throw an error (as well as rejecting the promise it returns). If `onSubmit` does throw an error, the form itself could enter an invalid state and the uncaught error message would be mixed up with uniforms implementation, so it would be hard for the end user to debug.
Codecov Report
@@ Coverage Diff @@
## master #449 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 157 157
Lines 1404 1414 +10
=====================================
+ Hits 1404 1414 +10
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two minor things and one bigger one.
Great work with the tests!
@@ -66,7 +66,6 @@ describe('BaseForm', () => { | |||
expect(context.uniforms).toHaveProperty('state', expect.any(Object)); | |||
expect(context.uniforms.state).toHaveProperty('changed', false); | |||
expect(context.uniforms.state).toHaveProperty('changedMap', {}); | |||
expect(context.uniforms.state).toHaveProperty('submitting', false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that this test is still needed (or at least it's a nice to have).
Besides that last comment, I really like it. You'd like to add the |
I will be implementing |
So let's work on it here. I'll think about the changelog and documentation. |
The existing collection of these tests was almost impossible to follow.
Ping @BudgieInWA. |
@radekmie Sorry about the delay and the git history. Here is my crack at I think submitting an
I don't know if it would be a better API if it went through these states:
|
Indeed, this is not so good. There's one more thing: should the I think it should work, by adding Opinions? (sorry for the delay) |
I think we want to define these flags as follows for easiest use:
In that case, I think it's fine that This seems to line up with your take, so I will start building. (we're not in a hurry 😄) |
Ping @BudgieInWA. |
@radekmie, how's that? |
.catch(() => { | ||
// `onSubmit` should never reject, so we ignore this rejection. | ||
}) | ||
.finally(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we are after catch
, then
would be better here.
@@ -33,6 +33,8 @@ const Validated = parent => class extends parent { | |||
]).isRequired | |||
}; | |||
|
|||
// TODO add `uniforms.state.validating` to `childContextTypes`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not obvious how this should be done. Unlike the actual state, with getChildContextState
, the static childContextTypes
is not assembled in this way. What we need is
static childContextTypes = merge(
{ uniforms: { state: { validating: PropTypes.bool.isRequired } } },
parent.childContextTypes
);
but I don't know of any such merge
function. Do we need to store the prop types nested in plain objects as well? Then we can have a shapify
function that turns the plain object tree into a PropTypes.shapeOf
tree for assignment to childContextTypes
and the extending classes can merge the plain objects (like lodash.merge
would) before assigning their own childContextTypes
.
Or am I missing something simple?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a way to merge them, but differently - validate it twice. Prop types are just functions, so we can create a new one, which will start with calling the old one.
For now, I'd export an object from the BaseForm
, marked as temporary (with comment and underscore prefixed name).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3640cde is my take on exposing a PropTypes object that can be extended. I'm happy for you to rename and move stuff around, maybe refactor shapify
and BaseForm.shapifyPropTypes
into a file or util library.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, from the functional level. I'd rather not have shapifyPropTypes
at all (but use shapify
directly) and put plainChildContextTypes
in an export (again, underscored) not to pollute BaseForm
.
Should I do it, @BudgieInWA?
@BudgieInWA it looks great! Just two minor things. |
It took some time, but we finally have it! Thanks again, @BudgieInWA! |
static childContextTypes = parent.shapifyPropTypes(plainChildContextTypes); | ||
static childContextTypes = { | ||
...parent.childContextTypes || {}, | ||
uniforms: childContextTypes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is awkward because parent.childContextTypes.uniforms
is replaced with BaseForm.childContextTypes.uniforms
(through childContextTypes
) no matter what parent
is. That is why BaseForm.plainChildContextTypes
was a part of BaseForm
(export __childContextTypesBuild
is still better than BaseForm.shapifyPropTypes
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know. There’s a small chance, that it happen not to be the case. Anyway, I’m sure it’s not really important, as the new context API doesn’t have this problem (and I’m thinking about it more and more....).
This PR closes #441.
This is my attempt to implement the feature tracked in #441 in uniforms core. I am looking forward to feedback, and if it turns out that this solutions would be acceptable, I can implement
validating
similarly.The commit messages provide motivation for the individual changes.