Skip to content
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 do I clone a component? #551

Open
RORAKUS opened this issue Nov 25, 2024 · 6 comments
Open

How do I clone a component? #551

RORAKUS opened this issue Nov 25, 2024 · 6 comments
Labels
PR welcomed Pull requests are welcomed

Comments

@RORAKUS
Copy link

RORAKUS commented Nov 25, 2024

How do I clone a KVision component? I'm trying to make a library with a function, where you get a template Component object and it will add it multiple times to a container, each with different attribute values. Cloning would be extremely useful - or is there other way?

I tried JSON serialize/deserialize. Didn't work. Creating my own clone? Not possible - no way to get all attributes so I can clone them.

Thanks!

@rjaros
Copy link
Owner

rjaros commented Nov 25, 2024

I don't think it is possible. I'm not sure what's your use case, but I think you have some function which creates this component. What is the problem to just call this function multiple times?

@RORAKUS
Copy link
Author

RORAKUS commented Nov 25, 2024

@rjaros It doesn't create a component. It receives one as a parameter. I'm making a device specific library, where you have a deviceSpecific{} wrapper, where you can define attribute values for different screen sizes. The point is so you don't have to write the component over and over just to change a small detail. In this wrapper you can normally add components, but the component will be duplicated with proper attributes and display values. Is there any way to duplicate it some shady javascript/kotlin reflection magic way?

@rjaros
Copy link
Owner

rjaros commented Nov 25, 2024

Almost all KVision components are also containers for some other components, so even duplicating all properties and attributes would not be enough. We would have to duplicate whole subtrees of components and rewrite all the references from children to their parents. And there is no way to do this with some general method in some top level component. It would be really a lot of work.

I have the impression that your problem would be easier to solve with a more functional approach, but I don't know how would it impact your whole project.

@RORAKUS
Copy link
Author

RORAKUS commented Nov 25, 2024

Do you think the work would be worth it? I think it should! A proper framework like this should surely have a method to copy/clone objects! I'm also missing a direct way to recall all component classes and attributes. It would be useful too.
I've looked into the code and it sure looks hard, but possible.

@rjaros
Copy link
Owner

rjaros commented Nov 25, 2024

I'll try to play a bit with this when I have some spare time. But of course PR is welcomed.

@rjaros rjaros added the PR welcomed Pull requests are welcomed label Nov 25, 2024
@RORAKUS
Copy link
Author

RORAKUS commented Nov 27, 2024

I've successfully implemented what I wanted using after insert hook to duplicate the element after inserting :).
Thanks for fast replies and help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR welcomed Pull requests are welcomed
Projects
None yet
Development

No branches or pull requests

2 participants