-
-
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
How to us "onChange" of one field and get/set value to other field? #278
Comments
Oh now I use <AutoForm
schema={Payments.schema}
showInlineError={true}
onSubmit={this.handleSubmit}
onChange={(key, value) => {
console.log(key, value);
// how to set value to balance field?
this.balanceField.value(????????) // example???
}}
>
<AutoField name="totalAmount" value={5000} /> // due amount of invoice
<AutoField name="paidAmount" />
<AutoField name="balance" /> Please |
Not good for onChange={(key, value) => {
console.log(key, value);
// how to set value to balance field?
$('[name="balance"]').val(1000)
}} |
today I tried handleSubmit = (doc) => {
console.log(doc);
};
-------
<AutoForm
schema={Payments.schema}
showInlineError={true}
onSubmit={this.handleSubmit}
onChange={(key, value) => {
if (key == "paidAmount") {
$('[name="balance"]').val(value);
}
}}
>
<AutoField name="paidAmount" {...formItemLayout} />
<AutoField name="balance" disabled={true} {...formItemLayout} />
</AutoForm> but can't get Object {paidAmount: 50, balance: 0} |
Most straightforward solution is: const Amount = ({onChange, ...props}) =>
<AutoField
{...props}
onChange={value => {
onChange(value);
onChange(value * 2, 'balance');
}}
/>
;
export default connectField(Amount, {ensureValue: false, includeInChain: false}); <AmountField name="amount" />
<AutoField name="balance" /> Please keep your statements more clear in the future. Also please do not create multiple comments, edition of the first will be fine. It will help to keep this repository neat. |
Sorry, I will try soon. |
Now I tried, but still don't work. |
@thearabbit did you remove the onChange from the Form level? try it only in the Field level. onChange at the form level is probably not what you want --- use @janowsiany 's suggestion with a custom connected field. Still having problems? Can you make an example repo with this one bad use case? I'd clone and try it out for you. FYI: I have a formable HOC which I use to give me access to the |
Calling I've found |
It depends on where and which
Could you say something more? |
I have an extremely responsive form, albeit not quite perfect. What I did was to use a modern functional react component with useState to set the model to internal state. The key for every change you want to see and then make is to always update the model itself so the next render is accurate.
Thus, you must be willing to clone the complete model on every key stroke. Every field should have an onChanged function that follows this (now obvious to me) pattern:
You can imagine that you can do any amount of processing to change the value of related fields in this function and set the final model to reflect the new state. It works fast and reliably. Your onSubmit will of course do something with this state (like writing to the database etc). |
I would like to use
onChange
of one field and get/set value to other field.Ex: I have
Payment Form of Invoice
Please help me.
The text was updated successfully, but these errors were encountered: