-
-
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
[Feature Request] Pass the modifier along with the document #66
Comments
Finally! import {BaseForm} from 'uniforms';
import {AutoForm} from 'uniforms-semantic'; // or any other
const Modifier = parent => class extends parent {
static Modifier = Modifier;
static displayName = `Modifier${parent.displayName}`;
onSubmit (event) {
if (event) {
event.preventDefault();
event.stopPropagation();
}
if (this.props.onSubmit) {
// You can also omit unchanged keys.
const $set = this.getModel();
const $unset = this.getChildContextSchema().getSubfields()
.filter(key => !$set[key])
.reduce((acc, key) => ({...acc, [key]: ''}), {})
this.props.onSubmit({
...Object.keys($set).length > 0 && {$set},
...Object.keys($unset).length > 0 && {$unset}
});
}
}
};
// This might seem a little bit crazy, but we have to override BaseForm#onSubmit
// If you are using for example Bootstrap3, then change AutoForm.Semantic to AutoForm.Bootstrap3
export default AutoForm.Auto(AutoForm.Validated(AutoForm.Quick(AutoForm.Semantic(Modifier(BaseForm))))); From here, you can easily create an Note: This API will remain unchanged, but some shortcuts are planned. |
Whoa! This is a precious gem! It never occured to me to extend from existing form components like this, wow! |
You provide an excellent explanation @radekmie about how to construct a modifier form. What is the proper way to actually use the modifier form you created in the example, given a schema, model and function? My first idea is would be: import UpdateForm from '/path/to/updateform.js';
import React, { Component } from 'react';
class Update extends Component{
...
render(){
...
<UpdateForm
schema={SomeImportedSchema}
model={SomeProvidedModel}
onSubmit={SomeProvidedFunction}
/>
...
} This does not render a form. Do I overlook something? |
No, @GoaGit, it's my fault - I wrote it as a concept and haven't tested it. Now I've done and I've just updated that implementation. |
That's really awesome @radekmie. You've created a very elegant solution. Looking forward to be able to use it. |
Form events such as
onSubmit
currently only passesdoc
but there are cases where amodifier
is more suitable, especially forupdate
formsConsider a schema where all the fields are optional
and consider this document:
Now if you delete the value of foo and submit, this will throw error saying that after filtering out, modifier is empty.
This is expected because
collection2
trims trailing spaces, deletes empty fields, runs autovalues etc before doing the actual insert, at which timedoc
becomes{}
so instead, we need to$unset: { foo: '' }
so to mitigate this, I am currently doing this:But it would be awesome if
onSubmit
were able to provide me themodifier
out of box, such that, the following would work:Now the original autoform has solutions similar to this:
onSubmit
passes a doc and a modifier and aformToModifier
hook which directly takes the modifier whenever form fields change (like anonChange
for update forms)For code reference, you can take a look at here and here
The text was updated successfully, but these errors were encountered: