Replies: 19 comments
-
Hello @msageryd We had a myriad of long discussions about the condition definitions and to be honest we are not exactly happy with the current implementation either. It would deserve a proper revisit. Unfortunately, we are talking about breaking change which will impact probably all of our users. So if we want to incorporate change like that to v2.x, we still need to support the current format. And we do not plant v3 release at this time. So this would have to be a separate feature. That said we always appreciate fresh pair of eyes to look over the project. So any suggestion and potential collaboration are more than welcome.
That is exactly how the condition was originally used. It was only for dynamically showing/hiding certain fields. The ability to set value was added later and we had to maintain compatibility of the schema. So that is the main and really only reason why the definition is like this. To preserve backwards compatibility until we will move forward with next major release. I am not against having a global condition definition. It does make sense to me. We could introduce a new rule to the schema validation to preserve the functionality without breaking changes. Of there is a global @rvsia do you have anything to add? |
Beta Was this translation helpful? Give feedback.
-
If you want to move towards a central condition object you could have a compability function which collects all field level conditions and puts them in the central condition object. The only thing you'd need to add is the fieldName for visibility conditions (since those are without fieldNames in current solution). |
Beta Was this translation helpful? Give feedback.
-
Yes, that would be the next step. Once we have a solid solution to the global condition config. We would have to deal with potential conflicts between the global and field level definitions. |
Beta Was this translation helpful? Give feedback.
-
@msageryd Thanks for suggesting this! We have discussed this feature some time ago so yeah, I agree with introducing some kind of 'lifecycle' condition that is computed after each change. We just need to be extra careful in terms of performance, but I think it should be manageable. Having hide/show: 'field1'
// or
hide/show: ['field1', 'field2'] Also, to get cleaner syntax, we should get rid of |
Beta Was this translation helpful? Give feedback.
-
How about maintaining a list It would be cool if final-form had such a list which would reset at a manual checkpoint. Maybe form.modifiedSinceLastCheckpoint, accompanied by a function for creating a checkpoint. |
Beta Was this translation helpful? Give feedback.
-
No, scratch that idea. I'm now fiddling with the following:
The resulting map (or object) will have one key for every "test-triggering" fieldName. Since each condition can be triggered by many fields, the same condition can be referenced from more than one place in the map. The map will be a speedy lookup for whenever a field is changed. The only condition tests that need to be ran are those in the array for the particular field. I'm building this for myself right now. Just tell me if you are interested in the code. Any thoughts? |
Beta Was this translation helpful? Give feedback.
-
@msageryd feel free to open PR. We always appreciate any improvents. I wont have more time today to discuss but i will take a look tommorow as soon as i can. |
Beta Was this translation helpful? Give feedback.
-
@msageryd did you have any time to publish the example? I am very interested in your proposition. |
Beta Was this translation helpful? Give feedback.
-
I actually went in another direction. Ended up just using your elegant condition parser. I had too much special requirements, so I built a quite different form engine. But I do think that the core of my condition handler would fit nicely with data-drive-forms. It would be great if I could Before porting my condition handler I would like to discuss some details so I can get is as much right as possible. Some differences between my code and your code:
The concept for my condition handler:
uiState actually hold an array (stack) of active conditions per fieldName. This is done because one field can be affected by more than one condition at the same time. The lastly added condition is the "current". If this condition stops being met, it will be removed from the stack and the next item will automatically be the current. The code for a Field component might look like this:
I think that data-driven-forms would need a helper function for this, since it's not very straight forward. The "current" active condition is always the first in the stack [0].
I realise that I'm quite bad at trying to explain what I do. The code will probably tell the story a litle better. This is how my conditions section looks like in the form definition:
|
Beta Was this translation helpful? Give feedback.
-
Definitely yes, I think it could be a nice improvement. As I stated before, the only condition would be keeping backward compatibility of the schema and no breaking changes of the To address some of your points.
There should be no problems with any logic. I've found that the biggest issue when porting from React native to React and vice verse is the difference in styling. So we should be good here.
Yup just another implementation of "reducer"
I can understand that. We actually have an unreleased package with schema parsers. We used to do a lot of conversion between a number of different schema types into the DDF schema we use now.
Agreed on this point.
This will require traversal of the schema before render and extracting all conditions into such a global definition. You can use the schema TS definition to help you navigate through it. We do traverse schema already for validation purposes to avoid potential runtime error if invalid schema is passed.
Yes, we evaluate them in order. I can't wait for the PR. |
Beta Was this translation helpful? Give feedback.
-
I tried to get going with this, but I'm running into trouble. Mind you, I've never even started a React app. But it doesn't seem too hard, according to your docs https://github.com/msageryd/react-forms/blob/master/packages/react-form-renderer/README.md#development-setup.
|
Beta Was this translation helpful? Give feedback.
-
@msageryd did you build all packages or just one? Running build in root is required before starting development. We allow building packages separately to make it easier to test our builds. We should explicitly state that in docs. |
Beta Was this translation helpful? Give feedback.
-
yarn install and yarn build was executed in the root (the react-forms folder) |
Beta Was this translation helpful? Give feedback.
-
@msageryd Can you try to run |
Beta Was this translation helpful? Give feedback.
-
@rvsia Thanks, that worked. There is a PR waiting to be checked out. The code lives in a branch "new-conditions-structure". I altered index.js in react-form-renderer so it shows some conditions examples. |
Beta Was this translation helpful? Give feedback.
-
Hmm. I just discovered that my uiStateReducer is buggy. This code was originally quite different (bult with Immer). I'll need to go through it some more to make it work as intended. There is a problem with removing old uiStates. You will be able to see the guts of the proposed solution anyway. If you find it interesting, I'll go ahead and fix the bug. |
Beta Was this translation helpful? Give feedback.
-
I will try to check it out today. If I don't give any feedback, don't hesitate to remind me (lot of stuff is going on atm.). |
Beta Was this translation helpful? Give feedback.
-
I've fixed the reducer bug. I also made the sample form more to the point. |
Beta Was this translation helpful? Give feedback.
-
Looking at the code right now. |
Beta Was this translation helpful? Give feedback.
-
Hi.
Thank you for building this really nice form renderer. I'm curious about the design decision to give the possibility put "condition" in every "field". I can only think of one argument to have a condition tied to a specific field, to set the visibility och this very field without having to appoint the field by name. I might have misunderstood something..
A setter can set values for any named field, regardless of which is the active field (i.e. regardless of which field the condition belongs to). Actually, the setter has to state the name of the field to set its value, so there is no use for the knowledge of which field the condition belong to.
With many conditions it can be a bit messy. I wonder if you contemplated to put all conditions in one big form level condition object instead of spreading conditions among the fields? This condition object could set visibility for named fields, much like values can be set for named fields.
I'm not suggesting a rebuild of your condition engine. I just want to understand it better.
I mean like this:
Beta Was this translation helpful? Give feedback.
All reactions