-
What is the best way to show/hide "child" traits when a parent trait changes? I have components with custom traits. Some traits are only relevant when another trait is "turned on". How can I show/hide traits that are relevant to the current state of the component? For instance, say I have a "Layout" component. By default, child elements display as blocks. On the Layout component, I have a trait to turn component into a css grid. If this trait is checked, child elements will display as grid items, and I show additional traits to show, including grid gap, grid columns, etc. This layout component is not a real component - I am just trying to describe a simple scenario where showing/hiding traits make sense. Anyway, I see two methods.
Reference: #1460 This seems to produce an infinite loop where the component is constantly updating itself. I am not sure why, any insight would be appreciated.
Is there a "best practice"? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Hi did you got solution for this? |
Beta Was this translation helpful? Give feedback.
-
Yes. I have two solutions you can try. First is the second method I describe above. Essentially, you make a super trait that contains all the child traits. Here is an example of an Animation trait. The user is initially presented with a drop down containing a list of possible CSS animations that can be applied to the component. When the user selects an animation, 3 more "traits" show up that allow the user to configure the settings of the animation. These values are saved as style attributes of the component.
You could also stringify the values of the child traits as JSON data and save that as the value of the parent trait. Method 2Here is another method. This one does not involve a custom trait. All of the logic to show/hide traits is defined when the component's model is initiated. Warning: this is untested code. I extracted it from my application, which is not very well organized. There may be better, cleaner implementations, but I think it demonstrates the basic idea.
|
Beta Was this translation helpful? Give feedback.
-
Is there is more elegant solution, when one trait depends on the value of another trait? Any updates planing for implementing "dynamic" traits @artf ? |
Beta Was this translation helpful? Give feedback.
Yes. I have two solutions you can try.
First is the second method I describe above. Essentially, you make a super trait that contains all the child traits.
Here is an example of an Animation trait. The user is initially presented with a drop down containing a list of possible CSS animations that can be applied to the component. When the user selects an animation, 3 more "traits" show up that allow the user to configure the settings of the animation. These values are saved as style attributes of the component.