mobx-react-form icon indicating copy to clipboard operation
mobx-react-form copied to clipboard

Toggle component Type error

Open richieio opened this issue 9 years ago • 18 comments

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 switched of type string supplied to EnhancedSwitch, expected boolean.

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

richieio avatar Feb 05 '17 15:02 richieio

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!

foxhound87 avatar Feb 05 '17 16:02 foxhound87

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,
};

foxhound87 avatar Feb 05 '17 16:02 foxhound87

Thanks Claudio!! That was fast! Much appreciation. I am trying it now.

richieio avatar Feb 05 '17 20:02 richieio

@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.

richieio avatar Feb 05 '17 21:02 richieio

No bother! Hope this fix should work. Let me know if you encounter other issues.

foxhound87 avatar Feb 05 '17 22:02 foxhound87

Yesss you rock!!!

richieio avatar Feb 05 '17 22:02 richieio

Thanks Richard, I really appreciate your quick feedback!

foxhound87 avatar Feb 05 '17 22:02 foxhound87

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.

faassen avatar Feb 06 '17 14:02 faassen

In addition with this change I now also get complaints from React that the "value" prop on "input" should not be null.

faassen avatar Feb 06 '17 14:02 faassen

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?

faassen avatar Feb 06 '17 14:02 faassen

Hi @faassen

Can you provide your form code?

foxhound87 avatar Feb 06 '17 15:02 foxhound87

@faassen I replicated your issue, I will fix it asap.

foxhound87 avatar Feb 06 '17 16:02 foxhound87

This fix will cover strings, numbers, booleans relying on initial/default/value props and also on type prop.

foxhound87 avatar Feb 06 '17 17:02 foxhound87

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]

faassen avatar Feb 08 '17 09:02 faassen

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.

foxhound87 avatar Feb 08 '17 14:02 foxhound87

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?

richieio avatar Feb 08 '17 16:02 richieio

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.

foxhound87 avatar Feb 08 '17 17:02 foxhound87

OK thanks! I will check my configuration and share later today when I get home.

richieio avatar Feb 08 '17 17:02 richieio