Toggle component Type error
Hey Claudio! I've got one for ya :)
I'm using your mobx-react-form nested fields Demo, in rfx-stack. I added more fields to the members[].hobbies[] nested field.
The problem is the field type is not being detected as Boolean unless I set a value for that field. Since it is nested, and I can add more fields, as soon as I click + to add a fieldset with the Toggle element, I get props errors because mobx-react-form sets the value as a blank string, instead of false. And thus when I click the Toggle it does not work or set correct value from form state.
Warning: Failed prop type: Invalid prop
switchedof typestringsupplied toEnhancedSwitch, expectedboolean.
The documentation only mentions in using Separated Properties method of initializing fields I can pass in a defaults and types, but without any example of formatting I am lost. I tried using members[].hobbies[] formatting and also as a nested object, with no luck. I wish to use Boolean for the checkbox value as material-ui requires, so that I may also align the type with my feathers/mongoose API schema as Boolean also.
I hope I am making sense! I can show example code or repo if you need.
Rich
Hi @richattack
I replicated your issue, it seems the defaults boolean are not set for nested fields.
Anyway using defaults is not the solution because they are applied only on field reset.
For this purpose I will introduce the initials object for setting the initial prop (which will be structured like defaults, not values) and also better handling for the types/type.
I will create failing tests for this case and I will fix it asap.
Thank You!
This update will only work for separated props mode.
The values object will take precedence on the initials object anyway.
So if you set a value prop for a field it will fill its initial prop, discarding the initials object value.
Example:
// assume we have these nested fields:
const fields = [
'members[].hobbies[]',
'members[].hobbies[].name',
'members[].hobbies[].active',
];
// we can setup initial values this way:
const initials = {
'members[].hobbies[].name': 'Initial Value',
'members[].hobbies[].active': true,
};
Thanks Claudio!! That was fast! Much appreciation. I am trying it now.
@foxhound87 I am so sorry to bother you again. But the test and fix only works if the initial value is true. My toggle components must be initial value of false, which doesn't work with your fix. mobx-react-form still tries to set the value of "on" Instead of false. which is a string and the component doesn't like it.
No bother! Hope this fix should work. Let me know if you encounter other issues.
Yesss you rock!!!
Thanks Richard, I really appreciate your quick feedback!
When I upgrade from mobx-react-form 1.18.9 to 1.18.11 my form broke and it seems to have something to do with the initial values. It's possible that I did something wrong myself before and this only exposed my bug.
I have a repeating form without initial values. When I add a sub form I get an error that seems to have to do with the initial value switching from the empty string to undefined (I use the initial value to determine the options available in another select box, which then breaks). I may be able to fix this with initials, but then it's a critical issue that initials doesn't appear to be documented. I'd also expect information about what to do in the release notes.
In addition with this change I now also get complaints from React that the "value" prop on "input" should not be null.
I tried to fix it by adding initials for my nested subforms setting the values to empty strings. This seems to have no effect. Maybe I'm using initials wrong?
Hi @faassen
Can you provide your form code?
@faassen I replicated your issue, I will fix it asap.
This fix will cover strings, numbers, booleans relying on initial/default/value props and also on type prop.
I still get an error when I try to add a subform:
[mobx] Encountered an uncaught exception that was thrown by a reaction or observer component, in: 'Reaction[RuleForm#0.render()] Error: The selected field is not defined (extractor)
at Object.throwError (http://localhost:3000/static/js/bundle.js:65552:10)
at Field.select (http://localhost:3000/static/js/bundle.js:75053:36)
at Field.$ (http://localhost:3000/static/js/bundle.js:74420:18)
at http://localhost:3000/static/js/bundle.js:57916:39
at Array.map (native)
at Field.map (http://localhost:3000/static/js/bundle.js:74722:36)
at RuleForm.render (http://localhost:3000/static/js/bundle.js:57898:35)
at Object.allowStateChanges (http://localhost:3000/static/js/bundle.js:33497:16)
at http://localhost:3000/static/js/bundle.js:36072:45
at trackDerivedFunction (http://localhost:3000/static/js/bundle.js:33782:21)
This seems to be a different error than I got yesterday. Previously one reason for the trouble was before the extractor field was there, but undefined instead of the empty string. Now it seems to be completely absent.
[I thought I posted this yesterday, here it is. I will work on a way to replicate]
This error is not related to the previous issue.
You are trying to select a fields that doesn't exist.
I suggest you to use the devtools to check your form structure and fields paths.
Claudio, I did some work with the latest fixes and I've run into this same error when using nested fields. Field not found error when trying to create nested fields. I received this error when using the mbox-react-form-demo nested-fields demo code in the rfx-stack. Maybe it is related to rfx-stack?
I don't think the issue with nested fields is related to the initial values or rfx-stack code.
I released another fix with better initial/default values handling.
At this point I have tried all possibilities, and my tests are ok.
Please share your form configuration.
OK thanks! I will check my configuration and share later today when I get home.